I18N_Arabic

# Source for file Normalise.php

Documentation is available at Normalise.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: Functions to normalise Arabic text.
31. *
32. * Filename: Normalise.php
33. *
34. * Original Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
35. *
36. * Purpose: Text normalisation through various stages. Also: unshaping.
37. *
38. * ----------------------------------------------------------------------
39. *
40. * This class provides various functions to manipulate arabic text and
41. * normalise it by applying filters, for example, to strip tatweel and
42. * tashkeel, to normalise hamza and lamalephs, and to unshape
43. * a joined Arabic text back into its normalised form.
44. *
45. * There is also a function to reverse a utf8 string.
46. *
47. * The functions are helpful for searching, indexing and similar
48. * functions.
49. *
50. * Note that this class can only deal with UTF8 strings. You can use functions
51. * from the other classes to convert between encodings if necessary.
52. *
53. * Example:
54. * <code>
55. * include('./I18N/Arabic.php');
56. * $obj = new I18N_Arabic('Normalise');
57. *
58. * $str = "Arabic text with tatweel, tashkeel...";
59. *
60. * echo "<p><u><i>Before:</i></u><br />$str<br /><br />";
61. *
62. * $text = $obj->stripTatweel($str);
63. *
64. * echo "<u><i>After:</i></u><br />$text<br /><br />";
65. * </code>
66. *
67. * @category I18N
68. * @package I18N_Arabic
69. * @author Djihed Afifi <djihed@gmail.com>
70. * @copyright 2006-2016 Khaled Al-Sham'aa
71. *
72. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
73. * @link http://www.ar-php.org
74. */
76. /**
77. * This class provides various functions to manipulate arabic text and
78. * normalise it by applying filters, for example, to strip tatweel and
79. * tashkeel, to normalise hamza and lamalephs, and to unshape
80. * a joined Arabic text back into its normalised form.
81. *
82. * The functions are helpful for searching, indexing and similar
83. * functions.
84. *
85. * @category I18N
86. * @package I18N_Arabic
87. * @author Djihed Afifi <djihed@gmail.com>
88. * @copyright 2006-2016 Khaled Al-Sham'aa
89. *
90. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
91. * @link http://www.ar-php.org
92. */
93. class I18N_Arabic_Normalise
94. {
95. private $_unshapeMap = array();
96. private $_unshapeKeys = array();
97. private $_unshapeValues = array();
98. private $_chars = array();
99. private $_charGroups = array();
100. private $_charArNames = array();
102. /**
103. * Load the Unicode constants that will be used ibn substitutions
104. * and normalisations.
105. *
106. * @ignore
107. */
108. public function __construct()
109. {
110. include dirname(__FILE__) . '/data/charset/ArUnicode.constants.php';
112. $this->_unshapeMap = $ligature_map;
113. $this->_unshapeKeys = array_keys($this->_unshapeMap);
114. $this->_unshapeValues = array_values($this->_unshapeMap);
115. $this->_chars = $char_names;
116. $this->_charGroups = $char_groups;
117. $this->_charArNames = $char_ar_names;
118. }
120. /**
121. * Strip all tatweel characters from an Arabic text.
122. *
123. * @param string $text The text to be stripped.
124. *
125. * @return string the stripped text.
126. * @author Djihed Afifi <djihed@gmail.com>
127. */
128. public function stripTatweel($text)
129. {
130. return str_replace($this->_chars['TATWEEL'], '', $text);
131. }
133. /**
134. * Strip all tashkeel characters from an Arabic text.
135. *
136. * @param string $text The text to be stripped.
137. *
138. * @return string the stripped text.
139. * @author Djihed Afifi <djihed@gmail.com>
140. */
141. public function stripTashkeel($text)
142. {
143. $tashkeel = array(
144. $this->_chars['FATHATAN'],
145. $this->_chars['DAMMATAN'],
146. $this->_chars['KASRATAN'],
147. $this->_chars['FATHA'],
148. $this->_chars['DAMMA'],
149. $this->_chars['KASRA'],
150. $this->_chars['SUKUN'],
151. $this->_chars['SHADDA']
152. );
153. return str_replace($tashkeel, "", $text);
154. }
156. /**
157. * Normalise all Hamza characters to their corresponding aleph
158. * character in an Arabic text.
159. *
160. * @param string $text The text to be normalised.
161. *
162. * @return string the normalised text.
163. * @author Djihed Afifi <djihed@gmail.com>
164. */
165. public function normaliseHamza($text)
166. {
167. $replace = array(
168. $this->_chars['WAW_HAMZA'] = $this->_chars['WAW'],
169. $this->_chars['YEH_HAMZA'] = $this->_chars['YEH'],
170. );
171. $alephs = array(
172. $this->_chars['ALEF_MADDA'],
173. $this->_chars['ALEF_HAMZA_ABOVE'],
174. $this->_chars['ALEF_HAMZA_BELOW'],
175. $this->_chars['HAMZA_ABOVE,HAMZA_BELOW']
176. );
178. $text = str_replace(array_keys($replace), array_values($replace), $text);
179. $text = str_replace($alephs, $this->_chars['ALEF'], $text);
180. return $text;
181. }
183. /**
184. * Unicode uses some special characters where the lamaleph and any
185. * hamza above them are combined into one code point. Some input
186. * system use them. This function expands these characters.
187. *
188. * @param string $text The text to be normalised.
189. *
190. * @return string the normalised text.
191. * @author Djihed Afifi <djihed@gmail.com>
192. */
193. public function normaliseLamaleph ($text)
194. {
195. $text = str_replace(
196. $this->_chars['LAM_ALEPH'],
197. $simple_LAM_ALEPH,
198. $text
199. );
200. $text = str_replace(
201. $this->_chars['LAM_ALEPH_HAMZA_ABOVE'],
202. $simple_LAM_ALEPH_HAMZA_ABOVE,
203. $text
204. );
205. $text = str_replace(
206. $this->_chars['LAM_ALEPH_HAMZA_BELOW'],
207. $simple_LAM_ALEPH_HAMZA_BELOW,
208. $text
209. );
210. $text = str_replace(
211. $this->_chars['LAM_ALEPH_MADDA_ABOVE'],
212. $simple_LAM_ALEPH_MADDA_ABOVE,
213. $text
214. );
215. return $text;
216. }
218. /**
219. * Return unicode char by its code point.
220. *
221. * @param char $u code point
222. *
223. * @return string the result character.
224. * @author Djihed Afifi <djihed@gmail.com>
225. */
226. public function unichr($u)
227. {
228. return mb_convert_encoding('&#'.intval($u).';', 'UTF-8', 'HTML-ENTITIES');
229. }
231. /**
232. * Takes a string, it applies the various filters in this class
233. * to return a unicode normalised string suitable for activities
234. * such as searching, indexing, etc.
235. *
236. * @param string $text the text to be normalised.
237. *
238. * @return string the result normalised string.
239. * @author Djihed Afifi <djihed@gmail.com>
240. */
241. public function normalise($text)
242. {
243. $text = $this->stripTashkeel($text);
244. $text = $this->stripTatweel($text);
245. $text = $this->normaliseHamza($text);
246. $text = $this->normaliseLamaleph($text);
248. return $text;
249. }
251. /**
252. * Takes Arabic text in its joined form, it untangles the characters
253. * and unshapes them.
254. *
255. * This can be used to process text that was processed through OCR
256. * or by extracting text from a PDF document.
257. *
258. * Note that the result text may need further processing. In most
259. * cases, you will want to use the utf8Strrev function from
260. * this class to reverse the string.
261. *
262. * Most of the work of setting up the characters for this function
263. * is done through the ArUnicode.constants.php constants and
264. * the constructor loading.
265. *
266. * @param string $text the text to be unshaped.
267. *
268. * @return string the result normalised string.
269. * @author Djihed Afifi <djihed@gmail.com>
270. */
271. public function unshape($text)
272. {
273. return str_replace($this->_unshapeKeys, $this->_unshapeValues, $text);
274. }
276. /**
277. * Take a UTF8 string and reverse it.
278. *
279. * @param string $str the string to be reversed.
280. * @param boolean $reverse_numbers whether to reverse numbers.
281. *
282. * @return string The reversed string.
283. */
284. public function utf8Strrev($str, $reverse_numbers = false)
285. {
286. preg_match_all('/./us', $str, $ar);
287. if ($reverse_numbers) {
288. return join('', array_reverse($ar[0]));
289. } else {
290. $temp = array();
291. foreach ($ar[0] as $value) {
292. if (is_numeric($value) && !empty($temp[0]) && is_numeric($temp[0])) {
293. foreach ($temp as $key => $value2) {
294. if (is_numeric($value2)) {
295. $pos = ($key + 1);
296. } else {
297. break;
298. }
299. }
300. $temp2 = array_splice($temp, $pos);
301. $temp = array_merge($temp, array($value), $temp2);
302. } else {
303. array_unshift($temp, $value);
304. }
305. }
306. return implode('', $temp);
307. }
308. }
310. /**
311. * Checks for Arabic Tashkeel marks (i.e. FATHA, DAMMA, KASRA, SUKUN,
312. * SHADDA, FATHATAN, DAMMATAN, KASRATAN).
313. *
314. * @param string $archar Arabic unicode char
315. *
316. * @return boolean True if it is Arabic Tashkeel mark
317. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
318. */
319. public function isTashkeel($archar)
320. {
321. $key = array_search($archar, $this->_chars);
323. if (in_array($key, $this->_charGroups['TASHKEEL'])) {
324. $value = true;
325. } else {
326. $value = false;
327. }
329. return $value;
330. }
332. /**
333. * Checks for Arabic Harakat marks (i.e. FATHA, DAMMA, KASRA, SUKUN, TANWIN).
334. *
335. * @param string $archar Arabic unicode char
336. *
337. * @return boolean True if it is Arabic Harakat mark
338. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
339. */
340. public function isHaraka($archar)
341. {
342. $key = array_search($archar, $this->_chars);
344. if (in_array($key, $this->_charGroups['HARAKAT'])) {
345. $value = true;
346. } else {
347. $value = false;
348. }
350. return $value;
351. }
353. /**
354. * Checks for Arabic short Harakat marks (i.e. FATHA, DAMMA, KASRA, SUKUN).
355. *
356. * @param string $archar Arabic unicode char
357. *
358. * @return boolean True if it is Arabic short Harakat mark
359. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
360. */
361. public function isShortharaka($archar)
362. {
363. $key = array_search($archar, $this->_chars);
365. if (in_array($key, $this->_charGroups['SHORTHARAKAT'])) {
366. $value = true;
367. } else {
368. $value = false;
369. }
371. return $value;
372. }
374. /**
375. * Checks for Arabic Tanwin marks (i.e. FATHATAN, DAMMATAN, KASRATAN).
376. *
377. * @param string $archar Arabic unicode char
378. *
379. * @return boolean True if it is Arabic Tanwin mark
380. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
381. */
382. public function isTanwin($archar)
383. {
384. $key = array_search($archar, $this->_chars);
386. if (in_array($key, $this->_charGroups['TANWIN'])) {
387. $value = true;
388. } else {
389. $value = false;
390. }
392. return $value;
393. }
395. /**
396. * Checks for Arabic Ligatures like LamAlef (i.e. LAM ALEF, LAM ALEF HAMZA
397. * ABOVE, LAM ALEF HAMZA BELOW, LAM ALEF MADDA ABOVE).
398. *
399. * @param string $archar Arabic unicode char
400. *
401. * @return boolean True if it is Arabic Ligatures like LamAlef
402. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
403. */
404. public function isLigature($archar)
405. {
406. $key = array_search($archar, $this->_chars);
408. if (in_array($key, $this->_charGroups['LIGUATURES'])) {
409. $value = true;
410. } else {
411. $value = false;
412. }
414. return $value;
415. }
417. /**
418. * Checks for Arabic Hamza forms (i.e. HAMZA, WAW HAMZA, YEH HAMZA, HAMZA ABOVE,
419. * HAMZA BELOW, ALEF HAMZA BELOW, ALEF HAMZA ABOVE).
420. *
421. * @param string $archar Arabic unicode char
422. *
423. * @return boolean True if it is Arabic Hamza form
424. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
425. */
426. public function isHamza($archar)
427. {
428. $key = array_search($archar, $this->_chars);
430. if (in_array($key, $this->_charGroups['HAMZAT'])) {
431. $value = true;
432. } else {
433. $value = false;
434. }
436. return $value;
437. }
439. /**
440. * Checks for Arabic Alef forms (i.e. ALEF, ALEF MADDA, ALEF HAMZA ABOVE,
441. * ALEF HAMZA BELOW,ALEF WASLA, ALEF MAKSURA).
442. *
443. * @param string $archar Arabic unicode char
444. *
445. * @return boolean True if it is Arabic Alef form
446. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
447. */
448. public function isAlef($archar)
449. {
450. $key = array_search($archar, $this->_chars);
452. if (in_array($key, $this->_charGroups['ALEFAT'])) {
453. $value = true;
454. } else {
455. $value = false;
456. }
458. return $value;
459. }
461. /**
462. * Checks for Arabic Weak letters (i.e. ALEF, WAW, YEH, ALEF_MAKSURA).
463. *
464. * @param string $archar Arabic unicode char
465. *
466. * @return boolean True if it is Arabic Weak letter
467. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
468. */
469. public function isWeak($archar)
470. {
471. $key = array_search($archar, $this->_chars);
473. if (in_array($key, $this->_charGroups['WEAK'])) {
474. $value = true;
475. } else {
476. $value = false;
477. }
479. return $value;
480. }
482. /**
483. * Checks for Arabic Yeh forms (i.e. YEH, YEH HAMZA, SMALL YEH, ALEF MAKSURA).
484. *
485. * @param string $archar Arabic unicode char
486. *
487. * @return boolean True if it is Arabic Yeh form
488. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
489. */
490. public function isYehlike($archar)
491. {
492. $key = array_search($archar, $this->_chars);
494. if (in_array($key, $this->_charGroups['YEHLIKE'])) {
495. $value = true;
496. } else {
497. $value = false;
498. }
500. return $value;
501. }
503. /**
504. * Checks for Arabic Waw like forms (i.e. WAW, WAW HAMZA, SMALL WAW).
505. *
506. * @param string $archar Arabic unicode char
507. *
508. * @return boolean True if it is Arabic Waw like form
509. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
510. */
511. public function isWawlike($archar)
512. {
513. $key = array_search($archar, $this->_chars);
515. if (in_array($key, $this->_charGroups['WAWLIKE'])) {
516. $value = true;
517. } else {
518. $value = false;
519. }
521. return $value;
522. }
524. /**
525. * Checks for Arabic Teh forms (i.e. TEH, TEH MARBUTA).
526. *
527. * @param string $archar Arabic unicode char
528. *
529. * @return boolean True if it is Arabic Teh form
530. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
531. */
532. public function isTehlike($archar)
533. {
534. $key = array_search($archar, $this->_chars);
536. if (in_array($key, $this->_charGroups['TEHLIKE'])) {
537. $value = true;
538. } else {
539. $value = false;
540. }
542. return $value;
543. }
545. /**
546. * Checks for Arabic Small letters (i.e. SMALL ALEF, SMALL WAW, SMALL YEH).
547. *
548. * @param string $archar Arabic unicode char
549. *
550. * @return boolean True if it is Arabic Small letter
551. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
552. */
553. public function isSmall($archar)
554. {
555. $key = array_search($archar, $this->_chars);
557. if (in_array($key, $this->_charGroups['SMALL'])) {
558. $value = true;
559. } else {
560. $value = false;
561. }
563. return $value;
564. }
566. /**
567. * Checks for Arabic Moon letters.
568. *
569. * @param string $archar Arabic unicode char
570. *
571. * @return boolean True if it is Arabic Moon letter
572. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
573. */
574. public function isMoon($archar)
575. {
576. $key = array_search($archar, $this->_chars);
578. if (in_array($key, $this->_charGroups['MOON'])) {
579. $value = true;
580. } else {
581. $value = false;
582. }
584. return $value;
585. }
587. /**
588. * Checks for Arabic Sun letters.
589. *
590. * @param string $archar Arabic unicode char
591. *
592. * @return boolean True if it is Arabic Sun letter
593. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
594. */
595. public function isSun($archar)
596. {
597. $key = array_search($archar, $this->_chars);
599. if (in_array($key, $this->_charGroups['SUN'])) {
600. $value = true;
601. } else {
602. $value = false;
603. }
605. return $value;
606. }
608. /**
609. * Return Arabic letter name in arabic.
610. *
611. * @param string $archar Arabic unicode char
612. *
613. * @return string Arabic letter name in arabic
614. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
615. */
616. public function charName($archar)
617. {
618. $key = array_search($archar, $this->_chars);
620. $name = $this->_charArNames["$key"];
622. return $name;
623. }
624. }

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