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

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 = []
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

if b == 0:
question.append(q)
qcount = qcount + 1

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


Is there a way of turning

if q == question[i]


to

if q is similar to question[i]


?

Stack Overflow Asked by user13973539 on November 18, 2021

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 = []

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:
else:
question.append(q.lower())
a = input("How should i answer that? ")
q = input("Ask me anything: ")


Output:

Ask me anything: What is your Name
How should i answer that? Joe
Joe
Joe
Joe
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 = []

q = input("Ask me anything: ")
while q.lower() != 'stop':
if q.lower() in question:
i = question.index(q.lower())
else:
question.append(q.lower())
a = input("How should i answer that? ")
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 = []
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

if b == 0:
question.append(q)
qcount = qcount + 1

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


Answered by Aryan on November 18, 2021

## Related Questions

### Azure AD B2C .NET Core redirects directly without showing login screen

1  Asked on July 24, 2020 by bjorn

### EF Core multiple navigation properties to same table with fluent api

3  Asked on July 23, 2020 by themuyu

1  Asked on July 23, 2020 by jacobp

### Building a python egg, first letter is incorrectly removed from root file/folder name

1  Asked on July 22, 2020 by tim496

### How to force links sent by email to open in user’s browser instead of gmail in-app browser

2  Asked on July 22, 2020 by mmj

### Efficiently copying values from one ndarray to another on unequal sized arrays

4  Asked on July 22, 2020 by doubledouble