0

我编写了这个小脚本来查找我的语料库中 10 个最常用单词的上下文。但它不起作用,我不知道我做错了什么。 tien_frequentste(mijn_corpus) 定义独立工作。

tienfrequentste = tien_frequentste(mijncorpus)
def context (corpus, most_freq):
   for category in corpus.categories():
     print "Context voor" , category, ":"
       for word in most_freq:
           print nltk.Text(corpus.words(categories=category)).concordance(word)

更新:
我在回溯
for context(corpus, most_freq)
for category in corpus.categories()
forself.init()
in_init. 和一个AttributeError:'NoneType' object has no attribute 'group'
不知道这些错误是什么意思..

Traceback (most recent call last):
 File "/Users/...document.py", line 92, in <module> context (mijn_corpus, tienfrequentste)

 File "/Users/...document.py", line 87, in context for category in corpus.categories(): File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nltk.corpus.reader.api.py, line 317, in categories self.init().

File "/Users/...document.py", line 87, in context for category in corpus.categories(): File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nltk.corpus.reader.api.py, line 289, in_init category = re.match(self._pattern, file id).group(1)

attributeError: 'Nonetype' object has no attribute "group"
4

2 回答 2

0

该错误来自将语料库文件分配给类别的正则表达式。它绊倒了一个与正则表达式模式不匹配的文件名。如果您使用带有类别的标准 NLTK 语料库,您必须在语料库目录中放置一个额外的文件。如果您使用自己的语料库,则说明配置错误。

顺便说一句,concordance()打印它的输出并返回None。如果你使用print它,你会看到一大堆None值。

于 2013-02-03T21:48:00.850 回答
0

您的语料库是否有类别并且是most_freq字符串列表?以下示例有效:

from nltk.corpus import reuters
for category in reuters.categories():
print "context voor", category, " : "
for word in ["get", "have", "do"]:
    print nltk.Text(reuters.words(categories=category)).concordance(word)
于 2013-01-27T23:24:31.937 回答