请,请,请帮助。我有一个文件夹,里面装满了我想使用 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/