-
Notifications
You must be signed in to change notification settings - Fork 5
/
js_code.js
110 lines (99 loc) · 3.1 KB
/
js_code.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
function autoCorrect(text){
var text = text.toLowerCase();
var text = text.replace(/gʻ|gʼ|g’|g'|g`|g‘/g,"ğ");
var text = text.replace(/oʻ|oʼ|o’|o'|o`|o‘/g,"ŏ");
var text = text.replace(/sh/g,"š");
var text = text.replace(/ch/g,"č");
var text = text.replace(/ʻʼ'|`|‘/g,"’");
return text;
};
function inverseCorrect(text){
var text = text.replace(/ğ/g,"g‘");
var text = text.replace(/ŏ/g,"o‘");
var text = text.replace(/š/g,"sh");
var text = text.replace(/č/g,"ch");
return text;
};
function createMap(text){
var text = text.replace(/[aoueiŏ]/g,"V");
var text = text.replace(/[bdfghjklmnpqrstvxyzğšč]/g,"C");
var parts = text.split("’");
var map = [];
parts.forEach(function(v,k){
var rem = v;
if (k != 0){
map.push("D");
}
l = v.length;
for (i=0;i<l;i++){
if (rem.length > 0) {
if (rem[0] == "V" && rem[1] != "C"){
map.push(1);
rem = rem.slice(1);
} else if (rem[0] == "V" && rem[1] == "C" && rem[2] == "V"){
map.push(1);
rem = rem.slice(1);
} else if (rem[0] == "V" && rem[1] == "C" && rem[2] != "V" && rem[3] != "C"){
map.push(2);
rem = rem.slice(2);
} else if (rem[0] == "V" && rem[1] == "C" && rem[2] == "C" && rem[3] != "V"){
map.push(3);
rem = rem.slice(3);
} else if (rem[0] == "C" && rem[1] == "V" && rem[2] != "C"){
map.push(2);
rem = rem.slice(2);
} else if (rem[0] == "C" && rem[1] == "V" && rem[2] == "C" && rem[3] == "V"){
map.push(2);
rem = rem.slice(2);
} else if (rem[0] == "C" && rem[1] == "V" && rem[2] == "C" && rem[3] == "C" && rem[4] == "V" || rem[0] == "C" && rem[1] == "V" && rem[2] == "C" && rem[3] != "C" && rem[3] != "V"){
map.push(3);
rem = rem.slice(3);
} else if (rem[0] == "C" && rem[1] == "V" && rem[2] == "C" && rem[3] == "C" && rem[4] != "V"){
map.push(4);
rem = rem.slice(4);
} else if (rem[0] == "C" && rem[1] == "C" && rem[2] == "V" && rem[3] != "C"){
map.push(3);
rem = rem.slice(3);
} else if (rem[0] == "C" && rem[1] == "C" && rem[2] == "V" && rem[3] == "C" && rem[4] == "V"){
map.push(3);
rem = rem.slice(3);
} else if (rem[0] == "C" && rem[1] == "C" && rem[2] == "V" && rem[3] == "C" && rem[4] != "V" && rem[5] != "C"){
map.push(4);
rem = rem.slice(4);
} else if (rem[0] == "C" && rem[1] == "C" && rem[2] == "V" && rem[3] == "C" && rem[4] == "C" && rem[5] != "V"){
map.push(5);
rem = rem.slice(5);
}
} else {
break;
}
}
});
return map;
}
function tokenize(field, word) {
var rgx = /^[abdefghijklmnopqrstuvxyzŏğšč’]+$/;
var word = autoCorrect(word).trim();
var map = createMap(word);
var rem = word;
var r = "";
map.forEach(function(v,k){
if (v == "D") {
r += "’";
rem = rem.slice(1);
} else {
var sl = rem.slice(0,v);
rem = rem.slice(v);
if (k == 0) {
r += sl;
} else {
r += "-"+sl;
}
}
});
if (!rgx.test(word)) {
r = "Kiritilgan so'z noto'g'ri";
}
r = inverseCorrect(r);
field.value = r;
}