stemmer
This commit is contained in:
+87
@@ -0,0 +1,87 @@
|
||||
import os
|
||||
# from text_normalaizer import TextNormalizer
|
||||
# text_normalaizer = TextNormalizer()
|
||||
# text_normalaizer.split_words("سلام خوبی")
|
||||
|
||||
# from nltk.stem.api import StemmerI
|
||||
# class Stemmer(StemmerI):
|
||||
class Stemmer():
|
||||
"""این کلاس شامل توابعی برای ریشهیابی کلمات است."""
|
||||
|
||||
def __init__(self: "Stemmer") -> None:
|
||||
self.ends = [
|
||||
"ات",
|
||||
"ان",
|
||||
"ترین",
|
||||
"تر",
|
||||
"م",
|
||||
"ت",
|
||||
"ش",
|
||||
"یی",
|
||||
"ی",
|
||||
"ها",
|
||||
"ٔ",
|
||||
"ا",
|
||||
"",
|
||||
]
|
||||
self.dir_path = os.path.dirname(os.path.realpath(__file__)) + "/"
|
||||
self.mokasar_noun_path = self.dir_path + "resource/stemmer/mokasar.txt"
|
||||
self.createDict()
|
||||
|
||||
def createDict(self):
|
||||
self.Plural_dict={}
|
||||
with open(self.mokasar_noun_path, 'r', encoding='utf-8') as file:
|
||||
lines = file.readlines()
|
||||
for line in lines:
|
||||
words = line.strip().split('\t')
|
||||
if len(words) == 2:
|
||||
self.Plural_dict[words[0]] = words[1]
|
||||
def stem(self: "Stemmer", word: str) -> str:
|
||||
"""ریشهٔ کلمه را پیدا میکند.
|
||||
|
||||
Examples:
|
||||
>>> stemmer = Stemmer()
|
||||
>>> stemmer.stem('کتابی')
|
||||
'کتاب'
|
||||
>>> stemmer.stem('کتابها')
|
||||
'کتاب'
|
||||
>>> stemmer.stem('کتابهایی')
|
||||
'کتاب'
|
||||
>>> stemmer.stem('کتابهایشان')
|
||||
'کتاب'
|
||||
>>> stemmer.stem('اندیشهاش')
|
||||
'اندیشه'
|
||||
>>> stemmer.stem('خانۀ')
|
||||
'خانه'
|
||||
|
||||
Args:
|
||||
word: کلمهای که باید ریشهٔ آن پیدا شود.
|
||||
|
||||
Returns:
|
||||
ریشهٔ کلمه.
|
||||
|
||||
"""
|
||||
# این بخش برای حذف پسوندهای اسمی در استمینگ هضم طراحی شده است
|
||||
for end in self.ends:
|
||||
if word.endswith(end):
|
||||
word = word[:-len(end)]
|
||||
|
||||
if word.endswith("ۀ"):
|
||||
word = word[:-1] + "ه"
|
||||
|
||||
# این بخش برای تبدیل جمع مکسر به دستور استمینگ هضم اضافه شده است. بعد از حذف پسوندها، جمع مکسر باشد، تبدیل به مفرد می شود
|
||||
if word in self.Plural_dict:
|
||||
word = self.Plural_dict.get(word)
|
||||
|
||||
return word
|
||||
|
||||
# if __name__ == "__main__":
|
||||
# mokasar = "StemLemma/resource/mokasar.txt"
|
||||
# with open(mokasar, 'r', encoding='utf-8') as file:
|
||||
# lines = file.readlines()
|
||||
# dictionary = {}
|
||||
# for line in lines:
|
||||
# words = line.strip().split('\t')
|
||||
# if len(words) == 2:
|
||||
# dictionary[words[0]] = words[1]
|
||||
# print(dictionary)
|
||||
Reference in New Issue
Block a user