我正在尝试编写代码来检查是否有人在同一个句子中切换了时态。它从文本框中获取数据,将其拆分为句子,将这些句子拆分为单词,并根据不同时态的动词列表检查单词。如果句子不一致,它会突出显示它。我已经成功完成了这么多,但是当我返回它时,我还希望在文本框中保持文本的格式相同。该程序也这样做,但是如果一个段落的第一句不一致,它将突出显示第一段和句子之间的所有空格。我尝试运行一个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