systemy_dialogowe/SDMockup.ipynb
2023-04-18 19:57:56 +02:00

313 lines
8.4 KiB
Plaintext

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# ASR"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"def asr(inputText: str) -> str:\n",
" # Do something\n",
" inputText\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# NLU"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"class NaturalLanguageUnderstanding:\n",
" acts: dict[list[str], str] = {\n",
" ( \"potwierdzam\", \"dobrze\", \"ok\" ): \"ack\",\n",
" (\"do widziena\", \"czesc\", \"koniec\", \"do zobaczenia\"): \"bye\",\n",
" (\"cześć\", \"dzień dobry\", \"hello\", \"hej\"): \"hello\",\n",
" (\"pomóc\", \"pomocy\", \"pomoc\"): \"help\",\n",
" (\"zaprzeczam\", \"odrzucam\"): \"negate\",\n",
" (\"alternatywny\", \"inne\", \"alternatywa\", \"inna\"): \"requalts\",\n",
" (\"szczegółów\", \"informacji\", \"info\", \"informacje\"): \"reqmore\",\n",
" (\"restart\"): \"restart\",\n",
" (\"dziękuję\", \"dzięki\"): \"thankyou\",\n",
" (\"tak\", \"chcę\"): \"confirm\",\n",
" (\"nie chce\"): \"deny\",\n",
" (\"basen\", \"parking\", \"śniadania\", \"osoby\"): \"inform\",\n",
" (\"jaki\",\"?\", \"czy\", \"jak\", \"ile\", \"co\", \"gdzie\"): \"request\"\n",
" }\n",
" def __init__(self, text: str):\n",
" self.text = text\n",
" self.act = \"\"\n",
" \n",
" \n",
" def get_dialog_act(self): \n",
" for word in self.text.lower().split():\n",
" for key in NaturalLanguageUnderstanding.acts:\n",
" if word in key:\n",
" self.act = NaturalLanguageUnderstanding.acts[key]\n",
" return\n",
" self.act = \"null\"\n",
" \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'request'"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nlu = NaturalLanguageUnderstanding(\"Jaki pokój proponujesz w tym hotelu?\")\n",
"nlu.get_dialog_act()\n",
"nlu.act"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# DST"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"class DialogueStateTracker:\n",
" \n",
" slots_dict: dict[tuple[str], str] = {\n",
" (\"osoby\", \"ludzie\", \"osób\", \"osobowy\"): \"people\",\n",
" (\"miasto\", \"miasta\", \"miejsowość\", \"poznań\", \"warszawa\", \"warszawie\", \"poznaniu\", \"kraków\", \"krakowie\"): \"city\",\n",
" (\"basen\", \"parking\", \"śniadania\"): \"facilities\",\n",
" (\"data\", \"datę\"): \"date\",\n",
" (\"pokój\", \"pokoje\"): \"room\"\n",
" }\n",
" \n",
" def __init__(self, nlu: NaturalLanguageUnderstanding):\n",
" self.slots = []\n",
" self.act = nlu.act\n",
" self.text = nlu.text\n",
" \n",
" def get_dialog_slots(self):\n",
" for word in self.text.lower().split():\n",
" for key in DialogueStateTracker.slots_dict:\n",
" if word in key:\n",
" self.slots.append(DialogueStateTracker.slots_dict[key])\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['room']"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dst: DialogueStateTracker = DialogueStateTracker(nlu)\n",
"dst.get_dialog_slots()\n",
"dst.slots\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Dialogue Policy"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"class DialoguePolicy:\n",
" user_act_to_system_act_dict: dict[str, str] = {\n",
" \"ack\": \"reqmore\",\n",
" \"bye\": \"bye\",\n",
" \"hello\": \"welcomemsg\",\n",
" \"help\": \"inform\",\n",
" \"negate\": \"offer\",\n",
" \"requalts\": \"offer\",\n",
" \"reqmore\": \"inform\",\n",
" \"restart\": \"welcomemsg\",\n",
" \"thankyou\": \"reqmore\",\n",
" \"confirm\": \"reqmore\",\n",
" \"deny\": \"offer\",\n",
" \"inform\": \"offer\",\n",
" \"request\": \"inform\",\n",
" \"null\": \"null\"\n",
" }\n",
" \n",
" def __init__(self, dst: DialogueStateTracker):\n",
" self.user_text = dst.text\n",
" self.user_act = dst.act\n",
" self.user_slots = dst.slots\n",
" self.system_act = \"\"\n",
" \n",
" def get_system_act(self):\n",
" self.system_act = DialoguePolicy.user_act_to_system_act_dict[self.user_act]\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'inform'"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dp: DialoguePolicy = DialoguePolicy(dst)\n",
"dp.get_system_act()\n",
"dp.system_act"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# NLG"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"class NaturalLanguageGeneration:\n",
" system_act_to_text = {\n",
" \"reqmore\": \"Informuje więcej o \",\n",
" \"bye\": \"Do widzenia\",\n",
" \"welcomemsg\": \"Witaj w systemie rezerwacji hotelowych. W czym mogę pomóc?\",\n",
" \"inform\": \"Informuje cię o \",\n",
" \"offer\": \"Co myślisz o hotlu z \",\n",
" \"reqmore\": \"Czy mogę jeszcze jakoś Ci pomóc?\",\n",
" \"null\": \"\"\n",
" }\n",
" user_slots_to_text = {\n",
" \"people\": \"pojemności pokoju\",\n",
" \"city\": \"mieście\",\n",
" \"facilities\": \"udogodnieniach\",\n",
" \"date\": \"dacie\",\n",
" \"room\": \"pokoju\"\n",
" }\n",
" \n",
" def __init__(self, dp: DialoguePolicy):\n",
" self.user_text = dp.user_text\n",
" self.user_act = dp.user_act\n",
" self.user_slots = dp.user_slots\n",
" self.system_act = dp.system_act\n",
" self.system_text = \"\"\n",
" \n",
" def generate_system_text(self):\n",
" text: str = NaturalLanguageGeneration.system_act_to_text[self.system_act]\n",
" slots_transformed = [NaturalLanguageGeneration.user_slots_to_text[slot] for slot in self.user_slots]\n",
" self.system_text = text + \" i \".join(slots_transformed)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Informuje cię o pokoju'"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nlg: NaturalLanguageGeneration = NaturalLanguageGeneration(dp)\n",
"nlg.generate_system_text()\n",
"nlg.system_text"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "SDenv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}