首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导入和使用NLTK语料库

导入和使用NLTK语料库
EN

Stack Overflow用户
提问于 2014-09-28 20:44:54
回答 1查看 2.4K关注 0票数 3

拜托,拜托,帮帮忙。我有一个包含文本文件的文件夹,我想使用NLTK进行分析。如何将其作为语料库导入,然后在其上运行NLTK命令?我已经将下面的代码组合在一起,但是它给了我一个错误:

代码语言:javascript
复制
    raise error, v # invalid expression
sre_constants.error: nothing to repeat

代码:

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-28 22:10:16

我理解这个问题与已知的错误有关(也许它是一个特性?),这一点在这个答案中有部分解释。简而言之,某些关于空洞事物的规则被炸毁了。

错误的来源是您的speeches =行。您应该将其更改为:

代码语言:javascript
复制
speeches = PlaintextCorpusReader(corpus_root, r'.*\.txt')

然后,所有的东西都会加载并编译得很好。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26089483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档