From 511b708fafc572500829055f6d576d7c66d50d33 Mon Sep 17 00:00:00 2001 From: br0kenpixel <23280129+br0kenpixel@users.noreply.github.com> Date: Wed, 26 Feb 2025 09:57:40 +0100 Subject: [PATCH] =?UTF-8?q?regex=20a=20k=C4=BE=C3=BA=C4=8Dov=C3=A9=20slov?= =?UTF-8?q?=C3=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notebook/chatbot.ipynb | 52 +++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/notebook/chatbot.ipynb b/notebook/chatbot.ipynb index 797f487..db8634c 100644 --- a/notebook/chatbot.ipynb +++ b/notebook/chatbot.ipynb @@ -2,23 +2,26 @@ "cells": [ { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "< My name is AI-CHATBOT.\n", - "< I can't do that.\n", - "< Nope.\n", - "< The sky is blue.\n", + "< I can't understand you.\n", + "< the weather is sunny\n", + "< I don't know, sorry.\n", + "< I can't answer that.\n", + "< I don't know, sorry.\n", + "< good winter time!\n", "< Bye\n" ] } ], "source": [ "import random\n", + "import re\n", "\n", "no_responses = [\n", " \"I can't answer that.\",\n", @@ -28,30 +31,23 @@ "]\n", "\n", "responses = {\n", - " \"What's the weather like today?\": [\n", - " \"It's sunny!\",\n", - " \"It seems sunny today.\",\n", - " \"Sunny.\"\n", + " \"name\": [\n", + " \"my name is AI-BOT\",\n", + " \"they call me AI-BOT\",\n", + " \"I go by AI-BOT\"\n", " ],\n", - " \"What is your name?\": [\n", - " \"My name is AI-CHATBOT.\",\n", - " \"It's AI-CHATBOT.\",\n", + " \"weather\": [\n", + " \"the weather is sunny\",\n", + " \"it's sunny today\"\n", " ],\n", - " \"What's your favorite color?\": [\n", - " \"It's yellow.\",\n", - " \"Yellow.\"\n", - " ],\n", - " \"What color is the sky?\": [\n", - " \"The sky is blue.\",\n", - " \"It's blue.\"\n", - " ],\n", - " \"Can you talk?\": [\n", - " \"No.\",\n", - " \"Nope.\",\n", - " \"I can't do that.\"\n", + " \"hockey\": [\n", + " \"good winter time!\",\n", + " \"we have to wait for the lake to be frozen\"\n", " ]\n", "}\n", "\n", + "intent_database = [key for key in responses.keys()]\n", + "\n", "while True:\n", " message = input(\"> \")\n", "\n", @@ -59,8 +55,12 @@ " print(\"< Bye\")\n", " break\n", " \n", - " if message in responses.keys():\n", - " print(\"<\", random.choice(responses[message]))\n", + " message = re.sub(\"[ ,.?!:;]\", \" \", message)\n", + " words = message.split()\n", + "\n", + " intent_word = next((word for word in words if word in intent_database), None)\n", + " if intent_word != None:\n", + " print(\"<\", random.choice(responses[intent_word]))\n", " else:\n", " print(\"<\", random.choice(no_responses))" ]