larsgson commited on
Commit
ca77c5d
·
verified ·
1 Parent(s): 594483e

compact-alignments: batch 1/1 (2 file(s))

Browse files
Files changed (2) hide show
  1. .publish_state.json +0 -0
  2. tokenize.js +125 -0
.publish_state.json CHANGED
The diff for this file is too large to render. See raw diff
 
tokenize.js ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // GENERATED by pipeline/scripts/gen_js_tokenizer.py — do not edit by hand.
2
+ // Reference tokenizer for the `bcv-commons/compact-alignments` dataset, byte-for-byte equivalent to the
3
+ // producing side"s Python `usj_source.tokenize()`.
4
+ //
5
+ // WHY YOU NEED IT: compact-alignments addresses target words by POSITION in the verse"s own tokenized
6
+ // text, never by repeating the word. Tokenize differently and every position after the first divergence
7
+ // resolves to the WRONG word, silently — the content hash in the filename cannot catch it, because that
8
+ // hash covers the verse TEXT, which is identical either way.
9
+ //
10
+ // NOT plain whitespace/punctuation splitting. A token is a maximal run of Unicode letters + combining
11
+ // marks (\p{L}/\p{M}); punctuation, whitespace AND digits are separators producing no token (a bare
12
+ // "40" is invisible to this tokenizer, not its own token). Scripts written without spaces between words
13
+ // are then segmented by rule: Han per character, Japanese at kana/script boundaries, Myanmar per syllable.
14
+ //
15
+ // TOKENIZER_VERSION is published in the dataset manifest. If it does not match the version this file
16
+ // implements, REFUSE TO DECODE rather than risk silently misaligned positions.
17
+
18
+ export const TOKENIZER_VERSION = 2;
19
+
20
+ // --- canonical combining class != 0 (generated; JS has no unicodedata.combining) ---
21
+ 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]];
22
+
23
+ function hasCombiningClass(cp) {
24
+ let lo = 0, hi = CCC.length - 1;
25
+ while (lo <= hi) {
26
+ const mid = (lo + hi) >> 1;
27
+ if (cp < CCC[mid][0]) hi = mid - 1;
28
+ else if (cp > CCC[mid][1]) lo = mid + 1;
29
+ else return true;
30
+ }
31
+ return false;
32
+ }
33
+
34
+ // Myanmar VIRAMA (stacks a consonant) + ASAT (kills a syllable-final one). These share the virama
35
+ // combining class with the Indic marks strip_marks drops, but they are not optional diacritics — they
36
+ // carry the syllable structure the Myanmar segmenter reads, so they are exempt.
37
+ const MYANMAR_KEEP = new Set([0x1039, 0x103a]);
38
+
39
+ // Variation selectors choose a GLYPH form and carry no linguistic content, but their combining class is
40
+ // 0 so they survive the filter above and would split one word into two vocabulary entries.
41
+ function isVariationSelector(cp) {
42
+ return (cp >= 0xfe00 && cp <= 0xfe0f) || (cp >= 0xe0100 && cp <= 0xe01ef);
43
+ }
44
+
45
+ export function stripMarks(text) {
46
+ let out = "";
47
+ for (const ch of text.normalize("NFC")) {
48
+ const cp = ch.codePointAt(0);
49
+ if ((!hasCombiningClass(cp) || MYANMAR_KEEP.has(cp)) && !isVariationSelector(cp)) out += ch;
50
+ }
51
+ return out;
52
+ }
53
+
54
+ const LETTER_OR_MARK = /[\p{L}\p{M}]/u;
55
+
56
+ function cjkClass(cp) {
57
+ if ((cp >= 0x4e00 && cp <= 0x9fff) || (cp >= 0x3400 && cp <= 0x4dbf) ||
58
+ (cp >= 0xf900 && cp <= 0xfaff)) return "han";
59
+ if (cp >= 0x3040 && cp <= 0x309f) return "hira";
60
+ if (cp >= 0x30a0 && cp <= 0x30ff) return "kata";
61
+ return null;
62
+ }
63
+
64
+ function isMyaConsonant(cp) {
65
+ return ((cp >= 0x1000 && cp <= 0x102a) || cp === 0x103f || (cp >= 0x1050 && cp <= 0x1055) ||
66
+ (cp >= 0x105a && cp <= 0x105d) || cp === 0x1061 || (cp >= 0x1065 && cp <= 0x1066) ||
67
+ (cp >= 0x106e && cp <= 0x1070) || (cp >= 0x1075 && cp <= 0x1081) || cp === 0x108e ||
68
+ (cp >= 0xa9e0 && cp <= 0xa9e4) || (cp >= 0xa9e7 && cp <= 0xa9ef) ||
69
+ (cp >= 0xa9fa && cp <= 0xa9fe) || (cp >= 0xaa60 && cp <= 0xaa6f) ||
70
+ (cp >= 0xaa71 && cp <= 0xaa76) || cp === 0xaa7a || (cp >= 0xaa7e && cp <= 0xaa7f));
71
+ }
72
+
73
+ const MYA_VIRAMA = 0x1039, MYA_ASAT = 0x103a, MYA_EVOWEL = 0x1031;
74
+
75
+ // A syllable starts at a consonant, or at U+1031 (written BEFORE its consonant but pronounced after).
76
+ // It does not start when the consonant is stacked under a virama, nor right after U+1031, nor after an
77
+ // asat — an asat-killed consonant closes the current syllable instead of opening the next.
78
+ function myaSyllables(tok) {
79
+ const chars = Array.from(tok);
80
+ const out = [];
81
+ let cur = [];
82
+ for (let i = 0; i < chars.length; i++) {
83
+ const cp = chars[i].codePointAt(0);
84
+ const prev = i ? chars[i - 1].codePointAt(0) : 0;
85
+ if ((isMyaConsonant(cp) || cp === MYA_EVOWEL) && cur.length &&
86
+ prev !== MYA_VIRAMA && prev !== MYA_EVOWEL && prev !== MYA_ASAT) {
87
+ out.push(cur.join("")); cur = [];
88
+ }
89
+ cur.push(chars[i]);
90
+ }
91
+ if (cur.length) out.push(cur.join(""));
92
+ return out;
93
+ }
94
+
95
+ function splitUnspaced(tok) {
96
+ const chars = Array.from(tok);
97
+ if (chars.some((c) => isMyaConsonant(c.codePointAt(0)))) return myaSyllables(tok);
98
+ if (!chars.some((c) => cjkClass(c.codePointAt(0)))) return [tok];
99
+ const out = [];
100
+ let cur = [], curCls = null;
101
+ for (const ch of chars) {
102
+ const cls = cjkClass(ch.codePointAt(0));
103
+ if (cls === "han") { // one token per Han character
104
+ if (cur.length) { out.push(cur.join("")); cur = []; curCls = null; }
105
+ out.push(ch);
106
+ } else if (cls !== curCls && cur.length) { // script boundary
107
+ out.push(cur.join("")); cur = [ch]; curCls = cls;
108
+ } else {
109
+ cur.push(ch); curCls = cls;
110
+ }
111
+ }
112
+ if (cur.length) out.push(cur.join(""));
113
+ return out;
114
+ }
115
+
116
+ export function tokenize(text) {
117
+ const out = [];
118
+ let cur = [];
119
+ for (const ch of stripMarks(text)) {
120
+ if (LETTER_OR_MARK.test(ch)) cur.push(ch);
121
+ else if (cur.length) { out.push(...splitUnspaced(cur.join(""))); cur = []; }
122
+ }
123
+ if (cur.length) out.push(...splitUnspaced(cur.join("")));
124
+ return out;
125
+ }