I18N_Arabic

# Source for file KeySwap.php

Documentation is available at KeySwap.php

1. <?php
2. /**
3. * ----------------------------------------------------------------------
4. *
5. * Copyright (c) 2006-2016 Khaled Al-Sham'aa
6. *
7. * http://www.ar-php.org
8. *
9. * PHP Version 5
10. *
11. * ----------------------------------------------------------------------
12. *
13. * LICENSE
14. *
15. * This program is open source product; you can redistribute it and/or
16. * modify it under the terms of the GNU Lesser General Public License (LGPL)
17. * as published by the Free Software Foundation; either version 3
18. * of the License, or (at your option) any later version.
19. *
20. * This program is distributed in the hope that it will be useful,
21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23. * GNU Lesser General Public License for more details.
24. *
25. * You should have received a copy of the GNU Lesser General Public License
26. * along with this program. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
27. *
28. * ----------------------------------------------------------------------
29. *
30. * Class Name: Arabic Keyboard Swapping Language
31. *
32. * Filename: KeySwap.php
33. *
34. * Original Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
35. *
36. * Purpose: Convert keyboard language programmatically (English - Arabic)
37. *
38. * ----------------------------------------------------------------------
39. *
40. * Arabic Keyboard Swapping Language
41. *
42. * PHP class to convert keyboard language between English and Arabic
43. * programmatically. This function can be helpful in dual language forms when
44. * users miss change keyboard language while they are entering data.
45. *
46. * If you wrote an Arabic sentence while your keyboard stays in English mode by
47. * mistake, you will get a non-sense English text on your PC screen. In that case
48. * you can use this class to make a kind of magic conversion to swap that odd text
49. * by original Arabic sentence you meant when you type on your keyboard.
50. *
51. * Please note that magic conversion in the opposite direction (if you type English
52. * sentences while your keyboard stays in Arabic mode) is also available in this
53. * class, but it is not reliable as much as previous case because in Arabic keyboard
54. * we have some keys provide a short-cut to type two chars in one click (those keys
55. * include: b, B, G and T).
56. *
57. * Well, we try in this class to come over this issue by suppose that user used
58. * optimum way by using short-cut keys when available instead of assemble chars
59. * using stand alone keys, but if (s)he does not then you may have some typo chars
60. * in converted text.
61. *
62. * Example:
63. * <code>
64. * include('./I18N/Arabic.php');
65. * $obj = new I18N_Arabic('KeySwap');
66. *
67. * $str = "Hpf lk hgkhs hglj'vtdkK Hpf hg`dk dldg,k f;gdjil Ygn ,p]hkdm ...";
68. *
69. * echo "<p><u><i>Before:</i></u><br />$str<br /><br />";
70. *
71. * $text = $obj->swapEa($str);
72. *
73. * echo "<u><i>After:</i></u><br />$text<br /><br />";
74. * </code>
75. *
76. * @category I18N
77. * @package I18N_Arabic
78. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
79. * @copyright 2006-2016 Khaled Al-Sham'aa
80. *
81. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
82. * @link http://www.ar-php.org
83. */
85. /**
86. * This PHP class convert keyboard language programmatically (English - Arabic)
87. *
88. * @category I18N
89. * @package I18N_Arabic
90. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
91. * @copyright 2006-2016 Khaled Al-Sham'aa
92. *
93. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
94. * @link http://www.ar-php.org
95. */
96. class I18N_Arabic_KeySwap
97. {
98. private $_transliteration = array();
99. private $_arLogodd;
100. private $_enLogodd;
102. private $_arKeyboard;
103. private $_enKeyboard;
104. private $_frKeyboard;
106. /**
107. * Loads initialize values
108. *
109. * @ignore
110. */
111. public function __construct()
112. {
113. $xml = simplexml_load_file(dirname(__FILE__).'/data/charset/arabizi.xml');
115. foreach ($xml->transliteration->item as $item) {
116. $index = $item['id'];
117. $this->_transliteration["$index"] = (string)$item;
118. }
120. $xml = simplexml_load_file(dirname(__FILE__).'/data/ArKeySwap.xml');
122. foreach ($xml->arabic->key as $key) {
123. $index = (int)$key['id'];
124. $this->_arKeyboard[$index] = (string)$key;
125. }
127. foreach ($xml->english->key as $key) {
128. $index = (int)$key['id'];
129. $this->_enKeyboard[$index] = (string)$key;
130. }
132. foreach ($xml->french->key as $key) {
133. $index = (int)$key['id'];
134. $this->_frKeyboard[$index] = (string)$key;
135. }
137. $this->_arLogodd = file(dirname(__FILE__).'/data/ar-logodd.php');
138. $this->_enLogodd = file(dirname(__FILE__).'/data/en-logodd.php');
139. }
141. /**
142. * Make conversion to swap that odd Arabic text by original English sentence
143. * you meant when you type on your keyboard (if keyboard language was
144. * incorrect)
145. *
146. * @param string $text Odd Arabic string
147. *
148. * @return string Normal English string
149. * @author Khaled Al-Sham'aa
150. */
151. public function swapAe($text)
152. {
153. $output = $this->_swapCore($text, 'ar', 'en');
155. return $output;
156. }
158. /**
159. * Make conversion to swap that odd English text by original Arabic sentence
160. * you meant when you type on your keyboard (if keyboard language was
161. * incorrect)
162. *
163. * @param string $text Odd English string
164. *
165. * @return string Normal Arabic string
166. * @author Khaled Al-Sham'aa
167. */
168. public function swapEa($text)
169. {
170. $output = $this->_swapCore($text, 'en', 'ar');
172. return $output;
173. }
175. /**
176. * Make conversion to swap that odd Arabic text by original French sentence
177. * you meant when you type on your keyboard (if keyboard language was
178. * incorrect)
179. *
180. * @param string $text Odd Arabic string
181. *
182. * @return string Normal French string
183. * @author Khaled Al-Sham'aa
184. */
185. public function swapAf($text)
186. {
187. $output = $this->_swapCore($text, 'ar', 'fr');
189. return $output;
190. }
192. /**
193. * Make conversion to swap that odd French text by original Arabic sentence
194. * you meant when you type on your keyboard (if keyboard language was
195. * incorrect)
196. *
197. * @param string $text Odd French string
198. *
199. * @return string Normal Arabic string
200. * @author Khaled Al-Sham'aa
201. */
202. public function swapFa($text)
203. {
204. $output = $this->_swapCore($text, 'fr', 'ar');
206. return $output;
207. }
209. /**
210. * Make conversion between different keyboard maps to swap odd text in
211. * one language by original meaningful text in another language that
212. * you meant when you type on your keyboard (if keyboard language was
213. * incorrect)
214. *
215. * @param string $text Odd string
216. * @param string $in Input language [ar|en|fr]
217. * @param string $out Output language [ar|en|fr]
218. *
219. * @return string Normal string
220. * @author Khaled Al-Sham'aa
221. */
222. private function _swapCore($text, $in, $out)
223. {
224. $output = '';
225. $text = stripslashes($text);
226. $max = mb_strlen($text);
228. switch ($in) {
229. case 'ar':
230. $inputMap = $this->_arKeyboard;
231. break;
232. case 'en':
233. $inputMap = $this->_enKeyboard;
234. break;
235. case 'fr':
236. $inputMap = $this->_frKeyboard;
237. break;
238. }
240. switch ($out) {
241. case 'ar':
242. $outputMap = $this->_arKeyboard;
243. break;
244. case 'en':
245. $outputMap = $this->_enKeyboard;
246. break;
247. case 'fr':
248. $outputMap = $this->_frKeyboard;
249. break;
250. }
252. for ($i=0; $i<$max; $i++) {
253. $chr = mb_substr($text, $i, 1);
254. $key = array_search($chr, $inputMap);
256. if ($key === false) {
257. $output .= $chr;
258. } else {
259. $output .= $outputMap[$key];
260. }
261. }
263. return $output;
264. }
266. /**
267. * Calculate the log odd probability that inserted string from keyboard
268. * is in English language
269. *
270. * @param string $str Inserted string from the keyboard
271. *
272. * @return float Calculated score for input string as English language
273. * @author Khaled Al-Sham'aa
274. */
275. protected function checkEn($str)
276. {
277. $lines = $this->_enLogodd;
278. $logodd = array();
280. $line = array_shift($lines);
281. $line = rtrim($line);
282. $second = preg_split("/\t/", $line);
283. $temp = array_shift($second);
285. foreach ($lines as $line) {
286. $line = rtrim($line);
287. $values = preg_split("/\t/", $line);
288. $first = array_shift($values);
290. for ($i=0; $i<28; $i++) {
291. $logodd["$first"]["{$second[$i]}"] = $values[$i];
292. }
293. }
295. $str = mb_strtolower($str);
296. $max = mb_strlen($str, 'UTF-8');
297. $rank = 0;
299. for ($i=1; $i<$max; $i++) {
300. $first = mb_substr($str, $i-1, 1, 'UTF-8');
301. $second = mb_substr($str, $i, 1, 'UTF-8');
303. if (isset($logodd["$first"]["$second"])) {
304. $rank += $logodd["$first"]["$second"];
305. } else {
306. $rank -= 10;
307. }
308. }
310. return $rank;
311. }
313. /**
314. * Calculate the log odd probability that inserted string from keyboard
315. * is in Arabic language
316. *
317. * @param string $str Inserted string from the keyboard
318. *
319. * @return float Calculated score for input string as Arabic language
320. * @author Khaled Al-Sham'aa
321. */
322. protected function checkAr($str)
323. {
324. $lines = $this->_arLogodd;
325. $logodd = array();
327. $line = array_shift($lines);
328. $line = rtrim($line);
329. $second = preg_split("/\t/", $line);
330. $temp = array_shift($second);
332. foreach ($lines as $line) {
333. $line = rtrim($line);
334. $values = preg_split("/\t/", $line);
335. $first = array_shift($values);
337. for ($i=0; $i<37; $i++) {
338. $logodd["$first"]["{$second[$i]}"] = $values[$i];
339. }
340. }
342. $max = mb_strlen($str, 'UTF-8');
343. $rank = 0;
345. for ($i=1; $i<$max; $i++) {
346. $first = mb_substr($str, $i-1, 1, 'UTF-8');
347. $second = mb_substr($str, $i, 1, 'UTF-8');
349. if (isset($logodd["$first"]["$second"])) {
350. $rank += $logodd["$first"]["$second"];
351. } else {
352. $rank -= 10;
353. }
354. }
356. return $rank;
357. }
359. /**
360. * This method will automatically detect the language of content supplied
361. * in the input string. It will return the suggestion of correct inserted text.
362. * The accuracy of the automatic language detection increases with the amount
363. * of text entered.
364. *
365. * @param string $str Inserted string from the keyboard
366. *
367. * @return string Fixed string language and letter case to the better guess
368. * @author Khaled Al-Sham'aa
369. */
370. public function fixKeyboardLang($str)
371. {
372. preg_match_all("/([\x{0600}-\x{06FF}])/u", $str, $matches);
374. $arNum = count($matches[0]);
375. $nonArNum = mb_strlen(str_replace(' ', '', $str), 'UTF-8') - $arNum;
377. $capital = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
378. $small = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
380. if ($arNum > $nonArNum) {
381. $arStr = $str;
382. $enStr = $this->swapAe($str);
383. $isAr = true;
384. } else {
385. $arStr = $this->swapEa($str);
386. $enStr = $str;
388. $strCaps = strtr($str, $capital, $small);
389. $arStrCaps = $this->swapEa($strCaps);
391. $isAr = false;
392. }
394. $enRank = $this->checkEn($enStr);
395. $arRank = $this->checkAr($arStr);
397. if ($arNum > $nonArNum) {
398. $arCapsRank = $arRank;
399. } else {
400. $arCapsRank = $this->checkAr($arStrCaps);
401. }
403. if ($enRank > $arRank && $enRank > $arCapsRank) {
404. if ($isAr) {
405. $fix = $enStr;
406. } else {
407. preg_match_all("/([A-Z])/u", $enStr, $matches);
408. $capsNum = count($matches[0]);
410. preg_match_all("/([a-z])/u", $enStr, $matches);
411. $nonCapsNum = count($matches[0]);
413. if ($capsNum > $nonCapsNum && $nonCapsNum > 0) {
414. $enCapsStr = strtr($enStr, $capital, $small);
415. $fix = $enCapsStr;
416. } else {
417. $fix = '';
418. }
419. }
420. } else {
421. if ($arCapsRank > $arRank) {
422. $arStr = $arStrCaps;
423. $arRank = $arCapsRank;
424. }
426. if (!$isAr) {
427. $fix = $arStr;
428. } else {
429. $fix = '';
430. }
431. }
433. return $fix;
434. }
435. }

Documentation generated on Fri, 01 Jan 2016 10:26:06 +0200 by phpDocumentor 1.4.0