Datasets:
Download tokenize.js from bcv-commons/compact-alignments: direct link, hf CLI and curl.
- Browser
- Download file 7.91 kB
-
https://huggingface.co/datasets/bcv-commons/compact-alignments/resolve/main/tokenize.js
- Command line
-
hf download hf://datasets/bcv-commons/compact-alignments/tokenize.js
-
curl -L -o tokenize.js https://huggingface.co/datasets/bcv-commons/compact-alignments/resolve/main/tokenize.js
7.91 kB
| // GENERATED by pipeline/scripts/gen_js_tokenizer.py — do not edit by hand. | |
| // Reference tokenizer for the `bcv-commons/compact-alignments` dataset, byte-for-byte equivalent to the | |
| // producing side"s Python `usj_source.tokenize()`. | |
| // | |
| // WHY YOU NEED IT: compact-alignments addresses target words by POSITION in the verse"s own tokenized | |
| // text, never by repeating the word. Tokenize differently and every position after the first divergence | |
| // resolves to the WRONG word, silently — the content hash in the filename cannot catch it, because that | |
| // hash covers the verse TEXT, which is identical either way. | |
| // | |
| // NOT plain whitespace/punctuation splitting. A token is a maximal run of Unicode letters + combining | |
| // marks (\p{L}/\p{M}); punctuation, whitespace AND digits are separators producing no token (a bare | |
| // "40" is invisible to this tokenizer, not its own token). Scripts written without spaces between words | |
| // are then segmented by rule: Han per character, Japanese at kana/script boundaries, Myanmar per syllable. | |
| // | |
| // TOKENIZER_VERSION is published in the dataset manifest. If it does not match the version this file | |
| // implements, REFUSE TO DECODE rather than risk silently misaligned positions. | |
| export const TOKENIZER_VERSION = 2; | |
| // --- canonical combining class != 0 (generated; JS has no unicodedata.combining) --- | |
| const CCC = [[768,846],[848,879],[1155,1159],[1425,1469],[1471,1471],[1473,1474],[1476,1477],[1479,1479],[1552,1562],[1611,1631],[1648,1648],[1750,1756],[1759,1764],[1767,1768],[1770,1773],[1809,1809],[1840,1866],[2027,2035],[2045,2045],[2070,2073],[2075,2083],[2085,2087],[2089,2093],[2137,2139],[2199,2207],[2250,2273],[2275,2303],[2364,2364],[2381,2381],[2385,2388],[2492,2492],[2509,2509],[2558,2558],[2620,2620],[2637,2637],[2748,2748],[2765,2765],[2876,2876],[2893,2893],[3021,3021],[3132,3132],[3149,3149],[3157,3158],[3260,3260],[3277,3277],[3387,3388],[3405,3405],[3530,3530],[3640,3642],[3656,3659],[3768,3770],[3784,3787],[3864,3865],[3893,3893],[3895,3895],[3897,3897],[3953,3954],[3956,3956],[3962,3965],[3968,3968],[3970,3972],[3974,3975],[4038,4038],[4151,4151],[4153,4154],[4237,4237],[4957,4959],[5908,5909],[5940,5940],[6098,6098],[6109,6109],[6313,6313],[6457,6459],[6679,6680],[6752,6752],[6773,6780],[6783,6783],[6832,6845],[6847,6862],[6964,6964],[6980,6980],[7019,7027],[7082,7083],[7142,7142],[7154,7155],[7223,7223],[7376,7378],[7380,7392],[7394,7400],[7405,7405],[7412,7412],[7416,7417],[7616,7679],[8400,8412],[8417,8417],[8421,8432],[11503,11505],[11647,11647],[11744,11775],[12330,12335],[12441,12442],[42607,42607],[42612,42621],[42654,42655],[42736,42737],[43014,43014],[43052,43052],[43204,43204],[43232,43249],[43307,43309],[43347,43347],[43443,43443],[43456,43456],[43696,43696],[43698,43700],[43703,43704],[43710,43711],[43713,43713],[43766,43766],[44013,44013],[64286,64286],[65056,65071],[66045,66045],[66272,66272],[66422,66426],[68109,68109],[68111,68111],[68152,68154],[68159,68159],[68325,68326],[68900,68903],[68969,68973],[69291,69292],[69373,69375],[69446,69456],[69506,69509],[69702,69702],[69744,69744],[69759,69759],[69817,69818],[69888,69890],[69939,69940],[70003,70003],[70080,70080],[70090,70090],[70197,70198],[70377,70378],[70459,70460],[70477,70477],[70502,70508],[70512,70516],[70606,70608],[70722,70722],[70726,70726],[70750,70750],[70850,70851],[71103,71104],[71231,71231],[71350,71351],[71467,71467],[71737,71738],[71997,71998],[72003,72003],[72160,72160],[72244,72244],[72263,72263],[72345,72345],[72767,72767],[73026,73026],[73028,73029],[73111,73111],[73537,73538],[90415,90415],[92912,92916],[92976,92982],[94192,94193],[113822,113822],[119141,119145],[119149,119154],[119163,119170],[119173,119179],[119210,119213],[119362,119364],[122880,122886],[122888,122904],[122907,122913],[122915,122916],[122918,122922],[123023,123023],[123184,123190],[123566,123566],[123628,123631],[124140,124143],[124398,124399],[125136,125142],[125252,125258]]; | |
| function hasCombiningClass(cp) { | |
| let lo = 0, hi = CCC.length - 1; | |
| while (lo <= hi) { | |
| const mid = (lo + hi) >> 1; | |
| if (cp < CCC[mid][0]) hi = mid - 1; | |
| else if (cp > CCC[mid][1]) lo = mid + 1; | |
| else return true; | |
| } | |
| return false; | |
| } | |
| // Myanmar VIRAMA (stacks a consonant) + ASAT (kills a syllable-final one). These share the virama | |
| // combining class with the Indic marks strip_marks drops, but they are not optional diacritics — they | |
| // carry the syllable structure the Myanmar segmenter reads, so they are exempt. | |
| const MYANMAR_KEEP = new Set([0x1039, 0x103a]); | |
| // Variation selectors choose a GLYPH form and carry no linguistic content, but their combining class is | |
| // 0 so they survive the filter above and would split one word into two vocabulary entries. | |
| function isVariationSelector(cp) { | |
| return (cp >= 0xfe00 && cp <= 0xfe0f) || (cp >= 0xe0100 && cp <= 0xe01ef); | |
| } | |
| export function stripMarks(text) { | |
| let out = ""; | |
| for (const ch of text.normalize("NFC")) { | |
| const cp = ch.codePointAt(0); | |
| if ((!hasCombiningClass(cp) || MYANMAR_KEEP.has(cp)) && !isVariationSelector(cp)) out += ch; | |
| } | |
| return out; | |
| } | |
| const LETTER_OR_MARK = /[\p{L}\p{M}]/u; | |
| function cjkClass(cp) { | |
| if ((cp >= 0x4e00 && cp <= 0x9fff) || (cp >= 0x3400 && cp <= 0x4dbf) || | |
| (cp >= 0xf900 && cp <= 0xfaff)) return "han"; | |
| if (cp >= 0x3040 && cp <= 0x309f) return "hira"; | |
| if (cp >= 0x30a0 && cp <= 0x30ff) return "kata"; | |
| return null; | |
| } | |
| function isMyaConsonant(cp) { | |
| return ((cp >= 0x1000 && cp <= 0x102a) || cp === 0x103f || (cp >= 0x1050 && cp <= 0x1055) || | |
| (cp >= 0x105a && cp <= 0x105d) || cp === 0x1061 || (cp >= 0x1065 && cp <= 0x1066) || | |
| (cp >= 0x106e && cp <= 0x1070) || (cp >= 0x1075 && cp <= 0x1081) || cp === 0x108e || | |
| (cp >= 0xa9e0 && cp <= 0xa9e4) || (cp >= 0xa9e7 && cp <= 0xa9ef) || | |
| (cp >= 0xa9fa && cp <= 0xa9fe) || (cp >= 0xaa60 && cp <= 0xaa6f) || | |
| (cp >= 0xaa71 && cp <= 0xaa76) || cp === 0xaa7a || (cp >= 0xaa7e && cp <= 0xaa7f)); | |
| } | |
| const MYA_VIRAMA = 0x1039, MYA_ASAT = 0x103a, MYA_EVOWEL = 0x1031; | |
| // A syllable starts at a consonant, or at U+1031 (written BEFORE its consonant but pronounced after). | |
| // It does not start when the consonant is stacked under a virama, nor right after U+1031, nor after an | |
| // asat — an asat-killed consonant closes the current syllable instead of opening the next. | |
| function myaSyllables(tok) { | |
| const chars = Array.from(tok); | |
| const out = []; | |
| let cur = []; | |
| for (let i = 0; i < chars.length; i++) { | |
| const cp = chars[i].codePointAt(0); | |
| const prev = i ? chars[i - 1].codePointAt(0) : 0; | |
| if ((isMyaConsonant(cp) || cp === MYA_EVOWEL) && cur.length && | |
| prev !== MYA_VIRAMA && prev !== MYA_EVOWEL && prev !== MYA_ASAT) { | |
| out.push(cur.join("")); cur = []; | |
| } | |
| cur.push(chars[i]); | |
| } | |
| if (cur.length) out.push(cur.join("")); | |
| return out; | |
| } | |
| function splitUnspaced(tok) { | |
| const chars = Array.from(tok); | |
| if (chars.some((c) => isMyaConsonant(c.codePointAt(0)))) return myaSyllables(tok); | |
| if (!chars.some((c) => cjkClass(c.codePointAt(0)))) return [tok]; | |
| const out = []; | |
| let cur = [], curCls = null; | |
| for (const ch of chars) { | |
| const cls = cjkClass(ch.codePointAt(0)); | |
| if (cls === "han") { // one token per Han character | |
| if (cur.length) { out.push(cur.join("")); cur = []; curCls = null; } | |
| out.push(ch); | |
| } else if (cls !== curCls && cur.length) { // script boundary | |
| out.push(cur.join("")); cur = [ch]; curCls = cls; | |
| } else { | |
| cur.push(ch); curCls = cls; | |
| } | |
| } | |
| if (cur.length) out.push(cur.join("")); | |
| return out; | |
| } | |
| export function tokenize(text) { | |
| const out = []; | |
| let cur = []; | |
| for (const ch of stripMarks(text)) { | |
| if (LETTER_OR_MARK.test(ch)) cur.push(ch); | |
| else if (cur.length) { out.push(...splitUnspaced(cur.join(""))); cur = []; } | |
| } | |
| if (cur.length) out.push(...splitUnspaced(cur.join(""))); | |
| return out; | |
| } | |