我正在制作存储在列表 docToken 中的令牌的 Bigrams。
print(docToken[520])
输出:['sleepy', 'account', 'just', 'man', 'tired', 'twitter', 'case', 'romney', 'candidate', 'looks']
list(nltk.bigrams(docToken[520]))
输出:[('sleepy', 'account'), ('account', 'just'), ('just', 'man'), ('man', 'tired'), ('tired', 'twitter '), ('twitter', 'case'), ('case', 'romney'), ('romney', 'candidate'), ('candidate', 'looks')]
当我nltk.bigrams(docToken[i])在循环中使用时,我在范围> = 1000上遇到以下错误:
bigram=[]
for i in range(5000):
ls=list(nltk.bigrams(docToken[i]))
for j in ls:
bigram.append(list(j))
当第一个循环中的 range(500) 时它工作得很好,但是当 Range 为 1000 或更大时,它给了我以下错误:
StopIteration Traceback (most recent call last)
~\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-76-8982951528bd> in <module>()
1 bigram=[]
2 for i in range(5000):
----> 3 ls=list(nltk.bigrams(docToken[i]))
4 for j in ls:
5 bigram.append(list(j))
~\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
三国纷争
红糖糍粑
ITMISS
相关分类