python名词同义词只替换2个_用同义词替换句子中的每个单词的Python程序

该博客介绍了一个Python程序,旨在接收用户输入的句子,然后用同义词替换每个单词。目前程序能有效运行,但存在一些问题,包括:无法处理无同义词的单词、选错词义、动词时态错误以及名词和动词混淆。作者探讨了手动修复这些问题的方案,并寻求自动化识别和解决这些问题的方法。
摘要由CSDN通过智能技术生成

基本上,我想做的是创建一个程序,将一个句子/段落作为用户输入,查找每个单词的同义词,并用同义词替换该词。到目前为止,我创建的这个程序运行得很好,但是有一些问题/人为错误/逻辑错误。我现在得到的是: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和其他控制结构来引导他们选择正确的单词,但我认为这对用户来说是很乏味的,而且会破坏整个要点,特别是如果他们输入的是很多话。在

所以基本上我在问,这些问题中的哪一个可以自动化?也就是说,有没有什么方法可以编写代码让计算机识别这些问题?修复这些错误是很容易的,但是真正让程序识别逻辑错误而不是让用户处理它是困难的部分。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值