Python - 生成单数名词的复数名词

问题描述 投票:6回答:4

我如何使用NLTK模块来编写名词的单数和复数形式,或者告诉它在搜索单词的txt文件时不要区分单数和复数?我可以使用NLTK使程序不区分大小写吗?

python nlp
4个回答
8
投票

你可以通过使用pattern.en来做到这一点,不太确定NLTK

>>> from pattern.en import pluralize, singularize
>>>  
>>> print pluralize('child') #children
>>> print singularize('wolves') #wolf

more


4
投票

目前正在编写的模式不支持Python 3(虽然这里有关于此问题的讨论,https://github.com/clips/pattern/issues/62

TextBlob https://textblob.readthedocs.io构建于模式和NLTK之上,还包括复数功能。它似乎做得很好,虽然它并不完美。请参阅下面的示例代码。

from textblob import TextBlob
words = "cat dog child goose pants"
blob = TextBlob(words)
plurals = [word.pluralize() for word in blob.words]
print(plurals)
# >>> ['cats', 'dogs', 'children', 'geese', 'pantss']

3
投票

这是使用NLTK完成此操作的一种可能方法。想象一下,您正在搜索“功能”这个词:

from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize

wnl = WordNetLemmatizer()
text = "This is a small text, a very small text with no interesting features."
tokens = [token.lower() for token in word_tokenize(text)]
lemmatized_words = [wnl.lemmatize(token) for token in tokens]
'feature' in lemmatized_words

在所有单词中使用str.lower()处理区分大小写,当然,如果有必要,您还必须将搜索词词形化。


0
投票

回答可能有点晚,但万一有人还在寻找类似的东西:

inflect(也可在github中使用)支持python 2.x和3.x.您可以找到给定单词的单数或复数形式:

import inflect
p = inflect.engine()

words = "cat dog child goose pants"
print([p.plural(word) for word in words.split(' ')])
# ['cats', 'dogs', 'children', 'geese', 'pant']

值得注意的是,复数的p.plural会给你单数形式。此外,您可以提供POS(部分语音)标记或提供数字,并且lib确定它是否需要复数或单数:

p.plural('cat', 4)   # cats
p.plural('cat', 1)   # cat
# but also...
p.plural('cat', 0)   # cats
© www.soinside.com 2019 - 2024. All rights reserved.