【发布时间】:2013-06-03 18:59:55
【问题描述】:
我正在尝试编写代码来检查是否有人在同一个句子中切换了时态。它从文本框中获取数据,将其拆分为句子,将这些句子拆分为单词,并根据不同时态的动词列表检查单词。如果句子不一致,它会突出显示它。我已经成功地做到了这一点,但是当我返回它时,我还希望在文本框中保持文本的格式相同。该程序也这样做,但是如果一个段落的第一句不一致,它将突出显示第一段和句子之间的所有空格。我尝试运行 if 语句来查找扩展空格并将其与句子分开,因此荧光笔不会将其作为句子的一部分。但是,我不断收到此错误:IndexError: list index out of range。
任何帮助将不胜感激。
这里是相关代码:
def verbTense(self):#Checks sentences for inconsistent tenses
text=self.input.get(1.0,'end')
self.input.delete(1.0,'end')
text=sentenceSplit(text)
self.input.tag_config('verb',background='yellow')
for i in text:
if inconsistentTense(i)==True:
self.input.insert('end',i,'verb')
else:
self.input.insert('end',i)
def sentenceSplit(x):#Splits a string into sentences.
fullText=[]
tempSentence=[]
x=formatSplit(x)
index=0
for i in x:
if i==" " and x[index+1]==" ":
fullText.append(i)
else:
if ".)" in i or "!" in i or "?" in i or "." in i or "!)" in i or "?)" in i or ")" in i or "]" in i or "}" in i:
tempSentence.append(i)
sentence=listIntoWord(tempSentence)
fullText.append(sentence)
tempSentence=[]
else:
tempSentence.append(i)
index+=1
return fullText
def listIntoWord(x):#Combines list of strings into one single string.
text=""
for i in x:
text+=str(i)
return text
def formatSplit(x):#Splits a string into different words while keeping the spaces.
wordString=[]
totalString=[]
for i in x:
if i==" ":
wordString=listIntoWord(wordString)
totalString.append(wordString)
wordString=[]
totalString.append(i)
else:
wordString.append(i)
return totalString
【问题讨论】:
-
文字墙... tldr ;;;您的索引超出范围....请发布一个小型自包含示例来说明您的问题...
-
请完整的堆栈跟踪,哪一行有错误?
-
如果 i==" " 和 x[index+1]==" ":
标签: python list indexing range grammar