我正在使用 python 模式来获取英语名词的单数形式。
In [1]: from pattern.en import singularize
In [2]: singularize('patterns')
Out[2]: 'pattern'
In [3]: singularize('gases')
Out[3]: 'gase'
我正在通过定义解决第二个例子中的问题
def my_singularize(strn):
'''
Return the singular of a noun. Add special cases to correct pattern generic rules.
'''
exceptionDict = {'gases':'gas','spectra':'spectrum','cross':'cross','nuclei':'nucleus'}
try:
return exceptionDict[strn]
except:
return singularize(strn)
有没有更好的方法来做到这一点,例如添加到模式规则,或者使 exceptionDict
以某种方式成为模式的内部?
最佳答案
如评论中所述,对单词进行词形还原会更好。 它的一部分 nltk stemming module .
from nltk.stem import WordNetLemmatizer
wnl = WordNetLemmatizer()
test_words = ['gases', 'spectrum','cross','nuclei']
%timeit [wnl.lemmatize(wrd) for wrd in test_words]
10000 loops, best of 3: 60.5 µs per loop
与你的功能相比
%timeit [my_singularize(wrd) for wrd in test_words]
1000 loops, best of 3: 162 µs per loop
nltk 词形还原表现更好。
关于python - 将术语添加到 python 模式单数化的好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23586591/