python - 将术语添加到 python 模式单数化的好方法

标签 python nlp

我正在使用 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/

相关文章:

python - Python 2.7 中 *args 附近的语法无效

python - 使用 Python 3(而非 Python 2)下载时 Zip 文件损坏

Python代码处理文本文档时不停顿

java - 如何解决OpenNLP中的 "Missing the manifest.properties"?

python - 分层索引数据帧上的 GroupBy 转换

python - 这个 cProfile 结果告诉我我需要修复什么?

java - ws4j 为应该返回 1 的相似性度量返回无穷大

python - key 错误 : "Unknown task summarization, available tasks are [' feature-extraction', 'sentiment-analysis' 、 'ner' 、 'question-answering' 、 'fill-mask' ]"

python - 无效参数错误 : Incompatible shapes with Keras LSTM Net

nlp - 英语动词列表及其时态、各种形式等