RuntimeError: генератор поднял StopIteration при генерации токенов в текстовом файле - PullRequest
0 голосов
/ 13 декабря 2018

Я пытаюсь запустить этот код:

with open(Textfile.txt', 'r') as text1:
            raw_text = text1.read().lower()

import re
from nltk.util import ngrams

raw_text = re.sub(r'[^a-zA-Z0-9\s]', ' ', raw_text)

tokens = [token for token in raw_text.split(" ") if token != ""]
# generate ngrams 
output = list(ngrams(tokens, 2))
        return

И я получаю следующую ошибку:

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-11-2ce960ed385c> in <module>()
      7 tokens = [token for token in raw_text.split(" ") if token != ""]
      8 # generate ngrams
----> 9 output = list(ngrams(tokens, 2))
     10 try:
     11     yield next(seq)

RuntimeError: generator raised StopIteration

Мой вопрос: как я могу применить ngrams из 2 к любомуТекстовый файл без этой ошибки?Было бы очень хорошо, если бы вы, ребята, могли бы помочь мне с этим вопросом :)

...