-
Notifications
You must be signed in to change notification settings - Fork 1
/
pellk.js
456 lines (413 loc) · 12.8 KB
/
pellk.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//Latin to Arabic Array
export const sConvertLatin2Arabic = new Array(
'(^|[^a-zêîûçş0-9\'’])([aeiouêîû])', '$1ئ$2', //insert hamza
'(ئ)([uû])([^êîûçşa-z0-9])', 'و$3', //omit the inserted hamza for 'u' (=and)
'a', 'ا',
'b', 'ب',
'ch|ç', 'چ',
'c', 'ج',
'd', 'د',
'ee|ê|é', 'ێ',
'e', 'ە',
'f', 'ف',
'gh', 'غ',
'g', 'گ',
'hh', 'ح',
'sh|ş', 'ش',
'h', 'ھ',
'î|y|í', 'ی',
'j', 'ژ',
'k', 'ک',
'll', 'ڵ',
'l', 'ل',
'm', 'م',
'n', 'ن',
'o', 'ۆ',
'p', 'پ',
'q', 'ق',
'rr', 'ڕ',
'r', 'ر',
's', 'س',
't', 'ت',
'uu|û|ú', 'وو',
'u|w', 'و',
'v', 'ڤ',
'x', 'خ',
'z', 'ز',
'\'|’', 'ع',
'\\u003F', '؟', //question mark
'\,', '،',
'\;', '؛'
);
export const sSuffixes = new Array(
'[ ]+()ئ(م|ی|ە|ین|ن|ا|ێ|ێن)($|[^ء-يٱ-ە])', //e.g. ziman a
'[ ]+()(یی|یە|یا|یێ|یێن)($|[^ء-يٱ-ە])', //e.g. zana ye
'([^ء-يٱ-ە])ئ(م|ی|ە|ین|ن|ا|ێ|ێن)($|[^ء-يٱ-ە])' //e.g. (ziman)a
);
export const sCapitalLetters = new Array(
'ê', 'Ê',
'î', 'Î',
'û', 'Û',
'ç', 'Ç',
'ş', 'Ş'
);
// Arabic to Latin Array
export const sConvertArabic2Latin = new Array(
//managing 'و' and 'ی'
'و([اێۆە])', 'w$1', //و + vowel => w (e.g. wan)
'ی([اێۆە])', 'y$1', //ی + vowel => y (e.g. yas)
'([اێۆە])و', '$1w', //vowel + و => w (e.g. kew)
'([اێۆە])ی', '$1y', //vowel + ی => y (e.g. bey)
'(^|[^ء-يٱ-ەwy])و([^ء-يٱ-ەwy])', '$1û$2', //non-letter + 'و' + non-letter => û (=and)
'(^|[^ء-يٱ-ەwy])و', '$1w', //non-letter + 'و' => w (e.g. wtar)
'یو', 'îw', //'ی' + 'و' => îw (e.g. nîw)
'یی', 'îy', //'ی' + 'ی' => îy (e.g. kanîy)
'وی', 'uy', //'و' + 'ی' => uy (e.g. buyn)
'وو', 'û', //'و' + 'و' => û (e.g. bû)
'ی', 'î',
'و', 'u',
'uu', 'û', //'و' + 'و' => û (e.g. bû)
'([ء-يٱ-ەîuûwy])ڕ', '$1rr', //when 'ڕ' not at the beginning of a word => rr
'ر|ڕ', 'r',
'ش', 'ş',
'ئ', '',
'ا', 'a',
'ب', 'b',
'چ', 'ç',
'ج', 'c',
'د', 'd',
'ێ', 'ê',
'ە|ه', 'e',
'ف', 'f',
'خ|غ', 'x',
'گ', 'g',
'ح|ھ', 'h',
'ژ', 'j',
'ک', 'k',
'ڵ', 'll',
'ل', 'l',
'م', 'm',
'ن', 'n',
'ۆ', 'o',
'پ', 'p',
'ق', 'q',
'س', 's',
'ت', 't',
'ڤ', 'v',
'ز', 'z',
'ع', '\'',
'', '',
'؟', '?',
'،', '\,',
'؛', '\;',
'٠|۰', '0',
'١|۱', '1',
'٢|۲', '2',
'٣|۳', '3',
'٤|۴', '4',
'٥|۵', '5',
'٦|۶', '6',
'٧|۷', '7',
'٨|۸', '8',
'٩|۹', '9',
'»|«', '"',
'ـ', '',
//insert i where applicable
'll', 'Ľ', //temporary conversion to avoid seeing ll as 2 letters
'rr', 'Ŕ', //temporary conversion to avoid seeing rr as 2 letters
'([bcçdfghjklĽmnpqrŔsştvwxz])([fjlĽmnrŔsşvwxyz])([fjlĽmnrŔsşvwxyz])([^aeêiîouûy])', '$1$2i$3$4', //e.g. grft -> grift
'([aeêiîouû])([bcçdfghjklĽmnpqrŔsştvwxz])([bcçdfghjklĽmnpqrŔsştvwxz])([bcçdfghjklĽmnpqrŔsştvwxz])\\b', '$1$2$3i$4', //e.g. cejnt -> cejnit
'([fjlĽrŔsşwyz])([fjlĽmnrŔsşvwxyz])([bcçdfghjklĽmnpqrŔsştvwxz])', '$1i$2$3', //e.g. wrd -> wird
'([bcçdghkmnpqtvx])([fjlĽmnrŔsşvwxyz])($|[^aeêiîouû])', '$1i$2$3', //e.g. prd -> pird
'([^aeêiîouû])([bcçdghkmnpqtvx])([fjlĽmnrŔsşvwxyz])($|[^aeêiîouû])', '$1$2i$3$4', //repeat the latter expression, in case skipped
'(^|[^aeêiîouy])([bcçdfghjklĽmnpqrŔsştvwxz])([bcçdfghjklĽmnpqrŔsştvwxz])($|[^aeêiîouû])', '$1$2i$3$4', //e.g. ktk -> kitk
'(^|[^a-zçşêîûĽŔ])([bcçdfghjklĽmnpqrŔsştvwxz])(\\s)', '$1$2i$3', //e.g. j -> ji
'Ľ', 'll', //revert the temporary conversion
'Ŕ', 'rr' //revert the temporary conversion
);
export const sOnsetI = new Array(
'([bcçdfghjklmnpqrsştvwxz])([wy][aeêiîouû])', '$1i$2', //e.g. dyar -> diyar
'(^|[^a-zêîûçş0-9\'’])([bcçdfghjklĽmnpqrŔsştvwxz])([bcçdfghjklĽmnpqrŔsştvwxz])', '$1$2i$3', //e.g. bra -> bira
'([bcçdfghjklmnpqrsştvwxz][bcçdfghjklĽmnpqrŔsştvwxz])([bcçdfghjklĽmnpqrŔsştvwxz])', '$1i$2' //e.g. aşkra -> aşkira
);
// Standardize Arabic scripts Array
export const sConvertStandardise = new Array(
'{1,}', '', //omit multiple successive zero-width non-joiners
'لاَ|لأ|لآ', 'ڵا',
'لً|لَ', 'ڵ',
'ض', 'چ',
'ث', 'پ',
'ظ', 'ڤ',
'ط', 'گ',
'ىَ|يَ|یَ', 'ێ',
'رِ', 'ڕ',
'ؤ|وَ', 'ۆ',
'ي|ى', 'ی',
'ذ', 'ژ',
'ك', 'ک',
// using unicode: 06D5 (Arabic letter Ae) for E
// using unicode: 06BE (Arabic letter He Doachashmee) for H
'ه', 'ھ',
'ه($|[^ء-يٱ-ە])', 'ە$1',
'ە', 'ە',
'ة', 'ە',
'ه', 'ھ', // rest of هs are H
'([ء-يٱ-ە])([^ء-يٱ-ە])', '$1$2'
);
export const sAliWeb = new Array(
'ص', 'ێ',
'أ', 'ڕ'
);
export const sConvertDilan = new Array(
'ص', 'ڵ',
'ث', 'ێ',
'ذ', 'ڕ'
);
// Latin type Array
export const sConvertLatin = new Array(
//typed, Hawar
'ee', 'ê',
'ii', 'î',
'uu', 'û',
'Ee|EE', 'Ê',
'Ii|II', 'Î',
'Uu|UU', 'Û',
'ch', 'ç',
'sh', 'ş',
'Ch|CH', 'Ç',
'Sh|SH', 'Ş'
);
export const sNumbers = new Array(
'0', '٠',
'1', '١',
'2', '٢',
'3', '٣',
'4', '٤',
'5', '٥',
'6', '٦',
'7', '٧',
'8', '٨',
'9', '٩'
);
//Arabic type Array
export const sKeyboardLayouts = new Array(
//English keyboard, Rojhellat keyboard, Bakur keyboard, Bashur Keyboard
//numbers row
'`', 'پ', '', 'ژ',
'1', '١', '١', '١',
'2', '٢', '٢', '٢',
'3', '٣', '٣', '٣',
'4', '٤', '٤', '٤',
'5', '٥', '٥', '٥',
'6', '٦', '٦', '٦',
'7', '٧', '٧', '٧',
'8', '٨', '٨', '٨',
'9', '٩', '٩', '٩',
'0', '٠', '٠', '٠',
'-', '-', '+', '-',
'=', '=', '\'', '=',
//numbers row shifted
'~', '~', '~', '~',
'!', '!', '!', '!',
'@', '@', '@', '@',
'#', '#', '#', '#',
'$', '$', '$', '$',
'%', '%', '%', '%',
'^', '÷', '^', '^',
'&', '×', '&', '&',
'*', '*', '*', '*',
'(', ')', ')', ')',
')', '(', '(', '(',
'_', 'ـ', '_', '_',
'+', '+', '+', '+',
//letters top row
'q', 'ڵ', 'ق', 'چ',
'w', 'ۆ', 'و', 'ص',
'e', 'ێ', 'ە', 'پ',
'r', 'ق', 'ر', 'ق',
't', 'ف', 'ت', 'ف',
'y', 'غ', 'ی', 'غ',
'u', 'ع', 'و', 'ع',
'i', 'ە', 'ئ', 'ھ',
'o', 'خ', 'ۆ', 'خ',
'p', 'ح', 'پ', 'ح',
'[', 'ج', 'وو', 'ج',
']', 'چ', 'ش', 'د',
'\\', 'ژ', 'چ', '\\',
//letters top row shifted
'Q', 'ض', '`', 'ض',
'W', 'ص', 'وو', '}',
'E', 'ث', 'ي', 'ث',
'R', 'ك', 'ڕ', '{',
'T', 'ي', 'ط', 'ڤ',
'Y', '_', 'ێ', 'إ',
'U', '', 'ء', '',
'I', 'ه', 'ع', '÷',
'O', ',', 'ؤ', '×',
'P', '', 'ث', '؛',
'{', ']', ']', '>',
'}', '[', '[', '<',
'|', '|', '|', '|',
//letters mid row
'a', 'ش', 'ا', 'ش',
's', 'س', 'س', 'س',
'd', 'ی', 'د', 'ی',
'f', 'ب', 'ف', 'ب',
'g', 'ل', 'گ', 'ل',
'h', 'ا', 'ھ', 'ا',
'j', 'ت', 'ژ', 'ت',
'k', 'ن', 'ک', 'ن',
'l', 'م', 'ل', 'م',
';', 'ک', 'ێ', 'ک',
'\'', 'گ', 'ی', 'گ',
//letters mid row shifted
'A', '»', 'آ', ']',
'S', '«', 'ش', '[',
'D', 'ێ', 'ذ', 'ێ',
'F', '', 'إ', '',
'G', 'ڵ', 'غ', 'ڵ',
'H', 'ئا', 'ە', 'أ',
'J', 'ء', 'أ', 'ـ',
'K', '}', 'ك', '،',
'L', '{', 'ڵ', '/',
':', ':', ':', 'ك',
'"', '؛', '"', 'ط',
//letters bottom row
'z', 'ڤ', 'ز', 'ئ',
'x', 'ھ', 'خ', 'ء',
'c', 'ز', 'ج', 'ۆ',
'v', 'ر', 'ڤ', 'ر',
'b', 'ڕ', 'ب', 'لا',
'n', 'د', 'ن', 'ی',
'm', 'ئ', 'م', 'ە',
',', 'و', '،', 'و',
'.', '.', '.', 'ز',
'/', '/', '-', '/',
//letters bottom row shifted
'Z', 'ظ', 'ض', '',
'X', 'ط', 'ص', 'وو',
'C', 'ژ', 'چ', 'ؤ',
'V', 'ڕ', 'ظ', 'ڕ',
'B', '', 'ى', 'ڵا',
'N', 'ذ', 'ة', 'آ',
'M', 'ة', 'ـ', 'ة',
'<', 'ۆ', '>', ',',
'>', '،', '<', '.',
'?', '؟', '؟', '؟'
);
/**
* TODO: add some docs
*
* @param {*} s
* @param {*} mode 1 is AR to LA
* TODO: add the rest
* @param {*} options
*/
export const convert = (
s,
mode = 1,
options = {
chkDilan: false,
chkAliWeb: false,
chkOnsetI: false,
chkCapitaliseSentence: false,
chkConvertNumbers: true, // yes!
chkTreatI: false,
dchkConnectSuffixes: true,
options: false,
}) => {
if(!s) return null;
switch (mode) {
case 1: //Arabic to Latin
// standardize Arabic before converting Arabic to Latin
if (options.chkDilan)
for (let i = 0; i < sConvertDilan.length; i += 2)
s = s.replace(new RegExp(sConvertDilan[i], 'g'), sConvertDilan[i + 1]);
if (options.chkAliWeb)
for (let i = 0; i < sAliWeb.length; i += 2)
s = s.replace(new RegExp(sAliWeb[i], 'g'), sAliWeb[i + 1]);
for (let i = 0; i < sConvertStandardise.length; i += 2)
s = s.replace(new RegExp(sConvertStandardise[i], 'g'), sConvertStandardise[i + 1]);
//main conversion
for (let i = 0; i < sConvertArabic2Latin.length; i += 2)
s = s.replace(new RegExp(sConvertArabic2Latin[i], 'gim'), sConvertArabic2Latin[i + 1]);
s = s.replace(new RegExp('ll', 'gim'), 'Ľ').replace(new RegExp('rr', 'gim'), 'Ŕ'); //temporary conversion
//add extra i's for Kurmanci texts
if (!options.chkOnsetI)
for (let i = 0; i < sOnsetI.length; i += 2)
s = s.replace(new RegExp(sOnsetI[i], 'gim'), sOnsetI[i + 1]); //e.g. bra -> bira
s = s.replace(new RegExp('Ľ', 'gim'), 'll').replace(new RegExp('Ŕ', 'gim'), 'rr'); //temporary conversion
//capitalise the first words of sentences
if (options.chkCapitaliseSentence)
s = s.replace(new RegExp('(^|\r\n|[\\.\\u003F!] [^a-zêîûçş0-9\'’]*)([a-zêîûçş])', 'gm'), function (m, p1, p2) {
for (let i = 0; i < sCapitalLetters.length; i += 2)
p2.replace(sCapitalLetters[i], sCapitalLetters[i + 1]);
return p1 + p2.toUpperCase();
});
break;
case 2: //Latin to Arabic
//main conversion
for (let i = 0; i < sConvertLatin2Arabic.length; i += 2)
s = s.replace(new RegExp(sConvertLatin2Arabic[i], 'gim'), sConvertLatin2Arabic[i + 1]);
//convert numbers
if (options.chkConvertNumbers)
for (let i = 0; i < sNumbers.length; i += 2)
s = s.replace(new RegExp(sNumbers[i], 'g'), sNumbers[i + 1]);
//convert ii and i based on the option selected
if (!options.chkTreatI)
s = s.replace(new RegExp('ii', 'gi'), 'ی').replace(new RegExp('i', 'gi'), '');
else
s = s.replace(new RegExp('i', 'gi'), 'ی');
//when user chooses to recoginse the dependent verb 'to be' can be separated (e.g. kurd in = kurdin)
if (options.dchkConnectSuffixes)
for (let i = 0; i < sSuffixes.length; i++)
s = s.replace(new RegExp(sSuffixes[i], 'gim'), '$1$2$3');
break;
case 3: //Standardise Arabic
//do Dilan conversions if requested
if (options.chkDilan)
for (let i = 0; i < sConvertDilan.length; i += 2)
s = s.replace(new RegExp(sConvertDilan[i], 'g'), sConvertDilan[i + 1]);
//Ali Web fonts
if (options.chkAliWeb)
for (let i = 0; i < sAliWeb.length; i += 2)
s = s.replace(new RegExp(sAliWeb[i], 'g'), sAliWeb[i + 1]);
//main conversion
for (let i = 0; i < sConvertStandardise.length; i += 2)
s = s.replace(new RegExp(sConvertStandardise[i], 'g'), sConvertStandardise[i + 1]);
//convert numbers if requested
if (options.chkConvertNumbers)
for (let i = 0; i < sNumbers.length; i += 2)
s = s.replace(new RegExp(sNumbers[i], 'g'), sNumbers[i + 1]);
break;
case 4: //Type Latin
for (let i = 0; i < sConvertLatin.length; i += 2)
s = s.replace(new RegExp(sConvertLatin[i], 'g'), sConvertLatin[i + 1]);
break;
}
return s;
}
/**
* Use it in your webviews
* @param {*} e
*/
export const getKeyStroke = (e) => {
//get the pressed key
var c = String.fromCharCode(window.event ? e.keyCode : e.which);
//which keyboard layout is selected
var keybrd;
if (document.getElementById('optRojhellat').checked)
keybrd = 1;
else if (document.getElementById('optBakur').checked)
keybrd = 2
else
keybrd = 3;
//find a substitute for the keyboard input based on the selected keyboard layout
for (let i = 0; i < sKeyboardLayouts.length; i += 4)
if (sKeyboardLayouts[i] == c) {
document.getElementById('txtArabic').value += sKeyboardLayouts[i + keybrd];
return false;
}
//there's no equivalent for the pressed key so don't change it
return true;
}