Nltk 条件频率分布问题

Nltk 条件频率分布问题,nltk,Nltk,我有以下代码: corpus= CategorizedPlaintextCorpusReader(loc,'(?!\.svn).*\.txt', cat_pattern=r'(Shakespeare|Milton)/.*') cfd=nltk.ConditionalFreqDist ((genre,word) for genre in corpus.categories() for w

我有以下代码:

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)
现在,出于某种强烈的perculiar原因,我得到了以下错误: “category=re.match(自我模式,文件id).group(1) AttributeError:“非类型”对象没有属性“组”

有人知道这是怎么回事吗

category = re.match(self._pattern, file_id).group(1) 
AttributeError: 'NoneType' object has no attribute 'group'
此错误消息告诉您,
re.match
返回了
None
。换句话说,没有对手。当您查找组(1)时,它会抛出错误

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

  • 简化匹配命令

  • 防御性的。首先使用
    if
    检查是否存在
    匹配项
    ,然后查找其组

  • 通常,当您遇到这样的错误时,请继续简化代码,以查看是什么导致了问题。Python可以用非常简洁的方式编写,但我发现在学习时最好更具描述性

  • 这会给你更多的选择

    希望这能帮助你前进

    此错误消息告诉您,
    re.match
    返回了
    None
    。换句话说,没有对手。当您查找组(1)时,它会抛出错误

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

  • 简化匹配命令

  • 防御性的。首先使用
    if
    检查是否存在
    匹配项
    ,然后查找其组

  • 通常,当您遇到这样的错误时,请继续简化代码,以查看是什么导致了问题。Python可以用非常简洁的方式编写,但我发现在学习时最好更具描述性

  • 这会给你更多的选择

    希望这能帮助你前进