我如何使用NLTK模块来编写名词的单数和复数形式,或者告诉它在搜索单词的txt文件时不要区分单数和复数?我可以使用NLTK使程序不区分大小写吗?
tae*_*esu 12
你可以通过使用pattern.en,而不是太确定来做到这一点NLTK
>>> from pattern.en import pluralize, singularize
>>>
>>> print pluralize('child') #children
>>> print singularize('wolves') #wolf
Run Code Online (Sandbox Code Playgroud)
看到更多
这是使用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
Run Code Online (Sandbox Code Playgroud)
区分大小写已str.lower()在所有单词中使用,当然,如果需要,您还必须对搜索词进行词素化。
当前编写的模式不支持Python 3(尽管此处https://github.com/clips/pattern/issues/62对此进行了持续的讨论。
TextBlob https://textblob.readthedocs.io建立在pattern和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']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8953 次 |
| 最近记录: |