加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

添加术语到python模式singularize的好方法

发布时间:2020-12-16 21:30:43 所属栏目:Python 来源:网络整理
导读:我使用 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)
我使用 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 lemmatizing表现更好.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读