我是 python 编码的新手。我想使用 UnigramTagger 和退避(在我的情况下是 RegexpTagger),我一直在努力弄清楚下面的错误是什么。感谢您对此的任何帮助。
>>> train_sents = (['@Sakshi', 'Hi', 'I', 'am', 'meeting', 'my', 'friend', 'today'])
>>> from tag_util import patterns
>>> from nltk.tag import RegexpTagger
>>> re_tagger = RegexpTagger(patterns)
>>> from nltk.tag import UnigramTagger
>>> from tag_util import backoff_tagger
>>> tagger = backoff_tagger(train_sents, UnigramTagger, backoff=re_tagger)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
tagger = backoff_tagger(train_sents, UnigramTagger, backoff=re_tagger)
File "tag_util.py", line 12, in backoff_tagger
for cls in tagger_classes:
TypeError: 'YAMLObjectMetaclass' object is not iterable
这是我在 tag_util 中用于模式和 backoff_tagger 的代码
import re
patterns = [
(r'^@\w+', 'NNP'),
(r'^\d+$', 'CD'),
(r'.*ing$', 'VBG'), # gerunds, i.e. wondering
(r'.*ment$', 'NN'),
(r'.*ful$', 'JJ'), # i.e. wonderful
(r'.*', 'NN')
]
def backoff_tagger(train_sents, tagger_classes, backoff=None):
for cls in tagger_classes:
backoff = cls(train_sents, backoff=backoff)
return backoff