Python 为什么我得到RuntimeError:generator引发的StopIteration?如何解决呢?
我正在制作存储在docToken列表中的令牌的二元图Python 为什么我得到RuntimeError:generator引发的StopIteration?如何解决呢?,python,nltk,Python,Nltk,我正在制作存储在docToken列表中的令牌的二元图 print(docToken[520]) 输出:['sleepy','account','just','man','tired','twitter','case', “罗姆尼”、“候选人”、“长相”] 输出:[('sleepy','account'),('account','just'),('just','man'), ('man','tired'),('tired','twitter'),('twitter','case'), ('cas
print(docToken[520])
输出:['sleepy','account','just','man','tired','twitter','case',
“罗姆尼”、“候选人”、“长相”]
输出:[('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))
当范围(500)在第一个循环中时,它工作正常,但当范围为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
StopIteration回溯(最近一次调用)
ngrams中的~\Anaconda3\lib\site packages\nltk\util.py(顺序,n,左键,
焊盘(右、左焊盘(符号、右焊盘(符号))
467当n>1时:
-->468历史记录。追加(下一个(序列))
469 n-=1
停止迭代:
上述异常是以下异常的直接原因:
运行时错误回溯(上次最近调用)
在()
1个二进制数=[]
2对于范围(5000)内的i:
---->3 ls=列表(nltk.bigrams(docToken[i]))
4对于ls中的j:
5.附加(列表(j))
bigrams中的~\Anaconda3\lib\site packages\nltk\util.py(序列,**kwargs)
489 """
490
-->491对于ngrams中的项目(顺序,2,**kwargs):
492收益项目
493
运行时错误:生成器引发了StopIteration
我无法解决此错误。不确定为什么nltk.bigrams(docToken[I])
会生成此错误,但我可以使用以下代码创建bigrams
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
我通过从3.3->3.4升级nltk解决了这个问题 dosimple-pip安装nltk==3.4
希望能奏效!我也遇到了同样的错误。一个可能的原因可能是
docToken
中的一个元素是空列表
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])))
例如,以下代码在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
[['the_wildlings', 'wildlings_are', 'are_dead'],
['do_the',
'the_dead',
'dead_frighten',
'frighten_you',
'you_ser',
'ser_waymar']]
输出:
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
[['the_wildlings', 'wildlings_are', 'are_dead'],
['do_the',
'the_dead',
'dead_frighten',
'frighten_you',
'you_ser',
'ser_waymar']]
另一个可能的原因可能是您在Python3.7中使用了nltk
3.3
请使用nltk 3.4,这是第一个支持Python 3.7的版本,您的问题应该在这个版本中得到解决
请参阅。您使用的是哪一版本的Python?我使用的是Python 3.7.0