TL;DR
当您使用的 lemmatizer 是为了解决不同的问题时,这是一个 XY 问题,即 lemmatizer 未能满足您的期望。
长期
问:什么是引理?
语言学中的词形还原(或词形还原)是将单词的变形形式组合在一起的过程,以便可以将它们作为单个项目进行分析,由单词的词条或字典形式标识。 - Wikipedia
问:什么是“字典形式”?
NLTK 使用morphy 算法,该算法使用 WordNet 作为“字典形式”的基础
另见How does spacy lemmatizer works?。注意 SpaCy 有额外的技巧来处理更多不规则的单词。
问:为什么是moisture -> moisture 和moisturizing -> moisturizing?
因为“水分”和“保湿”有同义词(类似于“字典形式”)
>>> from nltk.corpus import wordnet as wn
>>> wn.synsets('moisture')
[Synset('moisture.n.01')]
>>> wn.synsets('moisture')[0].definition()
'wetness caused by water'
>>> wn.synsets('moisturizing')
[Synset('humidify.v.01')]
>>> wn.synsets('moisturizing')[0].definition()
'make (more) humid'
问:我怎样才能得到moisture -> moist?
不是很实用。但也许可以尝试一个词干分析器(但不要期望太多)
>>> from nltk.stem import PorterStemmer
>>> porter = PorterStemmer()
>>> porter.stem("moisture")
'moistur'
>>> porter.stem("moisturizing")
'moistur'
问:那我怎么得到moisuturizing/moisuture -> moist?!!
没有充分的方法可以做到这一点。但在尝试这样做之前,做moisuturizing/moisuture -> moist 的最终目的是什么。
真的有必要这样做吗?
如果您真的需要,您可以尝试词向量并尝试寻找最相似的词,但是词向量会带来另一个完全不同的警告世界。
问:等一下,heard -> heard 太可笑了?!
是的,词性标注器没有正确标注听到的内容。很可能是因为句子不是正确的句子,所以句子中的词的词性标签是错误的:
>>> from nltk import word_tokenize, pos_tag
>>> sent
'The laughs you two heard were triggered by memories of his own high j-flying moist moisture moisturize moisturizing.'
>>> pos_tag(word_tokenize(sent))
[('The', 'DT'), ('laughs', 'NNS'), ('you', 'PRP'), ('two', 'CD'), ('heard', 'NNS'), ('were', 'VBD'), ('triggered', 'VBN'), ('by', 'IN'), ('memories', 'NNS'), ('of', 'IN'), ('his', 'PRP$'), ('own', 'JJ'), ('high', 'JJ'), ('j-flying', 'NN'), ('moist', 'NN'), ('moisture', 'NN'), ('moisturize', 'VB'), ('moisturizing', 'NN'), ('.', '.')]
我们看到heard 被标记为NNS(一个名词)。如果我们将其词形化为动词:
>>> from nltk.stem import WordNetLemmatizer
>>> wnl = WordNetLemmatizer()
>>> wnl.lemmatize('heard', pos='v')
'hear'
问:那我如何获得正确的 POS 标签?!
可能使用 SpaCy,您会得到 ('heard', 'VERB'):
>>> import spacy
>>> nlp = spacy.load('en_core_web_sm')
>>> sent
'The laughs you two heard were triggered by memories of his own high j-flying moist moisture moisturize moisturizing.'
>>> doc = nlp(sent)
>>> [(word.text, word.pos_) for word in doc]
[('The', 'DET'), ('laughs', 'VERB'), ('you', 'PRON'), ('two', 'NUM'), ('heard', 'VERB'), ('were', 'VERB'), ('triggered', 'VERB'), ('by', 'ADP'), ('memories', 'NOUN'), ('of', 'ADP'), ('his', 'ADJ'), ('own', 'ADJ'), ('high', 'ADJ'), ('j', 'NOUN'), ('-', 'PUNCT'), ('flying', 'VERB'), ('moist', 'NOUN'), ('moisture', 'NOUN'), ('moisturize', 'NOUN'), ('moisturizing', 'NOUN'), ('.', 'PUNCT')]
但请注意,在这种情况下,SpaCy 得到了('moisturize', 'NOUN'),而 NLTK 得到了('moisturize', 'VB')。
问:但是我不能通过 SpaCy 获得 moisturize -> moist 吗?
让我们不要回到我们定义引理的开始。简而言之:
>>> import spacy
>>> nlp = spacy.load('en_core_web_sm')
>>> sent
'The laughs you two heard were triggered by memories of his own high j-flying moist moisture moisturize moisturizing.'
>>> doc = nlp(sent)
>>> [word.lemma_ for word in doc]
['the', 'laugh', '-PRON-', 'two', 'hear', 'be', 'trigger', 'by', 'memory', 'of', '-PRON-', 'own', 'high', 'j', '-', 'fly', 'moist', 'moisture', 'moisturize', 'moisturizing', '.']
另见How does spacy lemmatizer works?(再次)
问:好的,好的。我无法获得moisturize -> moist...而且POS 标签对于heard -> hear 来说并不完美。但是为什么我不能得到j-flying -> fly?
回到你为什么需要转换j-flying -> fly的问题,有一些反例说明你为什么不想分离看起来像一个化合物的东西。
例如:
- 应该
Classical-sounding 转到sound吗?
- 应该
X-fitting 转到fit 吗?
- 应该
crash-landing 转到landing吗?
取决于您的应用程序的最终目的是什么,可能需要也可能不需要将令牌转换为您想要的形式。
问:那有什么好办法提取有意义的词呢?
我听起来像是一张破唱片,但这取决于您的最终目标是什么?
如果你的目标是真正理解单词的含义,那么你必须问自己一个问题,“含义的含义是什么?”
单个单词是否具有脱离上下文的含义?或者它是否具有它可能出现的所有可能上下文的含义总和。
Au currant,最先进的技术基本上将所有含义视为浮点数组,并且浮点数组之间的比较赋予其含义。但这是真正的意义还是只是达到目的的手段? (双关语)。
问:为什么我得到的问题多于答案?
欢迎来到源自哲学(如计算机科学)的计算语言学世界。自然语言处理俗称计算语言学的应用
深思
问:词形还原器比词干分析器更好吗?
A:没有明确的答案。 (参考Stemmers vs Lemmatizers)