我有一个 Python 代码,它采用阿拉伯语单词并获取词根并删除变音符号,但我的输出有问题。例如:当输入为“العربيه”时,输出为:“عرب”,但当输入为“كاتب”时,输出为:“ب”,当输入为“يخاف”时,输出为“خف”。
这是我的代码:
# -*- coding=utf-8 -*-
import re
from arabic_const import *
import Tashaphyne
from Tashaphyne import *
import enum
from enum import Enum
search_type=Enum('unvoc_word','voc_word','root_word')
HARAKAT_pat = re.compile(ur"[" + u"".join([FATHATAN, DAMMATAN, KASRATAN, FATHA, DAMMA, KASRA, SUKUN, SHADDA]) + u"]")
HAMZAT_pat = re.compile(ur"[" + u"".join([WAW_HAMZA, YEH_HAMZA]) + u"]");
ALEFAT_pat = re.compile(ur"[" + u"".join([ALEF_MADDA, ALEF_HAMZA_ABOVE, ALEF_HAMZA_BELOW, HAMZA_ABOVE, HAMZA_BELOW]) + u"]");
LAMALEFAT_pat = re.compile(ur"[" + u"".join([LAM_ALEF, LAM_ALEF_HAMZA_ABOVE, LAM_ALEF_HAMZA_BELOW, LAM_ALEF_MADDA_ABOVE]) + u"]");
#--------------------------------------
def strip_tashkeel(w):
"strip vowel from a word and return a result word"
return HARAKAT_pat.sub('', w)
#strip tatweel from a word and return a result word
#--------------------------------------
def strip_tatweel(w):
"strip tatweel from a word and return a result word"
return re.sub(ur'[%s]' % TATWEEL, '', w)
#--------------------------------------
def normalize_hamza(w):
"strip vowel from a word and return a result word"
w = ALEFAT_pat.sub(ALEF, w)
return HAMZAT_pat.sub(HAMZA, w)
#--------------------------------------
def normalize_lamalef(w):
"strip vowel from a word and return a result word"
return LAMALEFAT_pat.sub(u'%s%s' % (LAM, ALEF), w)
#--------------------------------------
def normalize_spellerrors(w):
"strip vowel from a word and return a result word"
w = re.sub(ur'[%s]' % TEH_MARBUTA, HEH, w)
return re.sub(ur'[%s]' % ALEF_MAKSURA, YEH, w)
def normalize_text(word,searchtype):
word = strip_tashkeel(word)
word = strip_tatweel(word)
word = normalize_lamalef(word)
word = normalize_hamza(word)
word = normalize_spellerrors(word)
if searchtype==search_type.root_word.index:
ArListem=ArabicLightStemmer();
stem=ArListem.lightStm(word);
word=ArListem.get_root();
print word
return word
#---------------------------------------------
这是测试代码:
**from task import normalize_text
normalize_text(u'كاتب',2)
输出是:ب