NLTK自定义POS标记(错误)

2024-06-08 15:47:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试将我自己的简单自定义标记器与nltk默认标记器相结合,在本例中是感知器标记器。在

我的代码如下(基于this answer):

import nltk.tag, nltk.data

default_tagger = nltk.data.load(nltk.tag._POS_TAGGER)
model = {'example_one': 'VB' 'example_two': 'NN'}
tagger = nltk.tag.UnigramTagger(model=model, backoff=default_tagger)

但是,这会产生以下错误:

^{pr2}$

我试图通过将默认标记符更改为:

from nltk.tag.perceptron import PerceptronTagger
default_tagger = PerceptronTagger()

但是我得到了以下错误:

  File "nltk_test.py", line 26, in <module>
    tagger = nltk.tag.UnigramTagger(model=model, backoff=default_tagger)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/nltk/tag/sequential.py", line 340, in __init__
    backoff, cutoff, verbose)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/nltk/tag/sequential.py", line 284, in __init__
    ContextTagger.__init__(self, model, backoff)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/nltk/tag/sequential.py", line 125, in __init__
    SequentialBackoffTagger.__init__(self, backoff)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/nltk/tag/sequential.py", line 50, in __init__
    self._taggers = [self] + backoff._taggers
AttributeError: 'PerceptronTagger' object has no attribute '_taggers'

纵观nltk.tagdocumentation,似乎{}已经不存在了。但是,将其更改为_pos_tag或{}也没有起作用。在


Tags: inpy标记defaultmodelinittagline
2条回答

快速回答:现在使用nltk3.0.1pip install nltk==3.0.1

更好的答案是:他们去年9月改变了treebank标签,它有很多其他的影响(我们目前固定在3.0.1上,因为新标签至少对我们的需求更差)。在

这似乎有效,但我不确定代码的正确性:

class BackoffTagger:
    def __init__(self):
        self._taggers = [PerceptronTagger()]

model = {'example_one': 'VB', 'example_two': 'NN'}
tagger = nltk.tag.UnigramTagger(model=model, backoff=BackoffTagger())
tagger.tag(['example_one'])
>>> [('example_one', 'VB')]

尝试以下自定义标记:

import nltk.tag, nltk.data
from nltk.tag.perceptron import PerceptronTagger
default_tagger = PerceptronTagger()

使用自定义标记定义模型:

^{pr2}$

输出

[('skin', 'site')]

相关问题 更多 >