【问题标题】:WordNetLemmatizer not returning the right lemma unless POS is explicit - Python NLTK除非 POS 是明确的,否则 WordNetLemmatizer 不会返回正确的引理 - Python NLTK
【发布时间】:2016-01-02 15:47:52
【问题描述】:

我正在对 Ted 数据集成绩单进行词形还原。我注意到一些奇怪的事情: 并非所有单词都被词形还原。话说,

selected -> select

这是对的。

但是,involved !-> involvehorsing !-> horse 除非我明确输入“v”(动词)属性。

在 python 终端上,我得到了正确的输出,但在我的 code 中却没有:

>>> from nltk.stem import WordNetLemmatizer
>>> from nltk.corpus import wordnet
>>> lem = WordNetLemmatizer()
>>> lem.lemmatize('involved','v')
u'involve'
>>> lem.lemmatize('horsing','v')
u'horse'

代码的相关部分是这样的:

for l in LDA_Row[0].split('+'):
    w=str(l.split('*')[1])
    word=lmtzr.lemmatize(w)
    wordv=lmtzr.lemmatize(w,'v')
    print wordv, word
    # if word is not wordv:
    #   print word, wordv

整个代码是here

有什么问题?

【问题讨论】:

  • 代码在没有安装的情况下无法工作......你能提取输入吗,例如LDA_Row 长什么样子?
  • 这是因为您的 POS 标签错误。 P/S:下一次,请尽量不要发布完整的代码,而是在解释问题的代码中表示 sn-ps,否则 Stackoverflow 用户可能会尝试关闭问题,说“问题不清楚”或者这是一个“我的代码不起作用”问题 =)

标签: python nlp nltk wordnet lemmatization


【解决方案1】:

lemmatizer需要正确的词性标签才能准确,如果使用WordNetLemmatizer.lemmatize()的默认设置,默认标签是名词,见https://github.com/nltk/nltk/blob/develop/nltk/stem/wordnet.py#L39

要解决问题,请始终在词形还原之前对您的数据进行 POS 标记,例如

>>> from nltk.stem import WordNetLemmatizer
>>> from nltk import pos_tag, word_tokenize
>>> wnl = WordNetLemmatizer()
>>> sent = 'This is a foo bar sentence'
>>> pos_tag(word_tokenize(sent))
[('This', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('foo', 'NN'), ('bar', 'NN'), ('sentence', 'NN')]
>>> for word, tag in pos_tag(word_tokenize(sent)):
...     wntag = tag[0].lower()
...     wntag = wntag if wntag in ['a', 'r', 'n', 'v'] else None
...     if not wntag:
...             lemma = word
...     else:
...             lemma = wnl.lemmatize(word, wntag)
...     print lemma
... 
This
be
a
foo
bar
sentence

注意'is -> be',即

>>> wnl.lemmatize('is')
'is'
>>> wnl.lemmatize('is', 'v')
u'be'

用你的例子回答问题:

>>> sent = 'These sentences involves some horsing around'
>>> for word, tag in pos_tag(word_tokenize(sent)):
...     wntag = tag[0].lower()
...     wntag = wntag if wntag in ['a', 'r', 'n', 'v'] else None
...     lemma = wnl.lemmatize(word, wntag) if wntag else word
...     print lemma
... 
These
sentence
involve
some
horse
around

请注意,WordNetLemmatizer 有一些怪癖:

此外,NLTK 的默认词性标注器正在进行一些重大更改以提高准确性:

对于 lemmatizer 的开箱即用/现成解决方案,您可以查看 https://github.com/alvations/pywsd 以及我如何制作一些 try-excepts 来捕捉 WordNet 中没有的单词,见https://github.com/alvations/pywsd/blob/master/pywsd/utils.py#L66

【讨论】:

  • 帮了大忙,谢谢!
猜你喜欢
  • 1970-01-01
  • 2022-12-23
  • 1970-01-01
  • 2017-06-21
  • 2015-08-29
  • 1970-01-01
相关资源
最近更新 更多