期间出现python断言错误nltk.条件请求分布

2024-06-25 23:45:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试执行一些python代码时收到了一个我不理解的错误。我试图通过优秀的NLTK教科书学习使用自然语言工具箱。在尝试下面的代码(对图2.1中我自己的数据进行了修改)时,我收到了下面的错误。在

我运行的代码:

import os, re, csv, string, operator
import nltk
from nltk.corpus import PlaintextCorpusReader
dir = '/Dropbox/hearings'

corpus_root = dir
text = PlaintextCorpusReader(corpus_root, ".*")

cfd = nltk.ConditionalFreqDist(
    (target, fileid[:3])
     for fileid in text.fileids()
     for w in text.words(fileid)
     for target in ['budget','appropriat']
     if w.lower().startswith(target))

cfd.plot()

收到错误(完全回溯):

^{pr2}$

我加入了新的IPython行来说明这是完全错误。(在阅读其他问题时,我发现“AssertionError:”后面往往会有更多的信息。在我的错误中,它是空白的。)

如果您能帮助我理解代码中的错误,我将不胜感激!谢谢!在


Tags: 代码textinimporttargetfor错误dir
1条回答
网友
1楼 · 发布于 2024-06-25 23:45:38

我可以通过创建一个空文件foo,然后调用text.words('foo')来重现错误:

In [18]: !touch 'foo'

In [19]: text = corpus.PlaintextCorpusReader('.', "foo")

In [20]: text.words('foo')
AssertionError:

为了避免出现空文件,可以执行以下操作:

^{pr2}$

相关问题 更多 >