亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何檢查我的聊天機器人中的 2 個句子是否相似

我如何檢查我的聊天機器人中的 2 個句子是否相似

慕妹3242003 2023-03-22 16:14:58
我使最簡單的聊天機器人成為可能。它會根據您之前希望它回答同一問題的內容來回答您的問題。代碼有點像這樣:question = []answer = []qcount = 0stop = 0b = 0while stop == 0:    b = 0    q = input("Ask me anything: ")    if q == "stop":        exit()    for i in range(qcount):        if q == question[i]:            b = 1            print(answer[i])    if b == 0:        question.append(q)        qcount = qcount + 1        a = input("How should i answer that? ")        answer.append(a)有沒有辦法轉if q == question[i]到if q is similar to question[i]?
查看完整描述

2 回答

?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

我有類似的答案給你。你也可以試試這個。您不需要計數器和退出語句。您可以將 while 語句本身定義為看門人。


我做了一些更多的改進。雖然這不會給你一個完美的聊天機器人,但它更接近了。


question = []

answer = []


q = input("Ask me anything: ")

while q.lower() != 'stop':

? ? i = -1

? ? z = q.lower().split()

? ? z.sort()


? ? for x in question:

? ? ? ? y = x.split()

? ? ? ? y.sort()

? ? ? ? if all(elem in y for elem in z):

? ? ? ? ? ? i = question.index(x)

? ? if i >= 0:

? ? ? ? print(answer[i])

? ? else:

? ? ? ? question.append(q.lower())

? ? ? ? a = input("How should i answer that? ")

? ? ? ? answer.append(a)

? ? q = input("Ask me anything: ")

輸出:


Ask me anything: What is your Name

How should i answer that? Joe

Ask me anything: What your name

Joe

Ask me anything: name

Joe

Ask me anything: your name

Joe

Ask me anything: what name

Joe

Ask me anything: what is name

Joe

如您所見,當您詢問“姓名是什么”時,它仍然假定您是在詢問您的姓名。您需要使用它來獲得更復雜的機器人。希望這可以幫助您朝著正確的方向前進。


我之前的回答也貼在這里。由于我們將字符串與列表進行比較,因此它必須完全匹配。檢查有問題的 q 并不能真正給你帶來優勢。您將需要拆分單詞并進行比較。這就是我在新回復中所做的(見上文)


question = []

answer = []


q = input("Ask me anything: ")

while q.lower() != 'stop':

? ? if q.lower() in question:

? ? ? ? i = question.index(q.lower())

? ? ? ? print (answer[i])

? ? else:

? ? ? ? question.append(q.lower())

? ? ? ? a = input("How should i answer that? ")

? ? ? ? answer.append(a)

? ? q = input("Ask me anything: ")


查看完整回答
反對 回復 2023-03-22
?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

使模糊查找器通過替換為此if q == question[i]不if q in question[i]查找特定單詞但查找關鍵字來執行此操作


question = []

answer = []

qcount = 0

stop = 0


b = 0


while stop == 0:

    b = 0

    q = input("Ask me anything: ")

    if q == "stop":

        exit()

    for i in range(qcount):

         if q in question[i]:  # HERE IS THE ANSWER

            b = 1

            print(answer[i])



    if b == 0:

        question.append(q)

        qcount = qcount + 1


        a = input("How should i answer that? ")

        answer.append(a)


查看完整回答
反對 回復 2023-03-22
  • 2 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號