user3526598
user3526598

Reputation: 71

Error while finding bigrams using nltk

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

Upvotes: 2

Views: 498

Answers (2)

TSH
TSH

Reputation: 3

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)

Upvotes: 0

Ayush Vatsyayan
Ayush Vatsyayan

Reputation: 13

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.

Upvotes: 1

Related Questions