2
votes

I am using this code to find bigrams

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

error:

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
do you fix this issue? - raditya gumay
Same here. Did you get a solution? - pceccon

2 Answers

1
votes

I faced the same issue. The solution is to keep the statement in try except block, and ignore the processing for that item in the list of inputs. This error is mainly due to some invalid input, such as empty one.

0
votes

This should work,

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)