I18N_Arabic

# Source for file Stemmer.php

Documentation is available at Stemmer.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 Text ArStemmer Class
31. *
32. * Filename: Stemmer.php
33. *
34. * Original Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
35. *
36. * Purpose: Get stem of an Arabic word
37. *
38. * ----------------------------------------------------------------------
39. *
40. * Source: http://arabtechies.net/node/83
41. * By: Taha Zerrouki <taha.zerrouki@gmail.com>
42. *
43. * ----------------------------------------------------------------------
44. *
45. * Arabic Word Stemmer Class
46. *
47. * PHP class to get stem of an Arabic word
48. *
49. * A stemmer is an automatic process in which morphological variants of terms
50. * are mapped to a single representative string called a stem. Arabic belongs
51. * to the Semitic family of languages which also includes Hebrew and Aramaic.
52. * Since morphological change in Arabic results from the addition of prefixes
53. * and infixes as well as suffixes, simple removal of suffixes is not as
54. * effective for Arabic as it is for English.
55. *
56. * Arabic has much richer morphology than English. Arabic has two genders,
57. * feminine and masculine; three numbers, singular, dual, and plural; and three
58. * grammatical cases, nominative, genitive, and accusative. A noun has the
59. * nominative case when it is a subject; accusative when it is the object of a
60. * verb; and genitive when it is the object of a preposition. The form of an
61. * Arabic noun is determined by its gender, number, and grammatical case. The
62. * definitive nouns are formed by attaching the Arabic article "AL" to the
63. * immediate front of the nouns. Besides prefixes, a noun can also carry a
64. * suffix which is often a possessive pronoun. In Arabic, the conjunction word
65. * "WA" (and) is often attached to the following word.
66. *
67. * Like nouns, an Arabic adjective can also have many variants. When an
68. * adjective modifies a noun in a noun phrase, the adjective agrees with the
69. * noun in gender, number, case, and definiteness. Arabic verbs have two tenses:
70. * perfect and imperfect. Perfect tense denotes actions completed, while
71. * imperfect denotes uncompleted actions. The imperfect tense has four mood:
72. * indicative, subjective, jussive, and imperative. Arabic verbs in perfect
73. * tense consist of a stem and a subject marker. The subject marker indicates
74. * the person, gender, and number of the subject. The form of a verb in perfect
75. * tense can have subject marker and pronoun suffix. The form of a
76. * subject-marker is determined together by the person, gender, and number of
77. * the subject.
78. * Example:
79. * <code>
80. * include('./I18N/Arabic.php');
81. * $obj = new I18N_Arabic('Stemmer');
82. *
83. * echo $obj->stem($word);
84. * </code>
85. *
86. * @category I18N
87. * @package I18N_Arabic
88. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
89. * @copyright 2006-2016 Khaled Al-Sham'aa
90. *
91. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
92. * @link http://www.ar-php.org
93. */
95. /**
96. * This PHP class get stem of an Arabic word
97. *
98. * @category I18N
99. * @package I18N_Arabic
100. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
101. * @copyright 2006-2016 Khaled Al-Sham'aa
102. *
103. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
104. * @link http://www.ar-php.org
105. */
106. class I18N_Arabic_Stemmer
107. {
108. private static $_verbPre = 'وأسفلي';
109. private static $_verbPost = 'ومكانيه';
110. private static $_verbMay;
112. private static $_verbMaxPre = 4;
113. private static $_verbMaxPost = 6;
114. private static $_verbMinStem = 2;
116. private static $_nounPre = 'ابفكلوأ';
117. private static $_nounPost = 'اتةكمنهوي';
118. private static $_nounMay;
120. private static $_nounMaxPre = 4;
121. private static $_nounMaxPost = 6;
122. private static $_nounMinStem = 2;
124. /**
125. * Loads initialize values
126. *
127. * @ignore
128. */
129. public function __construct()
130. {
131. self::$_verbMay = self::$_verbPre . self::$_verbPost;
132. self::$_nounMay = self::$_nounPre . self::$_nounPost;
133. }
135. /**
136. * Get rough stem of the given Arabic word
137. *
138. * @param string $word Arabic word you would like to get its stem
139. *
140. * @return string Arabic stem of the word
141. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
142. */
143. public static function stem($word)
144. {
145. $nounStem = self::roughStem(
146. $word, self::$_nounMay, self::$_nounPre, self::$_nounPost,
147. self::$_nounMaxPre, self::$_nounMaxPost, self::$_nounMinStem
148. );
149. $verbStem = self::roughStem(
150. $word, self::$_verbMay, self::$_verbPre, self::$_verbPost,
151. self::$_verbMaxPre, self::$_verbMaxPost, self::$_verbMinStem
152. );
154. if (mb_strlen($nounStem, 'UTF-8') < mb_strlen($verbStem, 'UTF-8')) {
155. $stem = $nounStem;
156. } else {
157. $stem = $verbStem;
158. }
160. return $stem;
161. }
163. /**
164. * Get rough stem of the given Arabic word (under specific rules)
165. *
166. * @param string $word Arabic word you would like to get its stem
167. * @param string $notChars Arabic chars those can't be in postfix or prefix
168. * @param string $preChars Arabic chars those may exists in the prefix
169. * @param string $postChars Arabic chars those may exists in the postfix
170. * @param integer $maxPre Max prefix length
171. * @param integer $maxPost Max postfix length
172. * @param integer $minStem Min stem length
173. *
174. * @return string Arabic stem of the word under giving rules
175. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
176. */
177. protected static function roughStem (
178. $word, $notChars, $preChars, $postChars, $maxPre, $maxPost, $minStem
179. ) {
180. $right = -1;
181. $left = -1;
182. $max = mb_strlen($word, 'UTF-8');
184. for ($i=0; $i < $max; $i++) {
185. $needle = mb_substr($word, $i, 1, 'UTF-8');
186. if (mb_strpos($notChars, $needle, 0, 'UTF-8') === false) {
187. if ($right == -1) {
188. $right = $i;
189. }
190. $left = $i;
191. }
192. }
194. if ($right > $maxPre) {
195. $right = $maxPre;
196. }
198. if ($max - $left - 1 > $maxPost) {
199. $left = $max - $maxPost -1;
200. }
202. for ($i=0; $i < $right; $i++) {
203. $needle = mb_substr($word, $i, 1, 'UTF-8');
204. if (mb_strpos($preChars, $needle, 0, 'UTF-8') === false) {
205. $right = $i;
206. break;
207. }
208. }
210. for ($i=$max-1; $i>$left; $i--) {
211. $needle = mb_substr($word, $i, 1, 'UTF-8');
212. if (mb_strpos($postChars, $needle, 0, 'UTF-8') === false) {
213. $left = $i;
214. break;
215. }
216. }
218. if ($left - $right >= $minStem) {
219. $stem = mb_substr($word, $right, $left-$right+1, 'UTF-8');
220. } else {
221. $stem = null;
222. }
224. return $stem;
225. }
226. }

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