I18N_Arabic

# Source for file Identifier.php

Documentation is available at Identifier.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: Identify Arabic Text Segments
31. *
32. * Filename: Identifier.php
33. *
34. * Original Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
35. *
36. * Purpose: This class will identify Arabic text in a given UTF-8 multi
37. * language document, it will return array of start and end
38. * positions for Arabic text segments.
39. *
40. * ----------------------------------------------------------------------
41. *
42. * Identify Arabic Text Segments
43. *
44. * Using this PHP Class you can fully automated approach to processing
45. * Arabic text by quickly and accurately determining Arabic text segments within
46. * multiple languages documents.
47. *
48. * Understanding the language and encoding of a given document is an essential step
49. * in working with unstructured multilingual text. Without this basic knowledge,
50. * applications such as information retrieval and text mining cannot accurately
51. * process data and important information may be completely missed or mis-routed.
52. *
53. * Any application that works with Arabic in multiple languages documents can
54. * benefit from the ArIdentifier class. Using this class, applications can take a
55. * fully automated approach to processing Arabic text by quickly and accurately
56. * determining Arabic text segments within multiple languages document.
57. *
58. * Example:
59. * <code>
60. * include('./I18N/Arabic.php');
61. * $obj = new I18N_Arabic('Identifier');
62. *
63. * $hStr=$obj->highlightText($str, '#80B020');
64. *
65. * echo $str . '<hr />' . $hStr . '<hr />';
66. *
67. * $taggedText = $obj->tagText($str);
68. *
69. * foreach($taggedText as $wordTag) {
70. * list($word, $tag) = $wordTag;
71. *
72. * if ($tag == 1) {
73. * echo "$word is Noun, ";
74. * }
75. *
76. * if ($tag == 0) {
77. * echo "$word is not Noun, ";
78. * }
79. * }
80. * </code>
81. *
82. * @category I18N
83. * @package I18N_Arabic
84. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
85. * @copyright 2006-2016 Khaled Al-Sham'aa
86. *
87. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
88. * @link http://www.ar-php.org
89. */
91. /**
92. * This PHP class identify Arabic text segments
93. *
94. * @category I18N
95. * @package I18N_Arabic
96. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
97. * @copyright 2006-2016 Khaled Al-Sham'aa
98. *
99. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
100. * @link http://www.ar-php.org
101. */
102. class I18N_Arabic_Identifier
103. {
104. /**
105. * Loads initialize values
106. *
107. * @ignore
108. */
109. public function __construct()
110. {
111. }
113. /**
114. * Identify Arabic text in a given UTF-8 multi language string
115. *
116. * @param string $str UTF-8 multi language string
117. *
118. * @return array Offset of the beginning and end of each Arabic segment in
119. * sequence in the given UTF-8 multi language string
120. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
121. */
122. public static function identify($str)
123. {
124. $minAr = 55436;
125. $maxAr = 55698;
126. $probAr = false;
127. $arFlag = false;
128. $arRef = array();
129. $max = strlen($str);
131. $i = -1;
132. while (++$i < $max) {
133. $cDec = ord($str[$i]);
135. // ignore ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 :
136. // If it come in the Arabic context
137. if ($cDec >= 33 && $cDec <= 58) {
138. continue;
139. }
141. if (!$probAr && ($cDec == 216 || $cDec == 217)) {
142. $probAr = true;
143. continue;
144. }
146. if ($i > 0) {
147. $pDec = ord($str[$i - 1]);
148. } else {
149. $pDec = null;
150. }
152. if ($probAr) {
153. $utfDecCode = ($pDec << 8) + $cDec;
155. if ($utfDecCode >= $minAr && $utfDecCode <= $maxAr) {
156. if (!$arFlag) {
157. $arFlag = true;
158. $arRef[] = $i - 1;
159. }
160. } else {
161. if ($arFlag) {
162. $arFlag = false;
163. $arRef[] = $i - 1;
164. }
165. }
167. $probAr = false;
168. continue;
169. }
171. if ($arFlag && !preg_match("/^\s$/", $str[$i])) {
172. $arFlag = false;
173. $arRef[] = $i;
174. }
175. }
177. return $arRef;
178. }
180. /**
181. * Find out if given string is Arabic text or not
182. *
183. * @param string $str String
184. *
185. * @return boolean True if given string is UTF-8 Arabic, else will return False
186. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
187. */
188. public static function isArabic($str)
189. {
190. $isArabic = false;
191. $arr = self::identify($str);
193. if (count($arr) == 1 && $arr[0] == 0) {
194. $isArabic = true;
195. }
197. return $isArabic;
198. }
199. }

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