1

いくつかの 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()

受け取ったエラー (完全なトレースバック):

In [6]: ---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-6-abc9ff8cb2f1> in <module>()
----> 1 execfile(r'/Dropbox/hearings/hearings_ingest.py') # PYTHON-MODE

/Dropbox/hearings/hearings_ingest.py in <module>()
     14 cfd = nltk.ConditionalFreqDist(
     15     (target, fileid[:3])
---> 16      for fileid in text.fileids()
     17      for w in text.words(fileid)
     18      for target in ['budget','appropriat']

/Users/ian/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/nltk/probability.pyc in __init__(self, cond_samples)
   1727         defaultdict.__init__(self, FreqDist)
   1728         if cond_samples:
-> 1729             for (cond, sample) in cond_samples:
   1730                 self[cond].inc(sample)
   1731 

/Dropbox/hearings/hearings_ingest.py in <genexpr>((fileid,))
     15     (target, fileid[:3])
     16      for fileid in text.fileids()
---> 17      for w in text.words(fileid)
     18      for target in ['budget','appropriat']
     19      if w.lower().startswith(target))

/Users/ian/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/nltk/corpus/reader/util.pyc in iterate_from(self, start_tok)
    341 
    342         # If we reach this point, then we should know our length.
--> 343         assert self._len is not None
    344 
    345     # Use concat for these, so we can use a ConcatenatedCorpusView

AssertionError: 

In [7]: 

これが完全なエラーであることを示すために、新しい IPython 行を含めます。(他の質問を読むと、「AssertionError:」の後に多くの情報が続くことがわかります。私のエラーでは空白です。)

私のコードのエラーを理解する上で助けていただければ幸いです! ありがとう!

4

1 に答える 1

1

空のファイルを作成してfooから呼び出すことで、エラーを再現できtext.words('foo')ます。

In [18]: !touch 'foo'

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

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

したがって、空のファイルを回避するには、次のようにします。

cfd = nltk.ConditionalFreqDist(
    (target, fileid[:3])
    for fileid in text.fileids()
    if os.path.getsize(fileid) > 0   # check the filesize is not 0
    for w in text.words(fileid)
    for target in ['budget', 'appropriat']
    if w.lower().startswith(target))
于 2013-07-08T17:39:23.770 に答える