/** * Pure-JS Byte-Level BPE tokenizer compatible with HuggingFace tokenizers.json. * Supports GPT-2 byte mapping, //// special tokens, * and UTF-8 Turkish text round-trip parity. */ (function (root) { function buildByteTables() { const bs = []; for (let i = 33; i <= 126; i++) bs.push(i); for (let i = 161; i <= 172; i++) bs.push(i); for (let i = 174; i <= 255; i++) bs.push(i); const cs = bs.slice(); let n = 0; for (let b = 0; b < 256; b++) { if (!bs.includes(b)) { bs.push(b); cs.push(256 + n); n += 1; } } const b2u = new Array(256); const u2b = new Map(); for (let i = 0; i < bs.length; i++) { const ch = String.fromCodePoint(cs[i]); b2u[bs[i]] = ch; u2b.set(ch, bs[i]); } return { b2u, u2b }; } class MimaTokenizer { constructor(tokJson) { const { b2u, u2b } = buildByteTables(); this.b2u = b2u; this.u2b = u2b; this.utf8Enc = new TextEncoder(); this.utf8Dec = new TextDecoder("utf-8", { fatal: false }); this.vocab = tokJson.model.vocab; this.idToTok = []; for (const [tok, id] of Object.entries(this.vocab)) { this.idToTok[id] = tok; } this.specialToId = new Map(); this.specialIds = new Set(); for (const at of tokJson.added_tokens || []) { this.specialToId.set(at.content, at.id); this.specialIds.add(at.id); this.idToTok[at.id] = at.content; } this.ranks = new Map(); const merges = tokJson.model.merges || []; for (let i = 0; i < merges.length; i++) { const pair = Array.isArray(merges[i]) ? merges[i] : merges[i].split(" "); this.ranks.set(pair[0] + "\u0000" + pair[1], i); } this.bpeCache = new Map(); this.pat = /'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu; } _bpe(token) { const cached = this.bpeCache.get(token); if (cached) return cached; let word = Array.from(token); if (word.length <= 1) { this.bpeCache.set(token, word); return word; } while (true) { let bestRank = Infinity; let bestPairA = null; let bestPairB = null; for (let i = 0; i < word.length - 1; i++) { const rank = this.ranks.get(word[i] + "\u0000" + word[i + 1]); if (rank !== undefined && rank < bestRank) { bestRank = rank; bestPairA = word[i]; bestPairB = word[i + 1]; } } if (bestPairA === null) break; const next = []; let i = 0; while (i < word.length) { if (i < word.length - 1 && word[i] === bestPairA && word[i + 1] === bestPairB) { next.push(bestPairA + bestPairB); i += 2; } else { next.push(word[i]); i += 1; } } word = next; if (word.length === 1) break; } this.bpeCache.set(token, word); return word; } encodeText(text) { if (!text) return []; const ids = []; const matches = text.match(this.pat) || []; for (const piece of matches) { const bytes = this.utf8Enc.encode(piece); let mapped = ""; for (let i = 0; i < bytes.length; i++) { mapped += this.b2u[bytes[i]]; } for (const sub of this._bpe(mapped)) { const id = this.vocab[sub]; if (id !== undefined) ids.push(id); } } return ids; } decode(ids, skipSpecial = true) { const bytes = []; for (const id of ids) { if (skipSpecial && this.specialIds.has(id)) continue; const tok = this.idToTok[id]; if (tok === undefined) continue; if (!skipSpecial && this.specialToId.has(tok)) { for (const b of this.utf8Enc.encode(tok)) bytes.push(b); continue; } for (const ch of Array.from(tok)) { const b = this.u2b.get(ch); if (b !== undefined) bytes.push(b); } } return this.utf8Dec.decode(new Uint8Array(bytes)); } } root.MimaTokenizer = MimaTokenizer; if (typeof module !== "undefined" && module.exports) { module.exports = { MimaTokenizer }; } })(typeof window !== "undefined" ? window : globalThis);