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": [
{
"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))"
]