简体   繁体   English

文件处理中超出范围的索引列表

[英]List of index out of range in file handling

I am new at making a program for a Natural Language.我刚开始制作自然语言程序。 I am having an error using Chunk Reader.我在使用 Chunk Reader 时遇到错误。 The error that I am getting is list of index out of range.我收到的错误是索引列表超出范围。 This my written code:这是我写的代码:

import nltk
from nltk.corpus.reader import ConllChunkCorpusReader
num_string = []
sen = int(input("Enter how many lines of text: ")) 
f = open("D:/VB/QUIZ NLTK/conll.iob", "w")
for i in range (0,sen):
    element = str(input(str(i + 1) + ". "))
    (num_string.append(element))
for name in num_string:
    f.write("%s\n" %(name))
f = open("D:/VB/QUIZ NLTK/conll.iob", "r")
conllreader = ConllChunkCorpusReader('D:/VB/QUIZ NLTK/conll.iob', r'.*\.iob', ('NP','VP', 'PP'))
print ("CONLLREADER CHUNKED WORDS")
print(conllreader.chunked_words('D:/VB/QUIZ NLTK/conll.iob'))
print ("==================================")
print ("CONLLREADER CHUNKED SENTS")
print(conllreader.chunked_sents('D:/VB/QUIZ NLTK/conll.iob'))
print ("==================================")
print ("CONLLREADER IOB WORDS")
print(conllreader.iob_words('D:/VB/QUIZ NLTK/conll.iob'))
print ("==================================")
print ("CONLLREADER IOB SENTS")
print(conllreader.iob_words('D:/VB/QUIZ NLTK/conll.iob'))
print ("==================================")

So, when I want 10 lines, I should input 10 times but after inputting, I am having an error that says list of index out of range and obviously, the conllreader is not working and that's where the error started.所以,当我想要 10 行时,我应该输入 10 次,但在输入之后,我遇到了一个错误,指出索引列表超出范围,显然,conllreader 不工作,这就是错误开始的地方。 I am also using a file handling.我也在使用文件处理。

conll.iob file: conll.iob 文件:

Mr. NNP B-NP NNP B-NP 先生

Meador NNP I-NP米多 NNP I-NP

had VBD B-VP有VBD B-VP

been VBN I-VP曾担任 VBN I-VP

executive JJ B-NP执行JJ B-NP

vice NN I-NP副NN I-NP

president NN I-NP总裁NN I-NP

of IN B-PP IN B-PP

Balcor NNP B-NP巴尔科 NNP B-NP

. . . . O

(NOTE: There is no space between the text, I just made a space so it will be clearer). (注意:文本之间没有空格,我只是做了一个空格,这样会更清楚)。 The file text is my sample.文件文本是我的示例。 So in my input for lines is 10. And again, I am having an error about having list index out of range.所以在我输入的行中是 10。而且,我再次遇到关于列表索引超出范围的错误。

import nltk
from nltk.corpus.reader import ConllChunkCorpusReader
num_string = []
sen = int(input("Enter how many lines of text: ")) 
f = open("D:/VB/QUIZ NLTK/conll.iob", "w")
try:
  for i in range (0,sen):
    element = str(input(str(i + 1) + ". "))
    (num_string.append(element))
except Exception as e:
  pass
for name in num_string:
    f.write("%s\n" %(name))
f = open("D:/VB/QUIZ NLTK/conll.iob", "r")
conllreader = ConllChunkCorpusReader('D:/VB/QUIZ NLTK/conll.iob', r'.*\.iob', ('NP','VP', 'PP'))
print ("CONLLREADER CHUNKED WORDS")
print(conllreader.chunked_words('D:/VB/QUIZ NLTK/conll.iob'))
print ("==================================")
print ("CONLLREADER CHUNKED SENTS")
print(conllreader.chunked_sents('D:/VB/QUIZ NLTK/conll.iob'))
print ("==================================")
print ("CONLLREADER IOB WORDS")
print(conllreader.iob_words('D:/VB/QUIZ NLTK/conll.iob'))
print ("==================================")
print ("CONLLREADER IOB SENTS")
print(conllreader.iob_words('D:/VB/QUIZ NLTK/conll.iob'))
print ("==================================")

Please Check it now.请立即检查。 I think it will work perfect.我认为它会很完美。

import nltk
from nltk.corpus.reader import ConllChunkCorpusReader
num_string = []
sen = int(input("Enter how many lines of text: ")) 
f = open("D:/VB/QUIZ NLTK/conll.iob", "w")
try:
  for i in range (0,sen):
    element = str(input(str(i + 1) + ". "))
    (num_string.append(element))
except Exception as e:
  pass
for name in num_string:
    f.write("%s\n" %(name))
f = open("D:/VB/QUIZ NLTK/conll.iob", "r")
conllreader = ConllChunkCorpusReader('D:/VB/QUIZ NLTK/conll.iob', r'.*\.iob', ('NP','VP', 'PP'))
print ("CONLLREADER CHUNKED WORDS")
print(conllreader.chunked_words('D:/VB/QUIZ NLTK/conll.iob'))
print ("==================================")
print ("CONLLREADER CHUNKED SENTS")
print(conllreader.chunked_sents('D:/VB/QUIZ NLTK/conll.iob'))
print ("==================================")
print ("CONLLREADER IOB WORDS")
print(conllreader.iob_words('D:/VB/QUIZ NLTK/conll.iob'))
print ("==================================")
print ("CONLLREADER IOB SENTS")
print(conllreader.iob_words('D:/VB/QUIZ NLTK/conll.iob'))
print ("==================================")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM