我创建了自己的语料库,类似于 nltk 中的 movie_reviews 语料库(按 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 文件中包含一个“cv.txt”,而 neg 包含一个“example.txt”