PageRenderTime 56ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/nltk/stem/isri.py

https://github.com/haewoon/nltk
Python | 382 lines | 338 code | 3 blank | 41 comment | 0 complexity | e3ab5a279d97c7cd3d2d338f69dd3fe3 MD5 | raw file
Possible License(s): Apache-2.0
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Natural Language Toolkit: The ISRI Arabic Stemmer
  4. #
  5. # Copyright (C) 2001-2012 NLTK Proejct
  6. # Algorithm: Kazem Taghva, Rania Elkhoury, and Jeffrey Coombs (2005)
  7. # Author: Hosam Algasaier <hosam_hme@yahoo.com>
  8. # URL: <http://www.nltk.org/>
  9. # For license information, see LICENSE.TXT
  10. """
  11. ISRI Arabic Stemmer
  12. The algorithm for this stemmer is described in:
  13. Taghva, K., Elkoury, R., and Coombs, J. 2005. Arabic Stemming without a root dictionary.
  14. Information Science Research Institute. University of Nevada, Las Vegas, USA.
  15. The Information Science Research Institutes (ISRI) Arabic stemmer shares many features
  16. with the Khoja stemmer. However, the main difference is that ISRI stemmer does not use root
  17. dictionary. Also, if a root is not found, ISRI stemmer returned normalized form, rather than
  18. returning the original unmodified word.
  19. Additional adjustments were made to improve the algorithm:
  20. 1- Adding 60 stop words.
  21. 2- Adding the pattern (تفاعيل) to ISRI pattern set.
  22. 3- The step 2 in the original algorithm was normalizing all hamza. This step is discarded because it
  23. increases the word ambiguities and changes the original root.
  24. """
  25. import re
  26. from api import StemmerI
  27. class ISRIStemmer(StemmerI):
  28. '''
  29. ISRI Arabic stemmer based on algorithm: Arabic Stemming without a root dictionary.
  30. Information Science Research Institute. University of Nevada, Las Vegas, USA.
  31. A few minor modifications have been made to ISRI basic algorithm.
  32. See the source code of this module for more information.
  33. isri.stem(token) returns Arabic root for the given token.
  34. The ISRI Stemmer requires that all tokens have Unicode string types.
  35. If you use Python IDLE on Arabic Windows you have to decode text first
  36. using Arabic '1256' coding.
  37. '''
  38. def __init__(self):
  39. self.stm = 'defult none'
  40. self.p3 = [u'\u0643\u0627\u0644', u'\u0628\u0627\u0644',
  41. u'\u0648\u0644\u0644', u'\u0648\u0627\u0644'] # length three prefixes
  42. self.p2 = [u'\u0627\u0644', u'\u0644\u0644'] # length two prefixes
  43. self.p1 = [u'\u0644', u'\u0628', u'\u0641', u'\u0633', u'\u0648',
  44. u'\u064a', u'\u062a', u'\u0646', u'\u0627'] # length one prefixes
  45. self.s3 = [u'\u062a\u0645\u0644', u'\u0647\u0645\u0644',
  46. u'\u062a\u0627\u0646', u'\u062a\u064a\u0646',
  47. u'\u0643\u0645\u0644'] # length three suffixes
  48. self.s2 = [u'\u0648\u0646', u'\u0627\u062a', u'\u0627\u0646',
  49. u'\u064a\u0646', u'\u062a\u0646', u'\u0643\u0645',
  50. u'\u0647\u0646', u'\u0646\u0627', u'\u064a\u0627',
  51. u'\u0647\u0627', u'\u062a\u0645', u'\u0643\u0646',
  52. u'\u0646\u064a', u'\u0648\u0627', u'\u0645\u0627',
  53. u'\u0647\u0645'] # length two suffixes
  54. self.s1 = [u'\u0629', u'\u0647', u'\u064a', u'\u0643', u'\u062a',
  55. u'\u0627', u'\u0646'] # length one suffixes
  56. self.pr4 = {0: [u'\u0645'], 1:[u'\u0627'],
  57. 2: [u'\u0627', u'\u0648', u'\u064A'], 3:[u'\u0629']} # groups of length four patterns
  58. self.pr53 = {0: [u'\u0627', u'\u062a'],
  59. 1: [u'\u0627', u'\u064a', u'\u0648'],
  60. 2: [u'\u0627', u'\u062a', u'\u0645'],
  61. 3: [u'\u0645', u'\u064a', u'\u062a'],
  62. 4: [u'\u0645', u'\u062a'],
  63. 5: [u'\u0627', u'\u0648'],
  64. 6: [u'\u0627', u'\u0645']} # Groups of length five patterns and length three roots
  65. self.re_short_vowels = re.compile(ur'[\u064B-\u0652]')
  66. self.re_hamza = re.compile(ur'[\u0621\u0624\u0626]')
  67. self.re_intial_hamza = re.compile(ur'^[\u0622\u0623\u0625]')
  68. self.stop_words = [u'\u064a\u0643\u0648\u0646',
  69. u'\u0648\u0644\u064a\u0633',
  70. u'\u0648\u0643\u0627\u0646',
  71. u'\u0643\u0630\u0644\u0643',
  72. u'\u0627\u0644\u062a\u064a',
  73. u'\u0648\u0628\u064a\u0646',
  74. u'\u0639\u0644\u064a\u0647\u0627',
  75. u'\u0645\u0633\u0627\u0621',
  76. u'\u0627\u0644\u0630\u064a',
  77. u'\u0648\u0643\u0627\u0646\u062a',
  78. u'\u0648\u0644\u0643\u0646',
  79. u'\u0648\u0627\u0644\u062a\u064a',
  80. u'\u062a\u0643\u0648\u0646',
  81. u'\u0627\u0644\u064a\u0648\u0645',
  82. u'\u0627\u0644\u0644\u0630\u064a\u0646',
  83. u'\u0639\u0644\u064a\u0647',
  84. u'\u0643\u0627\u0646\u062a',
  85. u'\u0644\u0630\u0644\u0643',
  86. u'\u0623\u0645\u0627\u0645',
  87. u'\u0647\u0646\u0627\u0643',
  88. u'\u0645\u0646\u0647\u0627',
  89. u'\u0645\u0627\u0632\u0627\u0644',
  90. u'\u0644\u0627\u0632\u0627\u0644',
  91. u'\u0644\u0627\u064a\u0632\u0627\u0644',
  92. u'\u0645\u0627\u064a\u0632\u0627\u0644',
  93. u'\u0627\u0635\u0628\u062d',
  94. u'\u0623\u0635\u0628\u062d',
  95. u'\u0623\u0645\u0633\u0649',
  96. u'\u0627\u0645\u0633\u0649',
  97. u'\u0623\u0636\u062d\u0649',
  98. u'\u0627\u0636\u062d\u0649',
  99. u'\u0645\u0627\u0628\u0631\u062d',
  100. u'\u0645\u0627\u0641\u062a\u0626',
  101. u'\u0645\u0627\u0627\u0646\u0641\u0643',
  102. u'\u0644\u0627\u0633\u064a\u0645\u0627',
  103. u'\u0648\u0644\u0627\u064a\u0632\u0627\u0644',
  104. u'\u0627\u0644\u062d\u0627\u0644\u064a',
  105. u'\u0627\u0644\u064a\u0647\u0627',
  106. u'\u0627\u0644\u0630\u064a\u0646',
  107. u'\u0641\u0627\u0646\u0647',
  108. u'\u0648\u0627\u0644\u0630\u064a',
  109. u'\u0648\u0647\u0630\u0627',
  110. u'\u0644\u0647\u0630\u0627',
  111. u'\u0641\u0643\u0627\u0646',
  112. u'\u0633\u062a\u0643\u0648\u0646',
  113. u'\u0627\u0644\u064a\u0647',
  114. u'\u064a\u0645\u0643\u0646',
  115. u'\u0628\u0647\u0630\u0627',
  116. u'\u0627\u0644\u0630\u0649']
  117. def stem(self, token):
  118. """
  119. Stemming a word token using the ISRI stemmer.
  120. """
  121. self.stm = token
  122. self.norm(1) # remove diacritics which representing Arabic short vowels
  123. if self.stm in self.stop_words: return self.stm # exclude stop words from being processed
  124. self.pre32() # remove length three and length two prefixes in this order
  125. self.suf32() # remove length three and length two suffixes in this order
  126. self.waw() # remove connective و if it precedes a word beginning with و
  127. self.norm(2) # normalize initial hamza to bare alif
  128. if len(self.stm)<=3: return self.stm # return stem if less than or equal to three
  129. if len(self.stm)==4: # length 4 word
  130. self.pro_w4()
  131. return self.stm
  132. elif len(self.stm)==5: # length 5 word
  133. self.pro_w53()
  134. self.end_w5()
  135. return self.stm
  136. elif len(self.stm)==6: # length 6 word
  137. self.pro_w6()
  138. self.end_w6()
  139. return self.stm
  140. elif len(self.stm)==7: # length 7 word
  141. self.suf1()
  142. if len(self.stm)==7:
  143. self.pre1()
  144. if len(self.stm)==6:
  145. self.pro_w6()
  146. self.end_w6()
  147. return self.stm
  148. return self.stm # if word length >7 , then no stemming
  149. def norm(self, num):
  150. """
  151. normalization:
  152. num=1 normalize diacritics
  153. num=2 normalize initial hamza
  154. num=3 both 1&2
  155. """
  156. self.k = num
  157. if self.k == 1:
  158. self.stm = self.re_short_vowels.sub('', self.stm)
  159. return self.stm
  160. elif self.k == 2:
  161. self.stm = self.re_intial_hamza.sub(ur'\u0627',self.stm)
  162. return self.stm
  163. elif self.k == 3:
  164. self.stm = self.re_short_vowels.sub('', self.stm)
  165. self.stm = self.re_intial_hamza.sub(ur'\u0627',self.stm)
  166. return self.stm
  167. def pre32(self):
  168. """remove length three and length two prefixes in this order"""
  169. if len(self.stm)>=6:
  170. for pre3 in self.p3:
  171. if self.stm.startswith(pre3):
  172. self.stm = self.stm[3:]
  173. return self.stm
  174. elif len(self.stm)>=5:
  175. for pre2 in self.p2:
  176. if self.stm.startswith(pre2):
  177. self.stm = self.stm[2:]
  178. return self.stm
  179. def suf32(self):
  180. """remove length three and length two suffixes in this order"""
  181. if len(self.stm)>=6:
  182. for suf3 in self.s3:
  183. if self.stm.endswith(suf3):
  184. self.stm = self.stm[:-3]
  185. return self.stm
  186. elif len(self.stm)>=5:
  187. for suf2 in self.s2:
  188. if self.stm.endswith(suf2):
  189. self.stm = self.stm[:-2]
  190. return self.stm
  191. def waw(self):
  192. """remove connective ‘و’ if it precedes a word beginning with ‘و’ """
  193. if (len(self.stm)>=4)&(self.stm[:2] == u'\u0648\u0648'):
  194. self.stm = self.stm[1:]
  195. return self.stm
  196. def pro_w4(self):
  197. """process length four patterns and extract length three roots"""
  198. if self.stm[0] in self.pr4[0]: # مفعل
  199. self.stm = self.stm[1:]
  200. return self.stm
  201. elif self.stm[1] in self.pr4[1]: # فاعل
  202. self.stm = self.stm[0]+self.stm[2:]
  203. return self.stm
  204. elif self.stm[2] in self.pr4[2]: # فعال - فعول - فعيل
  205. self.stm = self.stm[:2]+self.stm[3]
  206. return self.stm
  207. elif self.stm[3] in self.pr4[3]: # فعلة
  208. self.stm = self.stm[:-1]
  209. return self.stm
  210. else:
  211. self.suf1() # do - normalize short sufix
  212. if len(self.stm)==4:
  213. self.pre1() # do - normalize short prefix
  214. return self.stm
  215. def pro_w53(self):
  216. """process length five patterns and extract length three roots"""
  217. if ((self.stm[2] in self.pr53[0]) & (self.stm[0] == u'\u0627')): # افتعل - افاعل
  218. self.stm = self.stm[1]+self.stm[3:]
  219. return self.stm
  220. elif ((self.stm[3] in self.pr53[1]) & (self.stm[0] == u'\u0645')): # مفعول - مفعال - مفعيل
  221. self.stm = self.stm[1:3]+self.stm[4]
  222. return self.stm
  223. elif ((self.stm[0] in self.pr53[2]) & (self.stm[4] == u'\u0629')): # مفعلة - تفعلة - افعلة
  224. self.stm = self.stm[1:4]
  225. return self.stm
  226. elif ((self.stm[0] in self.pr53[3]) & (self.stm[2] == u'\u062a')): # مفتعل - يفتعل - تفتعل
  227. self.stm = self.stm[1]+self.stm[3:]
  228. return self.stm
  229. elif ((self.stm[0] in self.pr53[4]) & (self.stm[2] == u'\u0627')): #مفاعل - تفاعل
  230. self.stm = self.stm[1]+self.stm[3:]
  231. return self.stm
  232. elif ((self.stm[2] in self.pr53[5]) & (self.stm[4] == u'\u0629')): # فعولة - فعالة
  233. self.stm = self.stm[:2]+self.stm[3]
  234. return self.stm
  235. elif ((self.stm[0] in self.pr53[6]) & (self.stm[1] == u'\u0646')): # انفعل - منفعل
  236. self.stm = self.stm[2:]
  237. return self.stm
  238. elif ((self.stm[3] == u'\u0627') & (self.stm[0] == u'\u0627')): # افعال
  239. self.stm = self.stm[1:3]+self.stm[4]
  240. return self.stm
  241. elif ((self.stm[4] == u'\u0646') & (self.stm[3] == u'\u0627')): # فعلان
  242. self.stm = self.stm[:3]
  243. return self.stm
  244. elif ((self.stm[3] == u'\u064a') & (self.stm[0] == u'\u062a')): # تفعيل
  245. self.stm = self.stm[1:3]+self.stm[4]
  246. return self.stm
  247. elif ((self.stm[3] == u'\u0648') & (self.stm[1] == u'\u0627')): # فاعول
  248. self.stm = self.stm[0]+self.stm[2]+self.stm[4]
  249. return self.stm
  250. elif ((self.stm[2] == u'\u0627') & (self.stm[1] == u'\u0648')): # فواعل
  251. self.stm = self.stm[0]+self.stm[3:]
  252. return self.stm
  253. elif ((self.stm[3] == u'\u0626') & (self.stm[2] == u'\u0627')): # فعائل
  254. self.stm = self.stm[:2]+self.stm[4]
  255. return self.stm
  256. elif ((self.stm[4] == u'\u0629') & (self.stm[1] == u'\u0627')): # فاعلة
  257. self.stm = self.stm[0]+self.stm[2:4]
  258. return self.stm
  259. elif ((self.stm[4] == u'\u064a') & (self.stm[2] == u'\u0627')): # فعالي
  260. self.stm = self.stm[:2]+self.stm[3]
  261. return self.stm
  262. else:
  263. self.suf1() # do - normalize short sufix
  264. if len(self.stm)==5:
  265. self.pre1() # do - normalize short prefix
  266. return self.stm
  267. def pro_w54(self):
  268. """process length five patterns and extract length four roots"""
  269. if (self.stm[0] in self.pr53[2]): #تفعلل - افعلل - مفعلل
  270. self.stm = self.stm[1:]
  271. return self.stm
  272. elif (self.stm[4] == u'\u0629'): # فعللة
  273. self.stm = self.stm[:4]
  274. return self.stm
  275. elif (self.stm[2] == u'\u0627'): # فعالل
  276. self.stm = self.stm[:2]+self.stm[3:]
  277. return self.stm
  278. def end_w5(self):
  279. """ending step (word of length five)"""
  280. if len(self.stm)==3:
  281. return self.stm
  282. elif len(self.stm)==4:
  283. self.pro_w4()
  284. return self.stm
  285. elif len(self.stm)==5:
  286. self.pro_w54()
  287. return self.stm
  288. def pro_w6(self):
  289. """process length six patterns and extract length three roots"""
  290. if ((self.stm.startswith(u'\u0627\u0633\u062a')) or (self.stm.startswith(u'\u0645\u0633\u062a'))): # مستفعل - استفعل
  291. self.stm= self.stm[3:]
  292. return self.stm
  293. elif (self.stm[0]== u'\u0645' and self.stm[3]== u'\u0627' and self.stm[5]== u'\u0629'): # مفعالة
  294. self.stm = self.stm[1:3]+self.stm[4]
  295. return self.stm
  296. elif (self.stm[0]== u'\u0627' and self.stm[2]== u'\u062a' and self.stm[4]== u'\u0627'): # افتعال
  297. self.stm = self.stm[1]+self.stm[3]+self.stm[5]
  298. return self.stm
  299. elif (self.stm[0]== u'\u0627' and self.stm[3]== u'\u0648' and self.stm[2]==self.stm[4]): # افعوعل
  300. self.stm = self.stm[1]+self.stm[4:]
  301. return self.stm
  302. elif (self.stm[0]== u'\u062a' and self.stm[2]== u'\u0627' and self.stm[4]== u'\u064a'): # تفاعيل new pattern
  303. self.stm = self.stm[1]+self.stm[3]+self.stm[5]
  304. return self.stm
  305. else:
  306. self.suf1() # do - normalize short sufix
  307. if len(self.stm)==6:
  308. self.pre1() # do - normalize short prefix
  309. return self.stm
  310. def pro_w64(self):
  311. """process length six patterns and extract length four roots"""
  312. if (self.stm[0] and self.stm[4])==u'\u0627': # افعلال
  313. self.stm=self.stm[1:4]+self.stm[5]
  314. return self.stm
  315. elif (self.stm.startswith(u'\u0645\u062a')): # متفعلل
  316. self.stm = self.stm[2:]
  317. return self.stm
  318. def end_w6(self):
  319. """ending step (word of length six)"""
  320. if len(self.stm)==3:
  321. return self.stm
  322. elif len(self.stm)==5:
  323. self.pro_w53()
  324. self.end_w5()
  325. return self.stm
  326. elif len (self.stm)==6:
  327. self.pro_w64()
  328. return self.stm
  329. def suf1(self):
  330. """normalize short sufix"""
  331. for sf1 in self.s1:
  332. if self.stm.endswith(sf1):
  333. self.stm = self.stm[:-1]
  334. return self.stm
  335. def pre1(self):
  336. """normalize short prefix"""
  337. for sp1 in self.p1:
  338. if self.stm.startswith(sp1):
  339. self.stm = self.stm[1:]
  340. return self.stm
  341. if __name__ == "__main__":
  342. import doctest
  343. doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE)