【问题标题】:CategorizedPlaintextCorpusReader: how to specify categories with regex? 'NoneType' object has no attribute 'group' errorCategorizedPlaintextCorpusReader:如何使用正则表达式指定类别? “NoneType”对象没有属性“组”错误
【发布时间】:2016-07-17 05:28:50
【问题描述】:

我正在尝试创建一个具有两个类别的 CategorizedPlaintextCorpusReader:neg 和 pos。这些类别在文件名中为“_neg”和“_pos”。例子:

bda_TD_2520_HD_001.pdf_neg.txt
info_Ei650_de.pdf_pos

我的代码:

reader = CategorizedPlaintextCorpusReader('C:/users/s/desktop/corpus/', r'.*\.txt',
cat_pattern=r'.*(pos|neg)\.txt')

我得到错误:

AttributeError: 'NoneType' object has no attribute 'group'

我在这里做错了什么?

编辑

我更改了它,现在不再出现原来的错误。不过,我不确定它是否有效,因为我没有得到任何结果:

len(reader.categories()) # nothing

for cat in reader.categories():
    print (cat) # nothing

reader.fileids("neg") # ValueError: Category neg not found

【问题讨论】:

    标签: python regex nltk


    【解决方案1】:

    您只需要查看包含posneg 的文件:

    CategorizedPlaintextCorpusReader('C:/users/s/desktop/corpus/', 
                                     r'.*?_(neg|pos).*', 
                                     cat_pattern=r'.*?_(neg|pos).*')
    

    其中.*? 是一个non-greedy 匹配任何字符任意次数,(neg|pos) 是一个捕获组(它必须被捕获才能使类别提取器工作)匹配negpos

    为我工作。

    【讨论】:

    • "@user3813234 快速检查 - .*?_(?:pos|neg)(?:\.txt)? 怎么样?
    • 不,仍然没有找到该类别。
    • @user3813234 为答案添加了一个可行的解决方案。当您检查它是否适合您时,我会添加一些解释。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2019-06-09
    • 2018-04-19
    • 1970-01-01
    • 2015-08-22
    • 2010-12-02
    • 2015-08-15
    • 1970-01-01
    • 2022-07-20
    相关资源
    最近更新 更多