基本上,我想做的是创建一个程序,将一个句子/段落作为用户输入,查找每个单词的同义词,并用同义词替换该词。到目前为止,我创建的这个程序运行得很好,但是有一些问题/人为错误/逻辑错误。我现在得到的是:response=input("Enter what you want to thesaurize")
orig=response #puts user input into a string
num=orig.count(" ")+1 #finds number of words in the sentence
orig=orig.split(" ") #turns the sentence into a list of its words
new=[] #creates a new list to put the new words in, in case I'd want to go back to the original sentence for any reason
for i in range (num):
if orig[i] not in badWords: #makes sure that the word is not a no-synonym word like "the" or "be"
new.insert(i, myFuncs.replace(orig[i])) #the replace function (which I put in a separate module for neatness purposes) looks up the word on thesaurus.com and replaces it with a synonym
else:
new.insert(i, orig[i]) #If the word is an excluded word, it simply leaves it alone and keeps it in the new sentence
final="" #creates an empty string to put the new words in
for j in range(0,num):
final=final+new[j]+" " #turns the list of new words into a string that can be printed
print(final)
同样,这是完美的,但也有一些问题。基本上,我将其归纳为4个基本问题:
1)该词没有同义词,但仍不在排除词列表中
2)输入单词的错误含义,或返回的含义在用户输入的上下文中没有意义
3)动词的错误时态被返回并且
4)当输入名词时,该词的动词会返回,反之亦然(即“我要烤鸡”变成“我要烧鸡”或类似的意思)。在
基本上,我可以手动修复所有这些问题,方法是让用户浏览每个没有意义的单词,然后使用嵌套的if else和其他控制结构来引导他们选择正确的单词,但我认为这对用户来说是很乏味的,而且会破坏整个要点,特别是如果他们输入的是很多话。在
所以基本上我在问,这些问题中的哪一个可以自动化?也就是说,有没有什么方法可以编写代码让计算机识别这些问题?修复这些错误是很容易的,但是真正让程序识别逻辑错误而不是让用户处理它是困难的部分。在