NLTK Lemmatizer,提取有意义的词

NLTK Lemmatizer, Extract meaningful words

目前,我将创建一个基于机器学习的自动映射类别的代码。

在那之前我要做自然语言处理

有几个单词列表。

      sent ='The laughs you two heard were triggered 
             by memories of his own high j-flying 
             moist moisture moisturize moisturizing '.lower().split()

我做了以下代码。 我引用了这个 url。

from nltk.tag import pos_tag
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
def lemmatize_all(sentence):
    wnl = WordNetLemmatizer()
    for word, tag in pos_tag(word_tokenize(sentence)):
        if tag.startswith("NN"):
            yield wnl.lemmatize(word, pos='n')
        elif tag.startswith('VB'):
            yield wnl.lemmatize(word, pos='v')
        elif tag.startswith('JJ'):
            yield wnl.lemmatize(word, pos='a')



words = ' '.join(lemmatize_all(' '.join(sent)))

结果值如下所示。

laugh heard be trigger memory own high j-flying moist moisture moisturize moisturizing

我对以下结果感到满意。

laughs -> laugh 
were -> be
triggered -> trigger 
memories -> memory 
moist -> moist 

但是,不满足以下值。

heard -> heard 
j-flying -> j-flying 
moisture -> moisture 
moisturize -> moisturize 
moisturizing -> moisturizing 

虽然比初始值好,但我想要以下结果。

heard -> hear
j-flying -> fly
moisture -> moist
moisturize -> moist
moisturizing -> moist

如果你有其他好的提取有意义的词的方法, 请让我知道。 谢谢

词形还原不是一件容易的事。你不应该期待完美的结果。然而,Yiu 可以看看您是否更喜欢其他词形还原库的结果。

Spacy 是一个显而易见的 Python 评估选项。 Stanford core nlp 是另一个(基于 JVM 和 GPLed)。

还有其他选择,none会更完美。

TL;DR

当您使用的词形还原器用于解决不同的问题时,这是一个词形还原器未能满足您的期望的 XY 问题。


中龙

问:什么是引理?

Lemmatisation (or lemmatization) in linguistics is the process of grouping together the inflected forms of a word so they can be analysed as a single item, identified by the word's lemma, or dictionary form. - Wikipedia

问:"dictionary form"是什么?

NLTK 使用 morphy 算法,该算法使用 WordNet 作为 "dictionary forms"

的基础

另见 。注意 SpaCy 有额外的技巧来处理更多不规则的单词。

问:为什么 moisture -> moisturemoisturizing -> moisturizing

因为 "moisture" 和 "moisturizing"

有同义词集(类似于 "dictionary form")
>>> 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 太荒谬了?!

是的,词性标注器没有正确标注听力。很可能是因为这个句子不是一个正确的句子,所以句子中的单词的 POS 标签是错误的:

>>> 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', '.']

另见 (再次)

问:好的,好的。我无法获得 moisturize -> moist...而且 POS 标签不适合 heard -> hear。但是为什么我获取不到j-flying -> fly?

回到 为什么需要转换 j-flying -> fly 的问题,有一些反例说明为什么您不想分离看起来像化合物。

例如:

  • 应该Classical-soundingsound吗?
  • 应该X-fittingfit吗?
  • 应该crash-landinglanding吗?

取决于您应用程序的最终目的,将令牌转换为您想要的形式可能是必要的,也可能不是必要的。

问:那么提取有意义的词有什么好的方法吗?

我听起来像是破纪录,但这取决于您的最终目标是什么?

如果你的目标真的是理解单词的意思那么你必须问自己这个问题,"What is the meaning of meaning?"

单个单词脱离其上下文是否有意义?或者它是否具有它可能出现的所有可能上下文的含义总和。

Au currant,最先进的技术基本上将所有含义都视为浮点数数组,浮点数数组之间的比较就是赋予意义的意义。但这真的有意义还是只是达到目的的一种手段? (双关语)。

问:为什么我收到的问题多于答案?

欢迎来到源于哲学(如计算机科学)的计算语言学世界。自然语言处理俗称计算语言学的应用


深思

问:词形还原器比词干提取器好吗?

答:没有确定的答案。 (c.f.Stemmers vs Lemmatizers)