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

时间:2015-08-26 10:32:10

标签: python nlp nltk pos-tagger

我正在使用NLTK POS tagger,如下所示

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,建议将这个词添加到词典中作为动词。 问题是我有太多这样的未知单词。 但有一条线索,它们总是出现在一个短语的开头。

例如:'立即下载','立即预订','注册'

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

2 个答案:

答案 0 :(得分:1)

您可以在NLTK中加载其他第三方模型。看看Python NLTK pos_tag not returning the correct part-of-speech tag

要用一些黑客来回答这个问题,你可以通过添加一个代词来欺骗POS标记,以便动词获得一个主题,例如

>>> 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')]

答案 1 :(得分:0)

https://spacy.io/usage/linguistic-features#pos-tagging处创建了一个名为spaCy的新库,它运行良好,

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