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