1

I'm running a code to get the perplexity, number of ngrams from a text corpus. While doing it, I got a weird error saying:

C:\Users\Rosenkrantz\Documents\NetBeansProjects\JavaApplication2>python ai7.py
C:\Users\Rosenkrantz\Documents\NetBeansProjects\JavaApplication2>python ai7.py
47510
203044
308837
Traceback (most recent call last):
  File "ai7.py", line 95, in <module>
    tt=NgramModel(1, tText, estimator)
  File "C:\Python27\lib\site-packages\nltk\model\ngram.py", line 81, in __init__
    assert(isinstance(pad_left, bool))
AssertionError

The Code I am Running to get this is:

f_in = open("science.txt", 'r');
ln = f_in.read()
words = nltk.word_tokenize(ln)
tText = Text(words)
tt=NgramModel(1, tText, estimator)
tt1=NgramModel(2, tText1, estimator)
tt2=NgramModel(3, tText2, estimator)

All the imports seem to be proper.

4

1 回答 1

3

你确定你NgramModel用正确的论据打电话吗?查看当前版本的 NLTK 的源代码NgramModel如下所示:

def __init__(self, n, train, pad_left=True, pad_right=False,
                 estimator=None, *estimator_args, **estimator_kwargs): 

这似乎与您调用该函数的方式不匹配。你estimator的代码中有什么?因为您当前正在estimator作为pad_left参数传递。

于 2012-11-22T00:50:45.153 回答