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

2024-10-01 15:38:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个为自然语言编写程序的新手。我在使用区块读取器时出错。我得到的错误是索引列表超出范围。这是我的书面代码:

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 ("==================================")

所以,当我想要10行时,我应该输入10次,但输入后,我有一个错误,显示索引列表超出范围,显然,conllreader不工作,这就是错误的开始。我也在使用文件处理

conll.iob文件:

NNP B-NP先生

美多NNP I-NP

had VBD B-VP

已经过VBN I-VP

执行JJ B-NP

副NN I-NP

国家主席

B-PP中的分子结构

Balcor NNP B-NP

。O

(注意:文本之间没有空格,我只是留了个空格,这样会更清楚)。 文件文本是我的示例。所以在我的输入中,行是10。再一次,我在列表索引超出范围时出错


Tags: 列表string错误npquiznumvbprint
2条回答

请现在检查。我认为它会很好地工作。

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 ("==================================")
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 ("==================================")

相关问题 更多 >

    热门问题