我正在尝试将我自己的简单自定义标记器与nltk默认标记器结合使用,在本例中为perceptron标记器.
我的代码如下(基于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)
但是,这会产生以下错误:
File "nltk_test.py", line 24, in <module>
default_tagger = nltk.data.load(nltk.tag._POS_TAGGER)
AttributeError: 'module' object has no attribute '_POS_TAGGER'
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.tag documentation查看似乎_POS_TAGGER不再存在.但是将其更改为_pos_tag或pos_tag也不起作用.
解决方法:
快速回答:现在使用nltk 3.0.1 pip install nltk == 3.0.1
更好的答案:他们去年9月更改了树库标记器并且还有很多其他后果(我们目前固定在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')]