TransWikia.com

How do i check if 2 sentences are similar in my chatbot

Stack Overflow Asked by user13973539 on November 18, 2021

I made the simplest kind of chatbot possible. Its one that answers your questions depending on what you wanted it to answer to the same question before. The code is kinda like this:

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 == 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)

Is there a way of turning

if q == question[i]

to

if q is similar to question[i]

?

2 Answers

Aryan Mishra already provided you the answer. I have similar answer for you. You can try this as well. You dont need counters and exit statements. You can define the while statement itself as a gate keeper.

I made some more improvements. While this will NOT give you a perfect chatbot, it comes closer.

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: ")

Output:

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

As you can see, when you ask "what is name" it still assumes you are asking what is your name. You need to work with this to get to a more sophisticated bot. Hope this helps you move in the right direction.

My previous answer is also posted here. Since we are comparing a string to a list, it has to an exact match. checking for q in question does not really give you an advantage. You will need to split the words and compare them. Thats what I did in my new response (see above)

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: ")

Answered by Joe Ferndz on November 18, 2021

To Make A Fuzzy Finder Do This By Replacing if q == question[i] to if q in question[i] this does not look for a specific word but looks for a keyword

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)

Answered by Aryan on November 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP