Skip to main content
 首页 » 编程设计

python之使用 nltk 查找二元组时出错

2025年05月04日11findumars

我正在使用这段代码来查找双字母组

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 block 中,并忽略输入列表中对该项目的处理。 这个错误主要是由于一些无效的输入,比如空输入。