regex a kľúčové slová

This commit is contained in:
2025-02-26 09:57:40 +01:00
parent 5ea350c425
commit 511b708faf

View File

@@ -2,23 +2,26 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"< My name is AI-CHATBOT.\n", "< I can't understand you.\n",
"< I can't do that.\n", "< the weather is sunny\n",
"< Nope.\n", "< I don't know, sorry.\n",
"< The sky is blue.\n", "< I can't answer that.\n",
"< I don't know, sorry.\n",
"< good winter time!\n",
"< Bye\n" "< Bye\n"
] ]
} }
], ],
"source": [ "source": [
"import random\n", "import random\n",
"import re\n",
"\n", "\n",
"no_responses = [\n", "no_responses = [\n",
" \"I can't answer that.\",\n", " \"I can't answer that.\",\n",
@@ -28,30 +31,23 @@
"]\n", "]\n",
"\n", "\n",
"responses = {\n", "responses = {\n",
" \"What's the weather like today?\": [\n", " \"name\": [\n",
" \"It's sunny!\",\n", " \"my name is AI-BOT\",\n",
" \"It seems sunny today.\",\n", " \"they call me AI-BOT\",\n",
" \"Sunny.\"\n", " \"I go by AI-BOT\"\n",
" ],\n", " ],\n",
" \"What is your name?\": [\n", " \"weather\": [\n",
" \"My name is AI-CHATBOT.\",\n", " \"the weather is sunny\",\n",
" \"It's AI-CHATBOT.\",\n", " \"it's sunny today\"\n",
" ],\n", " ],\n",
" \"What's your favorite color?\": [\n", " \"hockey\": [\n",
" \"It's yellow.\",\n", " \"good winter time!\",\n",
" \"Yellow.\"\n", " \"we have to wait for the lake to be frozen\"\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",
" ]\n", " ]\n",
"}\n", "}\n",
"\n", "\n",
"intent_database = [key for key in responses.keys()]\n",
"\n",
"while True:\n", "while True:\n",
" message = input(\"> \")\n", " message = input(\"> \")\n",
"\n", "\n",
@@ -59,8 +55,12 @@
" print(\"< Bye\")\n", " print(\"< Bye\")\n",
" break\n", " break\n",
" \n", " \n",
" if message in responses.keys():\n", " message = re.sub(\"[ ,.?!:;]\", \" \", message)\n",
" print(\"<\", random.choice(responses[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", " else:\n",
" print(\"<\", random.choice(no_responses))" " print(\"<\", random.choice(no_responses))"
] ]