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

时间:2016-04-04 17:13:25

标签: python regex pos-tagger

When I use Brill Tagger, I get this error.

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

In sequential.py, I get error when it comes to for loop.

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

I run this same code a month ago and there was no error. sequential.py belongs to nltk files, does that means I should mess with it?

What have I done wrong? Please provide a fix if possible.

2 个答案:

答案 0 :(得分:3)

self._regexs is not an iterable object (like a list or a tuple). It is one compiled regular expression object.

Somewhere else in your code, you did something that effectively ends up doing something a lot like this:

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

It could be that you passed in a single item somewhere where the nltk API expected a sequence of such objects. I don't see any obvious way for the nltk code to have done this however.

答案 1 :(得分:-1)

解决方案非常简单。

我们只需要再次训练数据以获得新模型。

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