Python NLTK pos_tag 未返回正确的词性标记

标签 python machine-learning nlp nltk pos-tagger

有这个:

text = word_tokenize("The quick brown fox jumps over the lazy dog")

并运行:

nltk.pos_tag(text)

我明白了:

[('The', 'DT'), ('quick', 'NN'), ('brown', 'NN'), ('fox', 'NN'), ('jumps', 'NNS'), ('over', 'IN'), ('the', 'DT'), ('lazy', 'NN'), ('dog', 'NN')]

这是不正确的。句子中quick brown lazy的标签应该是:

('quick', 'JJ'), ('brown', 'JJ') , ('lazy', 'JJ')

通过他们的online tool 进行测试给出相同的结果; quickbrownfox 应该是形容词而不是名词。

最佳答案

简而言之:

NLTK is not perfect. In fact, no model is perfect.

注意:

从 NLTK 3.1 版开始,默认的 pos_tag 函数不再是 old MaxEnt English pickle .

现在是 @Honnibal's implementation 中的 感知器标记器 ,见 nltk.tag.pos_tag

>>> import inspect
>>> print inspect.getsource(pos_tag)
def pos_tag(tokens, tagset=None):
    tagger = PerceptronTagger()
    return _pos_tag(tokens, tagset, tagger) 

仍然更好但并不完美:

>>> from nltk import pos_tag
>>> pos_tag("The quick brown fox jumps over the lazy dog".split())
[('The', 'DT'), ('quick', 'JJ'), ('brown', 'NN'), ('fox', 'NN'), ('jumps', 'VBZ'), ('over', 'IN'), ('the', 'DT'), ('lazy', 'JJ'), ('dog', 'NN')]

在某些时候,如果有人想要 TL;DR 解决方案,请参阅 https://github.com/alvations/nltk_cli


长期:

尝试使用其他标记器(参见 https://github.com/nltk/nltk/tree/develop/nltk/tag),例如:

  • HunPos
  • 斯坦福 POS
  • 塞纳

使用来自 NLTK 的默认 MaxEnt POS 标记器,即 nltk.pos_tag:

>>> from nltk import word_tokenize, pos_tag
>>> text = "The quick brown fox jumps over the lazy dog"
>>> pos_tag(word_tokenize(text))
[('The', 'DT'), ('quick', 'NN'), ('brown', 'NN'), ('fox', 'NN'), ('jumps', 'NNS'), ('over', 'IN'), ('the', 'DT'), ('lazy', 'NN'), ('dog', 'NN')]

使用斯坦福词性标注器:

$ cd ~
$ wget http://nlp.stanford.edu/software/stanford-postagger-2015-04-20.zip
$ unzip stanford-postagger-2015-04-20.zip
$ mv stanford-postagger-2015-04-20 stanford-postagger
$ python
>>> from os.path import expanduser
>>> home = expanduser("~")
>>> from nltk.tag.stanford import POSTagger
>>> _path_to_model = home + '/stanford-postagger/models/english-bidirectional-distsim.tagger'
>>> _path_to_jar = home + '/stanford-postagger/stanford-postagger.jar'
>>> st = POSTagger(path_to_model=_path_to_model, path_to_jar=_path_to_jar)
>>> text = "The quick brown fox jumps over the lazy dog"
>>> st.tag(text.split())
[(u'The', u'DT'), (u'quick', u'JJ'), (u'brown', u'JJ'), (u'fox', u'NN'), (u'jumps', u'VBZ'), (u'over', u'IN'), (u'the', u'DT'), (u'lazy', u'JJ'), (u'dog', u'NN')]

使用 HunPOS(注意:默认编码是 ISO-8859-1 而不是 UTF8):

$ cd ~
$ wget https://hunpos.googlecode.com/files/hunpos-1.0-linux.tgz
$ tar zxvf hunpos-1.0-linux.tgz
$ wget https://hunpos.googlecode.com/files/en_wsj.model.gz
$ gzip -d en_wsj.model.gz 
$ mv en_wsj.model hunpos-1.0-linux/
$ python
>>> from os.path import expanduser
>>> home = expanduser("~")
>>> from nltk.tag.hunpos import HunposTagger
>>> _path_to_bin = home + '/hunpos-1.0-linux/hunpos-tag'
>>> _path_to_model = home + '/hunpos-1.0-linux/en_wsj.model'
>>> ht = HunposTagger(path_to_model=_path_to_model, path_to_bin=_path_to_bin)
>>> text = "The quick brown fox jumps over the lazy dog"
>>> ht.tag(text.split())
[('The', 'DT'), ('quick', 'JJ'), ('brown', 'JJ'), ('fox', 'NN'), ('jumps', 'NNS'), ('over', 'IN'), ('the', 'DT'), ('lazy', 'JJ'), ('dog', 'NN')]

使用 Senna(确保您拥有最新版本的 NLTK,对 API 进行了一些更改):

$ cd ~
$ wget http://ronan.collobert.com/senna/senna-v3.0.tgz
$ tar zxvf senna-v3.0.tgz
$ python
>>> from os.path import expanduser
>>> home = expanduser("~")
>>> from nltk.tag.senna import SennaTagger
>>> st = SennaTagger(home+'/senna')
>>> text = "The quick brown fox jumps over the lazy dog"
>>> st.tag(text.split())
[('The', u'DT'), ('quick', u'JJ'), ('brown', u'JJ'), ('fox', u'NN'), ('jumps', u'VBZ'), ('over', u'IN'), ('the', u'DT'), ('lazy', u'JJ'), ('dog', u'NN')]

或者尝试构建一个更好的词性标注器:


提示 pos_tag 在 stackoverflow 上的准确性包括:

关于 NLTK HunPos 的问题包括:

NLTK 和斯坦福词性标注器的问题包括:

关于Python NLTK pos_tag 未返回正确的词性标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30821188/

相关文章:

nlp - Milvus 在 150K 条记录时 CPU 索引速度非常慢

python - SciKit Learn、Keras 或 Pytorch 的差异

python - 我怎样才能使用 SQLAlchemy 做 "mysql explain"

python - 警告 : xcodeproj is not installed or is not configured properly

python - 使用python将视频文件拆分为一组部分

machine-learning - 如何识别归一化特征

machine-learning - 什么是 f1-score 以及它的值表示什么?

python - 处理语料库以实现 word2vec

r - 包 tm 停止字参数

Python:如何使用出现特定次数的python列表中的项目?