3 回答
TA贡献1804条经验 获得超7个赞
我也面临同样的错误。一个可能的原因可能是其中一个元素docToken是一个空列表。
例如,当i=2第二个元素是空列表时,以下代码会引发相同的错误。
from nltk import bigrams
docToken= [['the', 'wildlings', 'are', 'dead'], [], ['do', 'the', 'dead', 'frighten', 'you', 'ser', 'waymar']]
for i in range(3):
print (i)
print (list(nltk.bigrams(docToken[i])))
输出:
0
[('the', 'wildlings'), ('wildlings', 'are'), ('are', 'dead')]
1
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\nltk\util.py in ngrams(sequence, n, pad_left, pad_right, left_pad_symbol, right_pad_symbol)
467 while n > 1:
--> 468 history.append(next(sequence))
469 n -= 1
StopIteration:
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
<ipython-input-58-91f35cae32ed> in <module>
2 for i in range(3):
3 print (i)
----> 4 list(nltk.bigrams(docToken[i]))
~\AppData\Local\Continuum\anaconda3\lib\site-packages\nltk\util.py in bigrams(sequence, **kwargs)
489 """
490
--> 491 for item in ngrams(sequence, 2, **kwargs):
492 yield item
493
RuntimeError: generator raised StopIteration
您可以从中过滤出空列表docToken,然后创建二元组:
docToken= [['the', 'wildlings', 'are', 'dead'], [], ['do', 'the', 'dead', 'frighten', 'you', 'ser', 'waymar']]
docToken = [x for x in docToken if x]
bigram = []
for i in range(len(docToken)):
bigram.append(["_".join(w) for w in bigrams(docToken[i])])
bigram
输出:
[['the_wildlings', 'wildlings_are', 'are_dead'],
['do_the',
'the_dead',
'dead_frighten',
'frighten_you',
'you_ser',
'ser_waymar']]
另一个可能的原因可能是您nltk在 python 3.7 中使用了 3.3。
请使用 nltk 3.4,它是第一个支持 Python 3.7 的版本,您的问题应该在这个版本中得到解决。
请看这里。
TA贡献1871条经验 获得超8个赞
我无法解决此错误。不知道为什么nltk.bigrams(docToken[i])会生成这个,但我能够使用以下代码创建二元组。
bigram={}
for i in range(size):
ls=[]
for j in range(len(docToken[i])-1):
for k in range(j,len(docToken[i])-1):
ls.append([docToken[i][j],docToken[i][k+1]])
bigram[i]=ls
添加回答
举报
