I18N_Arabic

# Source for file Transliteration.php

Documentation is available at Transliteration.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: English-Arabic Transliteration
31. *
32. * Filename: Transliteration.php
33. *
34. * Original Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
35. *
36. * Purpose: Transliterate English words into Arabic by render them
37. * in the orthography of the Arabic language and vise versa
38. *
39. * ----------------------------------------------------------------------
40. *
41. * English-Arabic Transliteration
42. *
43. * PHP class transliterate English words into Arabic by render them in the
44. * orthography of the Arabic language and vise versa.
45. *
46. * Out of vocabulary (OOV) words are a common source of errors in cross language
47. * information retrieval. Bilingual dictionaries are often limited in their coverage
48. * of named- entities, numbers, technical terms and acronyms. There is a need to
49. * generate translations for these "on-the-fly" or at query time.
50. *
51. * A significant proportion of OOV words are named entities and technical terms.
52. * Typical analyses find around 50% of OOV words to be named entities. Yet these
53. * can be the most important words in the queries. Cross language retrieval
54. * performance (average precision) reduced more than 50% when named entities in the
55. * queries were not translated.
56. *
57. * When the query language and the document language share the same alphabet it may
58. * be sufficient to use the OOV word as its own translation. However, when the two
59. * languages have different alphabets, the query term must somehow be rendered in
60. * the orthography of the other language. The process of converting a word from one
61. * orthography into another is called transliteration.
62. *
63. * Foreign words often occur in Arabic text as transliteration. This is the case for
64. * many categories of foreign words, not just proper names but also technical terms
65. * such as caviar, telephone and internet.
66. *
67. * Example:
68. * <code>
69. * include('./I18N/Arabic.php');
70. * $obj = new I18N_Arabic('Transliteration');
71. *
72. * $ar_word_1 = $obj->en2ar($en_word_1);
73. * $en_word_2 = $obj->ar2en($ar_word_2);
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 transliterate English words into 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_Transliteration
97. {
98. private static $_arFinePatterns = array("/'+/u", "/([\- ])'/u", '/(.)#/u');
99. private static $_arFineReplacements = array("'", '\\1', "\\1'\\1");
101. private static $_en2arPregSearch = array();
102. private static $_en2arPregReplace = array();
103. private static $_en2arStrSearch = array();
104. private static $_en2arStrReplace = array();
106. private static $_ar2enPregSearch = array();
107. private static $_ar2enPregReplace = array();
108. private static $_ar2enStrSearch = array();
109. private static $_ar2enStrReplace = array();
111. private static $_diariticalSearch = array();
112. private static $_diariticalReplace = array();
114. private static $_iso233Search = array();
115. private static $_iso233Replace = array();
117. private static $_rjgcSearch = array();
118. private static $_rjgcReplace = array();
120. private static $_sesSearch = array();
121. private static $_sesReplace = array();
123. /**
124. * Loads initialize values
125. *
126. * @ignore
127. */
128. public function __construct()
129. {
130. $xml = simplexml_load_file(dirname(__FILE__).'/data/Transliteration.xml');
132. foreach ($xml->xpath("//preg_replace[@function='ar2en']/pair") as $pair) {
133. array_push(self::$_ar2enPregSearch, (string)$pair->search);
134. array_push(self::$_ar2enPregReplace, (string)$pair->replace);
135. }
137. foreach (
138. $xml->xpath("//str_replace[@function='diaritical']/pair") as $pair
139. ) {
140. array_push(self::$_diariticalSearch, (string)$pair->search);
141. array_push(self::$_diariticalReplace, (string)$pair->replace);
142. }
144. foreach ($xml->xpath("//str_replace[@function='ISO233']/pair") as $pair) {
145. array_push(self::$_iso233Search, (string)$pair->search);
146. array_push(self::$_iso233Replace, (string)$pair->replace);
147. }
149. foreach ($xml->xpath("//str_replace[@function='RJGC']/pair") as $pair) {
150. array_push(self::$_rjgcSearch, (string)$pair->search);
151. array_push(self::$_rjgcReplace, (string)$pair->replace);
152. }
154. foreach ($xml->xpath("//str_replace[@function='SES']/pair") as $pair) {
155. array_push(self::$_sesSearch, (string)$pair->search);
156. array_push(self::$_sesReplace, (string)$pair->replace);
157. }
159. foreach ($xml->xpath("//str_replace[@function='ar2en']/pair") as $pair) {
160. array_push(self::$_ar2enStrSearch, (string)$pair->search);
161. array_push(self::$_ar2enStrReplace, (string)$pair->replace);
162. }
164. foreach ($xml->xpath("//preg_replace[@function='en2ar']/pair") as $pair) {
165. array_push(self::$_en2arPregSearch, (string)$pair->search);
166. array_push(self::$_en2arPregReplace, (string)$pair->replace);
167. }
169. foreach ($xml->xpath("//str_replace[@function='en2ar']/pair") as $pair) {
170. array_push(self::$_en2arStrSearch, (string)$pair->search);
171. array_push(self::$_en2arStrReplace, (string)$pair->replace);
172. }
173. }
175. /**
176. * Transliterate English string into Arabic by render them in the
177. * orthography of the Arabic language
178. *
179. * @param string $string English string you want to transliterate
180. * @param string $locale Locale information (e.g. 'en_GB' or 'de_DE')
181. *
182. * @return String Out of vocabulary English string in Arabic characters
183. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
184. */
185. public static function en2ar($string, $locale='en_US')
186. {
187. setlocale(LC_ALL, $locale);
188. $string = iconv("UTF-8", "ASCII//TRANSLIT", $string);
189. $string = preg_replace('/[^\w\s]/', '', $string);
191. $string = strtolower($string);
192. $words = explode(' ', $string);
193. $string = '';
195. foreach ($words as $word) {
196. $word = preg_replace(
197. self::$_en2arPregSearch,
198. self::$_en2arPregReplace, $word
199. );
200. $word = str_replace(
201. self::$_en2arStrSearch,
202. self::$_en2arStrReplace,
203. $word
204. );
206. $string .= ' ' . $word;
207. }
209. return $string;
210. }
212. /**
213. * Transliterate Arabic string into English by render them in the
214. * orthography of the English language
215. *
216. * @param string $string Arabic string you want to transliterate
217. * @param string $standard Transliteration standard, default is UNGEGN
218. * and possible values are [UNGEGN, UNGEGN+, RJGC,
219. * SES, ISO233]
220. *
221. * @return String Out of vocabulary Arabic string in English characters
222. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
223. */
224. public static function ar2en($string, $standard='UNGEGN')
225. {
226. //$string = str_replace('ة ال', 'tul', $string);
228. $words = explode(' ', $string);
229. $string = '';
231. for ($i=0; $i<count($words)-1; $i++) {
232. $words[$i] = str_replace('ة', 'ت', $words[$i]);
233. }
235. foreach ($words as $word) {
236. $temp = $word;
238. if ($standard == 'UNGEGN+') {
239. $temp = str_replace(
240. self::$_diariticalSearch,
241. self::$_diariticalReplace,
242. $temp
243. );
244. } else if ($standard == 'RJGC') {
245. $temp = str_replace(
246. self::$_diariticalSearch,
247. self::$_diariticalReplace,
248. $temp
249. );
250. $temp = str_replace(
251. self::$_rjgcSearch,
252. self::$_rjgcReplace,
253. $temp
254. );
255. } else if ($standard == 'SES') {
256. $temp = str_replace(
257. self::$_diariticalSearch,
258. self::$_diariticalReplace,
259. $temp
260. );
261. $temp = str_replace(
262. self::$_sesSearch,
263. self::$_sesReplace,
264. $temp
265. );
266. } else if ($standard == 'ISO233') {
267. $temp = str_replace(
268. self::$_iso233Search,
269. self::$_iso233Replace,
270. $temp
271. );
272. }
274. $temp = preg_replace(
275. self::$_ar2enPregSearch,
276. self::$_ar2enPregReplace,
277. $temp
278. );
279. $temp = str_replace(
280. self::$_ar2enStrSearch,
281. self::$_ar2enStrReplace,
282. $temp
283. );
284. $temp = preg_replace(
285. self::$_arFinePatterns,
286. self::$_arFineReplacements,
287. $temp
288. );
290. if (preg_match('/[a-z]/', mb_substr($temp, 0, 1))) {
291. $temp = ucwords($temp);
292. }
294. $pos = strpos($temp, '-');
296. if ($pos > 0) {
297. if (preg_match('/[a-z]/', mb_substr($temp, $pos+1, 1))) {
298. $temp2 = substr($temp, 0, $pos);
299. $temp2 .= '-'.strtoupper($temp[$pos+1]);
300. $temp2 .= substr($temp, $pos+2);
301. } else {
302. $temp2 = $temp;
303. }
304. } else {
305. $temp2 = $temp;
306. }
308. $string .= ' ' . $temp2;
309. }
311. return $string;
312. }
314. /**
315. * Render numbers in given string using HTML entities that will show them as
316. * Arabic digits (i.e. 1, 2, 3, etc.) whatever browser language settings are
317. * (if browser supports UTF-8 character set).
318. *
319. * @param string $string String includes some digits here or there
320. *
321. * @return String Original string after replace digits by HTML entities that
322. * will show given number using Indian digits
323. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
324. */
325. public static function enNum($string)
326. {
327. $html = '';
329. $digits = str_split("$string");
331. foreach ($digits as $digit) {
332. $html .= preg_match('/\d/', $digit) ? "&#x3$digit;" : $digit;
333. }
335. return $html;
336. }
338. /**
339. * Render numbers in given string using HTML entities that will show them as
340. * Indian digits (i.e. ١, ٢, ٣, etc.) whatever browser language settings are
341. * (if browser supports UTF-8 character set).
342. *
343. * @param string $string String includes some digits here or there
344. *
345. * @return String Original string after replace digits by HTML entities that
346. * will show given number using Arabic digits
347. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
348. */
349. public static function arNum($string)
350. {
351. $html = '';
353. $digits = str_split("$string");
355. foreach ($digits as $digit) {
356. $html .= preg_match('/\d/', $digit) ? "&#x066$digit;" : $digit;
357. }
359. return $html;
360. }
361. }

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