【问题标题】:Issue with conditional frequency distribution条件频率分布问题
【发布时间】:2013-01-25 16:08:07
【问题描述】:

我有以下代码:

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)

现在,由于一些非常奇怪的原因,我收到以下错误: “类别 = re.match(self._pattern, file_id).group(1) AttributeError: 'NoneType' 对象没有属性 'group'"

有人知道那是什么意思吗?

【问题讨论】:

    标签: nltk


    【解决方案1】:
    category = re.match(self._pattern, file_id).group(1) 
    AttributeError: 'NoneType' object has no attribute 'group'
    

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

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

    1. 简化匹配命令。

    2. 保持防御。首先使用if 来检查是否有match 然后查找它的组。

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

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

    希望能帮助你前进。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-19
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 2011-02-05
      • 1970-01-01
      相关资源
      最近更新 更多