NLTK 将命令式中的动词识别为名词

NLTK identifies verb as Noun in Imperatives

我正在使用 NLTK 词性标注器,如下所示

sent1='get me now'
sent2='run fast'
tags=pos_tag(word_tokenize(sent2))
print tags
[('run', 'NN'), ('fast', 'VBD')]

我发现类似的帖子 NLTK Thinks that Imperatives are Nouns 建议将该词作为动词添加到词典中。 问题是我有太多这样的未知词。 但我有一个线索,它们总是出现在短语的开头。

例如:'Download now'、'Book it now'、'Sign up'

如何正确协助NLTK产生正确的结果

您可以在 NLTK 中加载其他第三方模型。看看


要用一些技巧来回答问题,您可以通过添加代词来欺骗词性标注器,这样动词就有主语了,例如

>>> from nltk import pos_tag
>>> sent1 = 'get me now'.split()
>>> sent2 = 'run fast'.split()
>>> pos_tag(['He'] + sent1)
[('He', 'PRP'), ('get', 'VBD'), ('me', 'PRP'), ('now', 'RB')]
>>> pos_tag(['He'] + sent1)[1:]
[('get', 'VBD'), ('me', 'PRP'), ('now', 'RB')]

要使答案功能化:

>>> from nltk import pos_tag
>>> sent1 = 'get me now'.split()
>>> sent2 = 'run fast'.split()
>>> def imperative_pos_tag(sent):
...     return pos_tag(['He']+sent)[1:]
... 
>>> imperative_pos_tag(sent1)
[('get', 'VBD'), ('me', 'PRP'), ('now', 'RB')]
>>> imperative_pos_tag(sent2)
[('run', 'VBP'), ('fast', 'RB')]

如果您希望祈使句中的所有动词都接收基本形式 VB 标签:

>>> from nltk import pos_tag
>>> sent1 = 'get me now'.split()
>>> sent2 = 'run fast'.split()
>>> def imperative_pos_tag(sent):
...     return [(word, tag[:2]) if tag.startswith('VB') else (word,tag) for word, tag in pos_tag(['He']+sent)[1:]]
... 
>>> imperative_pos_tag(sent1)
[('get', 'VB'), ('me', 'PRP'), ('now', 'RB')]
>>> imperative_pos_tag(sent2)
[('run', 'VB'), ('fast', 'RB')]

在这里找到了这个名为 spaCy 的新库 https://spacy.io/usage/linguistic-features#pos-tagging,它运行良好,

import spacy
nlp = spacy.load("en_core_web_sm")
text = ("run fast")
doc = nlp(text)
verbs = [(token, token.tag_) for token in doc]
print(verbs)

输出:

[('run', 'VB'), ('fast', 'RB')]

安装指南:https://spacy.io/usage