-
Notifications
You must be signed in to change notification settings - Fork 212
/
gen-use.js
285 lines (262 loc) · 5.75 KB
/
gen-use.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import codepoints from 'codepoints';
import fs from 'fs';
import UnicodeTrieBuilder from 'unicode-trie/builder';
import compile from 'dfa/compile';
const CATEGORIES = {
B: [
{UISC: 'Number'},
{UISC: 'Avagraha', UGC: 'Lo'},
{UISC: 'Bindu', UGC: 'Lo'},
{UISC: 'Consonant'},
{UISC: 'Consonant_Final', UGC: 'Lo'},
{UISC: 'Consonant_Head_Letter'},
{UISC: 'Consonant_Medial', UGC: 'Lo'},
{UISC: 'Consonant_Subjoined', UGC: 'Lo'},
{UISC: 'Tone_Letter'},
{UISC: 'Vowel', UGC: 'Lo'},
{UISC: 'Vowel_Independent'},
{UISC: 'Vowel_Dependent', UGC: 'Lo'}
],
CGJ: [0x034f],
CM: [
'Nukta',
'Gemination_Mark',
'Consonant_Killer'
],
CS: [
'Consonant_With_Stacker'
],
F: [
{UISC: 'Consonant_Final', UGC: {not: 'Lo'}},
{UISC: 'Consonant_Succeeding_Repha'}
],
FM: [
'Syllable_Modifier'
],
GB: [
'Consonant_Placeholder',
0x2015,
0x2022,
0x25fb,
0x25fc,
0x25fd,
0x25fe
],
H: [
'Virama',
'Invisible_Stacker'
],
HN: [
'Number_Joiner'
],
IND: [
'Consonant_Dead',
'Modifying_Letter',
{UGC: 'Po', U: {not: [0x104e, 0x2022]}}
],
M: [
{UISC: 'Consonant_Medial', UGC: {not: 'Lo'}}
],
N: [
'Brahmi_Joining_Number'
],
R: [
'Consonant_Preceding_Repha',
'Consonant_Prefixed'
],
Rsv: [
{UGC: 'Cn'} // TODO
],
S: [
{UGC: 'So', U: {not: 0x25cc}},
{UGC: 'Sc'}
],
SM: [
0x1b6b,
0x1b6c,
0x1b6d,
0x1b6e,
0x1b6f,
0x1b70,
0x1b71,
0x1b72,
0x1b73
],
SUB: [
{UISC: 'Consonant_Subjoined', UGC: {not: 'Lo'}}
],
V: [
{UISC: 'Vowel', UGC: {not: 'Lo'}},
{UISC: 'Vowel_Dependent', UGC: {not: 'Lo'}},
{UISC: 'Pure_Killer'}
],
VM: [
{UISC: 'Bindu', UGC: {not: 'Lo'}},
'Tone_Mark',
'Cantillation_Mark',
'Register_Shifter',
'Visarga'
],
VS: [
0xfe00, 0xfe01, 0xfe02, 0xfe03, 0xfe04, 0xfe05, 0xfe06, 0xfe07,
0xfe08, 0xfe09, 0xfe0a, 0xfe0b, 0xfe0c, 0xfe0d, 0xfe0e, 0xfe0f
],
WJ: [0x2060],
ZWJ: [
'Joiner'
],
ZWNJ: [
'Non_Joiner'
],
O: [
'Other'
]
};
const USE_POSITIONS = {
F: {
Abv: ['Top'],
Blw: ['Bottom'],
Pst: ['Right'],
},
M: {
Abv: ['Top'],
Blw: ['Bottom'],
Pst: ['Right'],
Pre: ['Left'],
},
CM: {
Abv: ['Top'],
Blw: ['Bottom'],
},
V: {
Abv: ['Top', 'Top_And_Bottom', 'Top_And_Bottom_And_Right', 'Top_And_Right'],
Blw: ['Bottom', 'Overstruck', 'Bottom_And_Right'],
Pst: ['Right'],
Pre: ['Left', 'Top_And_Left', 'Top_And_Left_And_Right', 'Left_And_Right'],
},
VM: {
Abv: ['Top'],
Blw: ['Bottom', 'Overstruck'],
Pst: ['Right'],
Pre: ['Left'],
},
SM: {
Abv: ['Top'],
Blw: ['Bottom'],
}
};
const UISC_OVERRIDE = {
0x17dd: 'Vowel_Dependent',
0x1ce2: 'Cantillation_Mark',
0x1ce3: 'Cantillation_Mark',
0x1ce4: 'Cantillation_Mark',
0x1ce5: 'Cantillation_Mark',
0x1ce6: 'Cantillation_Mark',
0x1ce7: 'Cantillation_Mark',
0x1ce8: 'Cantillation_Mark',
0x1ced: 'Tone_Mark'
};
const UIPC_OVERRIDE = {
0x1b6c: 'Bottom',
0x953: 'Not_Applicable',
0x954: 'Not_Applicable',
0x103c: 'Left',
0xa926: 'Top',
0xa927: 'Top',
0xa928: 'Top',
0xa929: 'Top',
0xa92a: 'Top',
0x111ca: 'Bottom',
0x11300: 'Top',
0x1133c: 'Bottom',
0x1171e: 'Left',
0x1cf2: 'Right',
0x1cf3: 'Right',
0x1cf8: 'Top',
0x1cf9: 'Top'
};
function check(pattern, value) {
if (typeof pattern === 'object' && pattern.not) {
if (Array.isArray(pattern.not)) {
return pattern.not.indexOf(value) === -1;
} else {
return value !== pattern.not;
}
}
return value === pattern;
}
function matches(pattern, code) {
if (typeof pattern === 'number') {
pattern = {U: pattern};
} else if (typeof pattern === 'string') {
pattern = {UISC: pattern};
}
for (let key in pattern) {
if (!check(pattern[key], code[key])) {
return false;
}
}
return true;
}
function getUISC(code) {
return UISC_OVERRIDE[code.code] || code.indicSyllabicCategory || 'Other';
}
function getUIPC(code) {
return UIPC_OVERRIDE[code.code] || code.indicPositionalCategory;
}
function getPositionalCategory(code, USE) {
let UIPC = getUIPC(code);
let pos = USE_POSITIONS[USE];
if (pos) {
for (let key in pos) {
if (pos[key].indexOf(UIPC) !== -1) {
return USE + key;
}
}
}
return USE;
}
function getCategory(code) {
for (let category in CATEGORIES) {
for (let pattern of CATEGORIES[category]) {
if (matches(pattern, {UISC: getUISC(code), UGC: code.category, U: code.code})) {
return getPositionalCategory(code, category);
}
}
}
return null;
}
let trie = new UnicodeTrieBuilder;
let symbols = {};
let numSymbols = 0;
let decompositions = {};
for (let i = 0; i < codepoints.length; i++) {
let codepoint = codepoints[i];
if (codepoint) {
let category = getCategory(codepoint);
if (!(category in symbols)) {
symbols[category] = numSymbols++;
}
trie.set(codepoint.code, symbols[category]);
if (codepoint.indicSyllabicCategory === 'Vowel_Dependent' && codepoint.decomposition.length > 0) {
decompositions[codepoint.code] = decompose(codepoint.code);
}
}
}
function decompose(code) {
let decomposition = [];
let codepoint = codepoints[code];
for (let c of codepoint.decomposition) {
let codes = decompose(c);
codes = codes.length > 0 ? codes : [c];
decomposition.push(...codes);
}
return decomposition;
}
fs.writeFileSync(__dirname + '/use.trie', trie.toBuffer());
let stateMachine = compile(fs.readFileSync(__dirname + '/use.machine', 'utf8'), symbols);
let json = Object.assign({
categories: Object.keys(symbols),
decompositions: decompositions
}, stateMachine);
fs.writeFileSync(__dirname + '/use.json', JSON.stringify(json));