我认为你忽略了使用spaCy数据库作为词形还原的参考部分。如果你看一下我在你的代码下面所做的修改,并提供输出,你会发现
duck
是
ducks
的正确
lemma_
。
import spacy
from spacy.lemmatizer import Lemmatizer
from spacy.lookups import Lookups
lookups = Lookups()
lemmatizer = Lemmatizer(lookups)
word = "ducks"
nlp = spacy.load('en_core_web_sm')
doc = nlp(word)
print("Token Attributes: \n", "token.text, token.pos_, token.tag_, token.dep_, token.lemma_")
for token in doc:
print("{:<12}{:<12}{:<12}{:<12}{:<12}".format(token.text, token.pos_, token.tag_, token.dep_, token.lemma_))
输出
Token Attributes:
token.text, token.pos_, token.tag_, token.dep_, token.lemma_
ducks NOUN NNS ROOT duck
词形归一化(Lemmatization)非常依赖于单词的词性。只有相同词性的单词才会映射到相同的词形。在句子“This is confusing”中,“confusing”被分析为形容词,因此它的词形还是“confusing”。相比之下,在句子“I was confusing you with someone else”中,“confusing”被分析为动词,词形被还原为“confuse”。
如果您想将不同词性的单词映射到同一个词形,您可以使用词干提取算法,例如
波特词干提取(Java),您可以对每个单词简单地调用该算法。