python - 导入和使用 NLTK 语料库

标签 python nltk

请,请,请帮助。我有一个文件夹,里面装满了我想使用 NLTK 进行分析的文本文件。我如何将其导入为语料库,然后在其上运行 NLTK 命令?我已经将下面的代码放在一起,但它给了我这个错误:

    raise error, v # invalid expression
sre_constants.error: nothing to repeat

代码:

import nltk
import re
from nltk.corpus.reader.plaintext import PlaintextCorpusReader

corpus_root = '/Users/jt/Documents/Python/CRspeeches'
speeches = PlaintextCorpusReader(corpus_root, '*.txt')

print "Finished importing corpus" 

words = FreqDist()

for sentence in speeches.sents():
    for word in sentence:
        words.inc(word.lower())

print words["he"]
print words.freq("he")

最佳答案

我知道这个问题与已知的 bug 有关。 (也许这是一个功能?),这在 this answer 中有部分解释。 .简而言之,某些关于空事物的正则表达式会出错。

错误的来源是你的 speeches = 行。您应该将其更改为以下内容:

speeches = PlaintextCorpusReader(corpus_root, r'.*\.txt')

然后一切都会正常加载和编译。

关于python - 导入和使用 NLTK 语料库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26089483/

相关文章:

python - 根据谓词将值添加到新列

python - nltk pos 标记器看起来包含 '.'

python - django:对 **kwargs 使用 namedtuple 而不是 dict?

python - Python 中是否有类似 PHP 中的 getimagesize 的函数?

python - 将朴素贝叶斯分类器保存在内存中

python - 无法在 NLTK 中导入 - Python

python - 使用python从NLTK中提取名词短语

python - 识别文章中的实体

python - 使用最小/最有效路径(Scipy distance.cdist)3D 浏览坐标?

php - WampServer HTTP 403 禁止