Files
ui-cviko1/notebook/chatbot.ipynb

2.3 KiB

In [ ]:
import random
import re

no_responses = [
    "I can't answer that.",
    "I don't know, sorry.",
    "I can't understand you.",
    "Can you elaborate, please?"
]

responses = {
    "name": [
        "my name is AI-BOT",
        "they call me AI-BOT",
        "I go by AI-BOT"
    ],
    "weather": [
        "the weather is sunny",
        "it's sunny today"
    ],
    "hockey": [
        "good winter time!",
        "we have to wait for the lake to be frozen"
    ]
}

intent_database = [key for key in responses.keys()]

while True:
    message = input("> ")

    if message == "bye":
        print("< Bye")
        break
    
    message = re.sub("[ ,.?!:;]", " ", message)
    words = message.split()

    intent_word = next((word for word in words if word in intent_database), None)
    if intent_word != None:
        print("<", random.choice(responses[intent_word]))
    else:
        print("<", random.choice(no_responses))
< I can't understand you.
< the weather is sunny
< I don't know, sorry.
< I can't answer that.
< I don't know, sorry.
< good winter time!
< Bye
In [ ]: