-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeySwap.php
More file actions
366 lines (309 loc) · 9.3 KB
/
KeySwap.php
File metadata and controls
366 lines (309 loc) · 9.3 KB
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
<?php
namespace ArabyPHP\KeySwap;
class KeySwap
{
/**
* @var array
*/
private $_transliteration = [];
/**
* @var array
*/
private $_arLogodd;
/**
* @var array
*/
private $_enLogodd;
/**
* @var array
*/
private $_arKeyboard;
/**
* @var array
*/
private $_enKeyboard;
/**
* @var array
*/
private $_frKeyboard;
/**
* Loads initialize values.
*
* @ignore
*/
public function __construct()
{
$xml = simplexml_load_file(__DIR__.'/data/arabizi.xml');
foreach ($xml->transliteration->item as $item) {
$index = $item['id'];
$this->_transliteration["$index"] = (string) $item;
}
$xml = simplexml_load_file(__DIR__.'/data/ArKeySwap.xml');
foreach ($xml->arabic->key as $key) {
$index = (int) $key['id'];
$this->_arKeyboard[$index] = (string) $key;
}
foreach ($xml->english->key as $key) {
$index = (int) $key['id'];
$this->_enKeyboard[$index] = (string) $key;
}
foreach ($xml->french->key as $key) {
$index = (int) $key['id'];
$this->_frKeyboard[$index] = (string) $key;
}
$this->_arLogodd = file(__DIR__.'/data/ar-logodd.php');
$this->_enLogodd = file(__DIR__.'/data/en-logodd.php');
}
/**
* Make conversion to swap that odd Arabic text by original English sentence
* you meant when you type on your keyboard (if keyboard language was
* incorrect).
*
* @param string $text Odd Arabic string
*
* @return string Normal English string
*
*/
public function swapAe($text)
{
$output = $this->_swapCore($text, 'ar', 'en');
return $output;
}
/**
* Make conversion to swap that odd English text by original Arabic sentence
* you meant when you type on your keyboard (if keyboard language was
* incorrect).
*
* @param string $text Odd English string
*
* @return string Normal Arabic string
*
*/
public function swapEa($text)
{
$output = $this->_swapCore($text, 'en', 'ar');
return $output;
}
/**
* Make conversion to swap that odd Arabic text by original French sentence
* you meant when you type on your keyboard (if keyboard language was
* incorrect).
*
* @param string $text Odd Arabic string
*
* @return string Normal French string
*
*/
public function swapAf($text)
{
$output = $this->_swapCore($text, 'ar', 'fr');
return $output;
}
/**
* Make conversion to swap that odd French text by original Arabic sentence
* you meant when you type on your keyboard (if keyboard language was
* incorrect).
*
* @param string $text Odd French string
*
* @return string Normal Arabic string
*
*/
public function swapFa($text)
{
$output = $this->_swapCore($text, 'fr', 'ar');
return $output;
}
/**
* Make conversion between different keyboard maps to swap odd text in
* one language by original meaningful text in another language that
* you meant when you type on your keyboard (if keyboard language was
* incorrect).
*
* @param string $text Odd string
* @param string $in Input language [ar|en|fr]
* @param string $out Output language [ar|en|fr]
*
* @return string Normal string
*
*/
private function _swapCore($text, $in, $out)
{
$output = '';
$text = stripslashes($text);
$max = mb_strlen($text);
switch ($in) {
case 'ar':
$inputMap = $this->_arKeyboard;
break;
case 'en':
$inputMap = $this->_enKeyboard;
break;
case 'fr':
$inputMap = $this->_frKeyboard;
break;
}
switch ($out) {
case 'ar':
$outputMap = $this->_arKeyboard;
break;
case 'en':
$outputMap = $this->_enKeyboard;
break;
case 'fr':
$outputMap = $this->_frKeyboard;
break;
}
for ($i = 0; $i < $max; $i++) {
$chr = mb_substr($text, $i, 1);
$key = array_search($chr, $inputMap);
if ($key === false) {
$output .= $chr;
} else {
$output .= $outputMap[$key];
}
}
return $output;
}
/**
* Calculate the log odd probability that inserted string from keyboard
* is in English language.
*
* @param string $str Inserted string from the keyboard
*
* @return float Calculated score for input string as English language
*
*/
protected function checkEn($str)
{
$lines = $this->_enLogodd;
$logodd = [];
$line = array_shift($lines);
$line = rtrim($line);
$second = preg_split("/\t/", $line);
$temp = array_shift($second);
foreach ($lines as $line) {
$line = rtrim($line);
$values = preg_split("/\t/", $line);
$first = array_shift($values);
for ($i = 0; $i < 28; $i++) {
$logodd["$first"]["{$second[$i]}"] = $values[$i];
}
}
$str = mb_strtolower($str);
$max = mb_strlen($str, 'UTF-8');
$rank = 0;
for ($i = 1; $i < $max; $i++) {
$first = mb_substr($str, $i - 1, 1, 'UTF-8');
$second = mb_substr($str, $i, 1, 'UTF-8');
if (isset($logodd["$first"]["$second"])) {
$rank += $logodd["$first"]["$second"];
} else {
$rank -= 10;
}
}
return $rank;
}
/**
* Calculate the log odd probability that inserted string from keyboard
* is in Arabic language.
*
* @param string $str Inserted string from the keyboard
*
* @return float Calculated score for input string as Arabic language
*
*/
protected function checkAr($str)
{
$lines = $this->_arLogodd;
$logodd = [];
$line = array_shift($lines);
$line = rtrim($line);
$second = preg_split("/\t/", $line);
$temp = array_shift($second);
foreach ($lines as $line) {
$line = rtrim($line);
$values = preg_split("/\t/", $line);
$first = array_shift($values);
for ($i = 0; $i < 37; $i++) {
$logodd["$first"]["{$second[$i]}"] = $values[$i];
}
}
$max = mb_strlen($str, 'UTF-8');
$rank = 0;
for ($i = 1; $i < $max; $i++) {
$first = mb_substr($str, $i - 1, 1, 'UTF-8');
$second = mb_substr($str, $i, 1, 'UTF-8');
if (isset($logodd["$first"]["$second"])) {
$rank += $logodd["$first"]["$second"];
} else {
$rank -= 10;
}
}
return $rank;
}
/**
* This method will automatically detect the language of content supplied
* in the input string. It will return the suggestion of correct inserted text.
* The accuracy of the automatic language detection increases with the amount
* of text entered.
*
* @param string $str Inserted string from the keyboard
*
* @return string Fixed string language and letter case to the better guess
*
*/
public function fixKeyboardLang($str)
{
preg_match_all("/([\x{0600}-\x{06FF}])/u", $str, $matches);
$arNum = count($matches[0]);
$nonArNum = mb_strlen(str_replace(' ', '', $str), 'UTF-8') - $arNum;
$capital = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$small = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
if ($arNum > $nonArNum) {
$arStr = $str;
$enStr = $this->swapAe($str);
$isAr = true;
} else {
$arStr = $this->swapEa($str);
$enStr = $str;
$strCaps = strtr($str, $capital, $small);
$arStrCaps = $this->swapEa($strCaps);
$isAr = false;
}
$enRank = $this->checkEn($enStr);
$arRank = $this->checkAr($arStr);
if ($arNum > $nonArNum) {
$arCapsRank = $arRank;
} else {
$arCapsRank = $this->checkAr($arStrCaps);
}
if ($enRank > $arRank && $enRank > $arCapsRank) {
if ($isAr) {
$fix = $enStr;
} else {
preg_match_all('/([A-Z])/u', $enStr, $matches);
$capsNum = count($matches[0]);
preg_match_all('/([a-z])/u', $enStr, $matches);
$nonCapsNum = count($matches[0]);
if ($capsNum > $nonCapsNum && $nonCapsNum > 0) {
$enCapsStr = strtr($enStr, $capital, $small);
$fix = $enCapsStr;
} else {
$fix = '';
}
}
} else {
if ($arCapsRank > $arRank) {
$arStr = $arStrCaps;
$arRank = $arCapsRank;
}
if (!$isAr) {
$fix = $arStr;
} else {
$fix = '';
}
}
return $fix;
}
}