NLTK自定义分类语料库不读取文件

2024-09-27 07:27:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了自己的语料库,类似于nltk中的电影评论语料库(按neg | pos分类)

在neg和pos文件夹中是txt文件。你知道吗

代码:

from nltk.corpus import CategorizedPlaintextCorpusReader

    mr = CategorizedPlaintextCorpusReader('C:\mycorpus', r'(?!\.).*\.txt',
            cat_pattern=r'(neg|pos)/.*')

当我尝试读取或与其中一个文件交互时,我无法。你知道吗

例如len(mr.categories())运行,但不返回任何内容:

>>>

我已经阅读了很多关于自定义分类语料库的文档和问题,但是我仍然无法使用它们。你知道吗

完整代码:

import nltk
from nltk.corpus import CategorizedPlaintextCorpusReader

mr = CategorizedPlaintextCorpusReader('C:\mycorpus', r'(?!\.).*\.txt',
        cat_pattern=r'(neg|pos)/.*')

len(mr.categories())

我最终希望能够对我的数据执行一个朴素的贝叶斯算法,但我无法读取内容。你知道吗

路径: C:\mycorpus\pos

C:\mycorpus\neg

pos文件中有一个'简历.txt'负片包含'示例.txt'


Tags: 文件代码fromposimporttxt分类corpus
2条回答

我觉得你的头发有点奇怪

cat_pattern=r'(neg|pos)/.*'

因为您使用的是基于MsDOS的系统(我猜是Windows),文件夹包含用\,而不是/(或者我不明白)

我使用的是Linux,以下对代码的修改(使用玩具语料库文件)对我来说是正确的:

import nltk
from nltk.corpus import CategorizedPlaintextCorpusReader

import os


mr = CategorizedPlaintextCorpusReader(
    '/home/ely/programming/nltk-test/mycorpus',
    r'(?!\.).*\.txt',
    cat_pattern=os.path.join(r'(neg|pos)', '.*')
)

print(len(mr.categories()))

这表明在Windows系统上使用cat_pattern作为文件系统分隔符的/字符串有问题。你知道吗

在我的示例中使用os.path.join,或者如果使用python3使用pathlib,这将是一个很好的解决方法,因此它不受操作系统的影响,并且不会出现与文件系统分隔符混合的正则表达式转义斜杠。你知道吗

事实上,对于参数字符串中所有文件系统分隔符的情况,您都可以使用这种方法,而且这通常是一个很好的习惯,可以使代码具有可移植性,避免使用奇怪的字符串。你知道吗

相关问题 更多 >

    热门问题