Files
ui-cviko1/notebook/chatbot.ipynb
2025-02-26 10:36:06 +01:00

4.5 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 = {
    "hello": ["Hello, how can I help you."],
    "i feel (.*)": ["Why do you feel {}?", "How long have you been felting {}?"],
    "i am (.*)": ["How long have you been {}?", "Why do you say you are {}?"],
    "i 'm (.*)": ["Why are you {}?", "How long have you been {}?"],
    "i (.*) you": [
        "Why do you {} yoourself?",
        "What makes you think you {} yoourself?",
    ],
    "i (.*) myself": [
        "Why do you {} yoourself?",
        "What makes you think you {} yoourself?",
    ],
    "(.*) sorry (.*)": [
        "There is no need to apologize.",
        "What are you apologizing for?",
    ],
    "(.*) friend (.*)": [
        "There is no need to apologize.",
        "What are you apologizing for?",
    ],
    "(.*) mother (.*)": ["Tell me more about your family."],
    "(.*) father (.*)": ["Tell me more about your family."],
    "yes": ["Why seem quite sure.", "OK, but can you elaborate."],
    "no": ["Why not.", "OK, but can you elaborate a bit?"],
    "(.*) your 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 game!",
        "we have to wait for the lake will be frozen",
    ],
    "(.*) football (.*)": [
        "millions of people follow their favorite team in every game",
        "this game helps kids stay active",
        "it is also called as “Soccer” in North America",
        "ronaldo is the best",
    ],
    "When will our (.*) finish?": [
        "Probably a few minutes",
        "I dont really know, check the time maybe?"
    ],
    " (.*)": [
        "Please tell me more.",
        "Let's change focus a bit... tell me about your family.",
        "Can you elaborate on that?",
    ],
    "": [
        "Why do you think that?",
        "Please tell me more.",
        "Let's change focus a bit... tell me about your family.",
        "Can you elaborate on that?",
    ],
}

def match_response(message):
    for pattern, response_list in responses.items():
        matches = re.match(pattern, message.lower())
        if matches:
            chosen_template = random.choice(response_list)
            return chosen_template.format(*matches.groups())
    return "I am sorry, I do not understand what your're saying."

while True:
    message = input("> ")

    if message == "bye":
        print("< Bye")
        break
    
    print(match_response(message))
we have to wait for the lake will be frozen
How long have you been felting happy today?
Can you elaborate on that?
What makes you think you like hockey, don't yoourself?
< Bye
In [ ]: