-
Notifications
You must be signed in to change notification settings - Fork 1
/
almor.js.html
418 lines (378 loc) · 13.6 KB
/
almor.js.html
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
<!-- almor.js.html -->
<script>
// const level_proper = 0;
// const level_1 = 1;
// const level_2 = 2;
// const level_3 = 3;
// const level_4 = 4;
// const level_5 = 5;
// const level_unknown = 6;
function handle_trivial_case(wordorigin, level){
let default_obj = {}
default_obj.word = wordorigin;
default_obj.level = level;
default_obj.prob = -999999;
default_obj.pos = "";
default_obj.lp = "";
default_obj.lemma = "";
return default_obj
}
function analyzeSolutions(word){
let wordorigin = word;
word = cleanArabicOrOriginal(word);
word = toBuckwalter(word);
let result = {}
let output = {};
let solutions = [];
let options = []
let cnt = 0;
// Handle deterministic (trivial) cases
if(wordorigin.match(/^\s*$/) ){
// Word is just whitespace
return [word];
}
if(wordorigin.match(/#٠#/) ){
//Forced level propernoun
let default_obj = handle_trivial_case(wordorigin, level_proper)
options.push(default_obj);
result.best_option = {...default_obj}
}
else if(wordorigin.match(/#١#/) ){
//Forced level 1
let default_obj = handle_trivial_case(wordorigin, level_1)
options.push(default_obj);
result.best_option = {...default_obj}
}
else if(wordorigin.match(/#٢#/) ){
//Forced level 2
let default_obj = handle_trivial_case(wordorigin, level_2)
options.push(default_obj);
result.best_option = {...default_obj}
}
else if(wordorigin.match(/#٣#/) ){
let default_obj = handle_trivial_case(wordorigin, level_3)
options.push(default_obj);
result.best_option = {...default_obj}
}
else if(wordorigin.match(/#٤#/) ){
let default_obj = handle_trivial_case(wordorigin, level_4)
options.push(default_obj);
result.best_option = {...default_obj}
}
else if(wordorigin.match(/#٥#/) ){
let default_obj = handle_trivial_case(wordorigin, level_5)
options.push(default_obj);
result.best_option = {...default_obj}
} else if(wordorigin.match(/#٦#/) ){
let default_obj = handle_trivial_case(wordorigin, level_unknown)
options.push(default_obj);
result.best_option = {...default_obj}
}
//if it's a number, level 1
else if (wordorigin.match(/\d+/) || wordorigin.match(/[\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669\u0660]/)){
let default_obj = handle_trivial_case(wordorigin, level_1)
options.push(default_obj);
result.best_option = {...default_obj}
}
//if it's a punctuation, level 1
else if(isPunct(wordorigin)){
let default_obj = handle_trivial_case(wordorigin, level_1)
options.push(default_obj);
result.best_option = {...default_obj}
}
//if it's a foreig word, unknown
else if (word.match(/[BCceGIJLMOPQRUVWX]/) || word.match(/^(\s|[aiuo\~\`FKN])*$/)){
let default_obj = handle_trivial_case(wordorigin, level_unknown)
options.push(default_obj);
result.best_option = {...default_obj}
}
let matchword = word.replace(/(\s|[aiuo\~\`FKN\_])/g, "");
let unvocword = matchword;
matchword = matchword.replace(/[>|<\{]/g, "A")
matchword = matchword.replace(/Y/g, "y")
matchword = matchword.replace(/p/g, "h")
if(matchword == ""){
matchword = word;
}
let max_prob = -1000000;
let segmented = segmentword(matchword);
for(let segmentation of segmented){
let prefix = segmentation[0];
let stem = segmentation[1];
let suffix = segmentation[2];
let stem_value_list = hash_stem[stem];
//Ignore words that are not stems
if(stem_value_list){
// for every possible stem value
for (let stem_value of stem_value_list) {
let raw_lex = stem_value["l"].split("+")[0];
let cat_b = stem_value["c"];
let prefix_value_list = hash_prefix[prefix];
//for every possible prefix value
for (let prefix_value of prefix_value_list) {
let cat_a = prefix_value["c"]
//If the prefix matches the stem
if( table.table_AB[cat_a + "!" + cat_b] ){
let suffix_value_list = hash_suffix[suffix];
//look at every possible suffix value
for (let suffix_value of suffix_value_list) {
let cat_c = suffix_value["c"];
//If the suffix matches the stem and the suffix matches the prefix
if( table.table_BC[cat_b + "!" + cat_c] && table.table_AC[cat_a + "!" + cat_c]){
//Get info about stem analysis
let analysis = {}
analysis.pos = stem_value["l"].split("+")[1]
analysis.lemma = stem_value["l"].split("+")[0]
analysis.lp = stem_value["l"]
analysis.level = getLevel(analysis.lp);
analysis.word = wordorigin;
analysis.gloss = getGloss(analysis.lp)
let current_prob = -999999;
//check if the word exists in the lexpos model
if( lexposModel.hasOwnProperty(analysis.lp) ) {
current_prob = lexposModel[analysis.lp]
} else if(analysis.pos.match(/noun\_prop/) != null){
current_prob = -200
}
analysis.prob = current_prob;
options.push(analysis);
if(current_prob > max_prob){
max_prob = current_prob;
output = analysis;
}
}
}
}
}
}
}
}
if(options.length === 0){
let analysis = {}
analysis.level = 6;
analysis.word = wordorigin;
analysis.prob = -999999;
analysis.pos = "";
analysis.lp = "";
analysis.lemma = "";
analysis.gloss="";
options.push(analysis);
output = analysis;
}
let unique_lps = []
let unique_sols = []
for (let option of options){
if (!unique_lps.includes(option.lp) && option.lp !== ""){
unique_lps.push(option.lp)
unique_sols.push(option)
}
}
// Sort
unique_sols = unique_sols.sort((a, b)=>(b.prob > a.prob) ? 1 : -1)
if(!result.hasOwnProperty("best_option")){
result.best_option = output
} else {
result.best_option.actual_level = output.level;
}
result.options = unique_sols
return result
}
//Find all possible suffixes / stems/ prefixes of word
var segmentword = function(str){
var segmented = [];
var prefix_len = 0;
var suffix_len = 0;
var stem_len = 0;
var prefix = "";
var suffix = "";
var stem = "";
var str_len = str.length;
var max_prefix_len = 4;
var max_suffix_len = 6;
while ( prefix_len <= max_prefix_len ) {
prefix = str.substr(0, prefix_len);
if ( hash_prefix.hasOwnProperty(prefix)) {
stem_len = (str_len - prefix_len);
suffix_len = 0;
while ((stem_len >= 1) && (suffix_len <= max_suffix_len)) {
stem = str.substr(prefix_len, stem_len);
suffix = str.substr((prefix_len + stem_len), suffix_len);
if (hash_suffix.hasOwnProperty(suffix)) {
segmented.push([prefix , stem , suffix]);
}
stem_len--;
suffix_len++;
}
};
prefix_len++;
}
return segmented;
}
function cleanLex(lex) {
if (typeof lex != 'undefined'){
//remove underscore segment at the end
var nouLex = lex.split("_")[0];
var ndLex = nouLex.split("-")[0];
return ndLex;
}
else{
return lex;
}
}
function getLevel(result) {
let level = lexpos_level[result];
if (level == null){ level=level_unknown };
return level;
}
function getGloss(lemmapos){
let gloss = lemma_pos_gloss[lemmapos];
if(gloss == null) gloss = "";
return gloss
}
function replaceAll(find, replace, str) {
replaced = str.replace(new RegExp(find, 'g'), replace);
return replaced;
}
function toUnicode(text){
//The function takes a text and returns a Unicode representation of it
text = replaceAll( "'" , "\u0621", text);
text = replaceAll( "\\|" , "\u0622", text);
text = replaceAll( ">" , "\u0623", text );
text = replaceAll( "&" , "\u0624", text);
text = replaceAll( "<" , "\u0625", text);
text = replaceAll( "\\}" , "\u0626", text);
text = replaceAll( "A" , "\u0627", text);
text = replaceAll( "b" , "\u0628", text);
text = replaceAll( "p" , "\u0629", text);
text = replaceAll( "t" , "\u062A", text);
text = replaceAll( "v" , "\u062B", text);
text = replaceAll( "j" , "\u062C", text);
text = replaceAll( "H" , "\u062D", text);
text = replaceAll( "x" , "\u062E", text);
text = replaceAll( "d" , "\u062F", text);
text = replaceAll( "\\*" , "\u0630", text);
text = replaceAll( "r" , "\u0631", text);
text = replaceAll( "z" , "\u0632", text);
text = replaceAll( "s" , "\u0633", text);
text = replaceAll( "\\$" , "\u0634", text);
text = replaceAll( "S" , "\u0635", text);
text = replaceAll( "D" , "\u0636", text);
text = replaceAll( "T" , "\u0637", text);
text = replaceAll( "Z" , "\u0638", text);
text = replaceAll( "E" , "\u0639", text);
text = replaceAll( "g" , "\u063A", text);
text = replaceAll( "_" , "\u0640", text);
text = replaceAll( "f" , "\u0641", text);
text = replaceAll( "q" , "\u0642", text);
text = replaceAll( "k" , "\u0643", text);
text = replaceAll( "l" , "\u0644", text);
text = replaceAll( "m" , "\u0645", text);
text = replaceAll( "n" , "\u0646", text);
text = replaceAll( "h" , "\u0647", text);
text = replaceAll( "w" , "\u0648", text);
text = replaceAll( "Y" , "\u0649", text);
text = replaceAll( "y" , "\u064A", text);
text = replaceAll( "F" , "\u064B", text);
text = replaceAll( "N" , "\u064C", text);
text = replaceAll( "K" , "\u064D", text);
text = replaceAll( "a" , "\u064E", text);
text = replaceAll( "u" , "\u064F", text);
text = replaceAll( "i" , "\u0650", text);
text = replaceAll( "~" , "\u0651", text);
text = replaceAll( "o" , "\u0652", text);
text = replaceAll( "`" , "\u0670", text);
text = replaceAll( "\\{" , "\u0671", text);
return text;
}
function toBuckwalter(text){
text = replaceAll("\u0621", "'", text);
text = replaceAll("\u0622", "|", text);
text = replaceAll("\u0623", ">", text);
text = replaceAll("\u0624", "&", text);
text = replaceAll("\u0625", "<", text);
text = replaceAll("\u0626", "}", text);
text = replaceAll("\u0627", "A", text);
text = replaceAll("\u0628","b", text); // baa'
text = replaceAll("\u0629","p", text); // taa' marbuuTa
text = replaceAll("\u062A", "t", text); // taa'
text = replaceAll("\u062B","v", text); // thaa'
text = replaceAll("\u062C","j", text); // jiim
text = replaceAll("\u062D","H", text); // Haa'
text = replaceAll("\u062E", "x", text);// khaa'
text = replaceAll("\u062F", "d", text);// daal
text = replaceAll("\u0630", "*", text);// dhaal
text = replaceAll("\u0631", "r", text);// raa'
text = replaceAll("\u0632", "z", text);// zaay
text = replaceAll("\u0633","s", text); // siin
text = replaceAll("\u0634", "$", text);// shiin
text = replaceAll("\u0635","S", text); // Saad
text = replaceAll("\u0636", "D", text);// Daad
text = replaceAll("\u0637","T", text); // Taa'
text = replaceAll("\u0638", "Z", text);// Zaa' (DHaa')
text = replaceAll("\u0639", "E", text);// cayn
text = replaceAll("\u063A", "g", text);// ghayn
text = replaceAll("\u0640","_", text); // taTwiil
text = replaceAll("\u0641","f", text); // faa'
text = replaceAll("\u0642","q", text); // qaaf
text = replaceAll("\u0643", "k", text);// kaaf
text = replaceAll("\u0644", "l", text);// laam
text = replaceAll("\u0645","m", text); // miim
text = replaceAll("\u0646","n", text); // nuun
text = replaceAll("\u0647", "h", text);// haa'
text = replaceAll("\u0648", "w", text);// waaw
text = replaceAll("\u0649", "Y", text);// 'alif maqSuura
text = replaceAll("\u064A", "y", text);// yaa'
text = replaceAll("\u064B", "F", text);// fatHatayn
text = replaceAll("\u064C", "N", text); // Dammatayn
text = replaceAll("\u064D", "K", text);// kasratayn
text = replaceAll("\u064E", "a", text);// fatHa
text = replaceAll("\u064F","u", text); // Damma
text = replaceAll("\u0650", "i", text);// kasra
text = replaceAll("\u0651", "~", text);// shaddah
text = replaceAll("\u0652", "o", text);// sukuun
text = replaceAll("\u0670","`", text); // dagger 'alif
text = replaceAll("\u0671", "{", text);// waSla
return text;
}
function stripDiac(text){
text = replaceAll("\u0640", "", text);// kashida tatweel , not a diactritic but useless
text = replaceAll("\u064B", "", text);// fatHatayn
text = replaceAll("\u064C", "", text); // Dammatayn
text = replaceAll("\u064D", "", text);// kasratayn
text = replaceAll("\u064E", "", text);// fatHa
text = replaceAll("\u064F","", text); // Damma
text = replaceAll("\u0650", "", text);// kasra
text = replaceAll("\u0651", "", text);// shaddah
text = replaceAll("\u0652", "", text);// sukuun
text = replaceAll("\u0670","", text); // dagger 'alif
text = replaceAll("\u0671", "", text);// waSla
return text;
}
function isPunct(str) {
return str.match(/^\s*[\-\=\«\»\"\_\–\:\#\@\!\?\؟\،\^\/\(\)\[\]\%\;\\\+\.\,]+\s*$/);
}
function stripPunct(str){
var newstr = "";
for(var x in str){
if(!isPunct(str[x])){
newstr += str[x];
}
}
return newstr;
}
function stripNumbers(str){
let newStr = "";
for(var x in str){
if(!str[x].match(/[\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669\u0660]/)){
newStr += str[x];
}
}
return newStr;
}
function cleanArabicOrOriginal(str){
var nodiac = stripPunct(stripDiac(str.trim()));
var arbCore = nodiac.replace(/[^\u0621-\u063a,\u0641-\u064a]/gi, '');
return arbCore;
}
//window.analyzeSolutions = analyzeSolutions;
</script>