'_sre.SRE_Pattern' 对象在 Python 3.4 中不可迭代

'_sre.SRE_Pattern' object is not iterable in Python 3.4

当我使用 Brill Tagger 时,出现此错误。

TypeError: '_sre.SRE_Pattern' object is not iterable
WARNING:root:2016-04-05 00:05:37.503718 is when this event was logged.
ERROR:root:'_sre.SRE_Pattern' object is not iterable
Traceback (most recent call last):
  File "D:\Dropbox\VCL\MyWrapper.py", line 137, in run_alg
    CLC_POS.tag_file(input_utf8, path_out + '.pos', file_encoding, CLC_POS.load_tagger('pos_tbl_86943.model'), '')
  File "D:\Dropbox\VCL\CLC_POS.py", line 277, in tag_file
    token_tag = tagger.tag(word_list)
  File "C:\Python34\lib\site-packages\nltk\tag\brill.py", line 264, in tag
    tagged_tokens = self._initial_tagger.tag(tokens)
  File "C:\Python34\lib\site-packages\nltk\tag\sequential.py", line 61, in tag
    tags.append(self.tag_one(tokens, i, tags))
  File "C:\Python34\lib\site-packages\nltk\tag\sequential.py", line 81, in tag_one
    tag = tagger.choose_tag(tokens, index, history)
  File "C:\Python34\lib\site-packages\nltk\tag\sequential.py", line 546, in choose_tag
    for regexp, tag in self._regexs:
TypeError: '_sre.SRE_Pattern' object is not iterable

在 sequential.py 中,当涉及到 for 循环时出现错误。

def choose_tag(self, tokens, index, history):
    for regexp, tag in self._regexs:
        if re.match(regexp, tokens[index]):
            return tag
    return None

我 运行 一个月前用过同样的代码,没有错误。 sequential.py 属于 nltk 文件,这是否意味着我应该弄乱它?

我做错了什么?如果可能,请提供修复。

self._regexs 不是可迭代对象(如列表或元组)。它是一个编译的正则表达式对象。

在您的代码中的其他地方,您所做的事情实际上最终会做很多类似这样的事情:

self._regexs = re.compile(r'...')

可能是您在 nltk API 需要一系列此类对象的地方传入了一个项目。然而,我没有看到 nltk 代码有任何明显的方法可以做到这一点。

解决方法很简单。

我们只需要再次训练数据就可以得到新的模型。

我认为问题是 NLTK 中的一些变化,但不确定它在哪里。此外,这只影响 Brill Tagger,不影响 CRF Tagger。