テキスト ファイルを読み取って、すべてのストップ ワードを削除しようとしています。ただし、使用時に Index out of range エラーが発生しb[i].pop(j)
ます。しかしprint(b[i][j])
、を使用すると、エラーが発生せず、単語が出力として取得されます。誰でもエラーを見つけることができますか?
import nltk
from nltk.corpus import stopwords
stop = stopwords.words('english')
fo = open("text.txt", "r")
# text.txt is just a text document
list = fo.read();
list = list.replace("\n","")
# removing newline character
b = list.split('.',list.count('.'))
# splitting list into lines
for i in range (len(b) - 1) :
b[i] = b[i].split()
# splitting each line into words
for i in range (0,len(b)) :
for j in range (0,len(b[i])) :
if b[i][j] in stop :
b[i].pop(j)
# print(b[i][j])
#print(b)
# Close opend file
fo.close()
出力:
Traceback (most recent call last):
File "prog.py", line 29, in <module>
if b[i][j] in stop :
IndexError: list index out of range
b[i].pop(j)
コメントおよびコメント解除に関する出力print(b[i][j])
:
is
that
the
from
the
the
the
can
the
and
and
the
is
and
can
be
into
is
a
or