【问题标题】:NLTK Lemmatizer, Extract meaningful wordsNLTK Lemmatizer,提取有意义的词
【发布时间】:2018-09-18 19:44:59
【问题描述】:

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

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

有几个单词列表。

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

我编写了以下代码。 我引用了这个网址。 NLTK: lemmatizer and pos_tag

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

如果您还有其他提取有意义单词的好方法, 请告诉我。 谢谢

【问题讨论】:

  • moisture 有引理时,你为什么会期望moisture -> moist

标签: python-3.x nlp nltk lemmatization


【解决方案1】:

TL;DR

当您使用的 lemmatizer 是为了解决不同的问题时,这是一个 XY 问题,即 lemmatizer 未能满足您的期望。


长期

问:什么是引理?

语言学中的词形还原(或词形还原)是将单词的变形形式组合在一起的过程,以便可以将它们作为单个项目进行分析,由单词的词条或字典形式标识。 - Wikipedia

问:什么是“字典形式”?

NLTK 使用morphy 算法,该算法使用 WordNet 作为“字典形式”的基础

另见How does spacy lemmatizer works?。注意 SpaCy 有额外的技巧来处理更多不规则的单词。

问:为什么是moisture -> moisturemoisturizing -> 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

【讨论】:

    【解决方案2】:

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

    Spacy 是一个显而易见的 Python 评估选项。 斯坦福核心 nlp 是另一个(基于 JVM 和 GPL)。

    还有其他选择,没有一个是完美的。

    【讨论】:

      猜你喜欢
      • 2021-11-01
      • 2016-02-10
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多