-
Notifications
You must be signed in to change notification settings - Fork 6
/
KeySwap.php
executable file
·435 lines (383 loc) · 13.4 KB
/
KeySwap.php
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
<?php
/**
* ----------------------------------------------------------------------
*
* Copyright (c) 2006-2016 Khaled Al-Sham'aa
*
* http://www.ar-php.org
*
* PHP Version 5
*
* ----------------------------------------------------------------------
*
* LICENSE
*
* This program is open source product; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License (LGPL)
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
*
* ----------------------------------------------------------------------
*
* Class Name: Arabic Keyboard Swapping Language
*
* Filename: KeySwap.php
*
* Original Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
*
* Purpose: Convert keyboard language programmatically (English - Arabic)
*
* ----------------------------------------------------------------------
*
* Arabic Keyboard Swapping Language
*
* PHP class to convert keyboard language between English and Arabic
* programmatically. This function can be helpful in dual language forms when
* users miss change keyboard language while they are entering data.
*
* If you wrote an Arabic sentence while your keyboard stays in English mode by
* mistake, you will get a non-sense English text on your PC screen. In that case
* you can use this class to make a kind of magic conversion to swap that odd text
* by original Arabic sentence you meant when you type on your keyboard.
*
* Please note that magic conversion in the opposite direction (if you type English
* sentences while your keyboard stays in Arabic mode) is also available in this
* class, but it is not reliable as much as previous case because in Arabic keyboard
* we have some keys provide a short-cut to type two chars in one click (those keys
* include: b, B, G and T).
*
* Well, we try in this class to come over this issue by suppose that user used
* optimum way by using short-cut keys when available instead of assemble chars
* using stand alone keys, but if (s)he does not then you may have some typo chars
* in converted text.
*
* Example:
* <code>
* include('./I18N/Arabic.php');
* $obj = new I18N_Arabic('KeySwap');
*
* $str = "Hpf lk hgkhs hglj'vtdkK Hpf hg`dk dldg,k f;gdjil Ygn ,p]hkdm ...";
*
* echo "<p><u><i>Before:</i></u><br />$str<br /><br />";
*
* $text = $obj->swapEa($str);
*
* echo "<u><i>After:</i></u><br />$text<br /><br />";
* </code>
*
* @category I18N
* @package I18N_Arabic
* @author Khaled Al-Sham'aa <khaled@ar-php.org>
* @copyright 2006-2016 Khaled Al-Sham'aa
*
* @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
* @link http://www.ar-php.org
*/
/**
* This PHP class convert keyboard language programmatically (English - Arabic)
*
* @category I18N
* @package I18N_Arabic
* @author Khaled Al-Sham'aa <khaled@ar-php.org>
* @copyright 2006-2016 Khaled Al-Sham'aa
*
* @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
* @link http://www.ar-php.org
*/
class I18N_Arabic_KeySwap
{
private $_transliteration = array();
private $_arLogodd;
private $_enLogodd;
private $_arKeyboard;
private $_enKeyboard;
private $_frKeyboard;
/**
* Loads initialize values
*
* @ignore
*/
public function __construct()
{
$xml = simplexml_load_file(dirname(__FILE__).'/data/charset/arabizi.xml');
foreach ($xml->transliteration->item as $item) {
$index = $item['id'];
$this->_transliteration["$index"] = (string)$item;
}
$xml = simplexml_load_file(dirname(__FILE__).'/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(dirname(__FILE__).'/data/ar-logodd.php');
$this->_enLogodd = file(dirname(__FILE__).'/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
* @author Khaled Al-Sham'aa
*/
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
* @author Khaled Al-Sham'aa
*/
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
* @author Khaled Al-Sham'aa
*/
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
* @author Khaled Al-Sham'aa
*/
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
* @author Khaled Al-Sham'aa
*/
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
* @author Khaled Al-Sham'aa
*/
protected function checkEn($str)
{
$lines = $this->_enLogodd;
$logodd = array();
$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
* @author Khaled Al-Sham'aa
*/
protected function checkAr($str)
{
$lines = $this->_arLogodd;
$logodd = array();
$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
* @author Khaled Al-Sham'aa
*/
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;
}
}