Code Library
Chat Bot program :
from difflib import SequenceMatcher
def compare_strings(string1, dictionary):
similarity_ratios = {key: SequenceMatcher(None, string1, key).ratio() for key in dictionary.keys()}
max_ratio = max(similarity_ratios.values())
if max_ratio >= 0.7:
matching_keys = [key for key, value in similarity_ratios.items() if value == max_ratio]
matching_values = [dictionary[key] for key in matching_keys]
return matching_values
else:
return "I don't have an answer."
# Example usage
dictionary = {"hello": "Hi there!", "hi": "Hello!", "hey": "Hey, how are you?"
,"good morning":"good morning "
,"good night":"good night"
,"byee":"byee take care"
,"good byee":"good byee have a great day"
,"who are you":"I am a chat bot how can i help you"
,"what can you do":"I am a simple chat bot "
}
while True:
string1 = input()
output = compare_strings(string1, dictionary)
print(output)
hello
['Hi there!']
who are you
['I am chat bot how can i help you']