1

我有以下代码:

corpus= CategorizedPlaintextCorpusReader(loc,'(?!\.svn).*\.txt', cat_pattern=r'(Shakespeare|Milton)/.*')
    cfd=nltk.ConditionalFreqDist ((genre,word)
                          for genre in corpus.categories()
                          for word in corpus.words(categories = genre))
    genres=['Shakespeare','Milton']
    pronouns=['I','you','he','she', 'it','we','they']

    cfd.tabulate (conditions=genres,samples=pronouns)

现在,由于一些非常奇怪的原因,我收到以下错误:“category = re.match(self._pattern, file_id).group(1) AttributeError: 'NoneType' object has no attribute 'group'”

有人知道那是什么吗?

4

1 回答 1

1
category = re.match(self._pattern, file_id).group(1) 
AttributeError: 'NoneType' object has no attribute 'group'

此错误消息告诉您re.match返回的None. 换句话说,没有匹配。当您查找 group(1) 时,它会引发错误。

在前进方面,您有几个选择:

  1. 简化匹配命令。

  2. 保持防守。首先使用 anif检查是否有 amatch然后查找它的组。

  3. 通常,当您遇到此类错误时,请继续简化代码以查看导致问题的原因。Python 可以用非常简洁的方式编写,但我发现在学习时更具描述性会更好。

这个SO question应该给你更多的选择。

希望能帮助你前进。

于 2013-01-26T19:30:11.963 回答