Python 使用nltk查找bigram时出错

Python 使用nltk查找bigram时出错,python,nltk,Python,Nltk,我正在使用此代码查找bigrams score_fn=BigramAssocMeasures.chi_sq n=200 bigram_finder = BigramCollocationFinder.from_words(all_words) bigrams = bigram_finder.nbest(score_fn, n) 错误: File "C:\Python34\lib\site-packages\nltk\metrics\association.py", line 212, in

我正在使用此代码查找bigrams

score_fn=BigramAssocMeasures.chi_sq
n=200
bigram_finder = BigramCollocationFinder.from_words(all_words)
bigrams = bigram_finder.nbest(score_fn, n)
错误:

File "C:\Python34\lib\site-packages\nltk\metrics\association.py", line   212, in  phi_sq      ((n_ii + n_io) * (n_ii + n_oi) * (n_io + n_oo) * (n_oi + n_oo))) 
ZeroDivisionError: float division by zero

我也面临同样的问题。解决方案是将语句保留在try-except块中,并忽略对输入列表中该项的处理。 此错误主要是由于一些无效输入造成的,例如空输入。

这应该可以工作

try:
    score_fn=BigramAssocMeasures.chi_sq
    n=200
    bigram_finder = BigramCollocationFinder.from_words(all_words)
    bigrams = bigram_finder.nbest(score_fn, n)
except ZeroDivisonError:
    # Do whatever you want it to do
    print(0)

你解决这个问题了吗?这里也一样。你找到解决办法了吗?