使用nltk查找bigrams时出错

时间:2015-02-14 06:51:37

标签: 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  phi_sq      ((n_ii + n_io) * (n_ii + n_oi) * (n_io + n_oo) * (n_oi + n_oo))) 
ZeroDivisionError: float division by zero

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。解决方案是将语句保留在try除块之外,并忽略输入列表中该项的处理。 此错误主要是由于某些无效输入,例如空的输入。

答案 1 :(得分:0)

这应该有效,

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)