diff --git a/.ipynb_checkpoints/DST_DP-checkpoint.ipynb b/.ipynb_checkpoints/DST_DP-checkpoint.ipynb new file mode 100644 index 0000000..837fdf5 --- /dev/null +++ b/.ipynb_checkpoints/DST_DP-checkpoint.ipynb @@ -0,0 +1,342 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "4af8e091", + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "\n", + "class Rules_DST(): \n", + "\n", + " def __init__(self):\n", + " self.state = json.load(open('data.json'))\n", + "\n", + " def update_user(self, user_acts=None):\n", + " for intent, domain, slot, value in user_acts:\n", + " domain = domain.lower()\n", + " intent = intent.lower()\n", + " slot = slot.lower()\n", + " if intent == 'start_conversation':\n", + " continue\n", + "\n", + " elif intent == 'end_conversation':\n", + " self.state = json.load(open('data.json'))\n", + " elif domain not in self.state['belief_state']:\n", + " continue\n", + " \n", + " \n", + " elif 'inform' in intent:\n", + " if (slot == 'inform'):\n", + " continue\n", + " \n", + " if(domain in slot):\n", + " slot.replace(domain + \"/\", '')\n", + "\n", + " domain_dic = self.state['belief_state'][domain]\n", + " if slot in domain_dic:\n", + " self.state['belief_state'][domain][slot] = value\n", + " \n", + " \n", + " elif intent == 'request':\n", + " if domain not in self.state['request_state']:\n", + " self.state['request_state'][domain] = {}\n", + " if slot not in self.state['request_state'][domain]:\n", + " self.state['request_state'][domain][slot] = 0\n", + " else:\n", + " self.state['request_state'][domain][slot] = value\n", + " \n", + " elif intent == 'start_conversation':\n", + " self.state[\"user_action\"].append([intent, domain, slot, value])\n", + " continue\n", + "\n", + " elif intent == 'end_conversation':\n", + " self.state = json.load(open('data.json'))\n", + " \n", + " self.state[\"user_action\"].append([intent, domain, slot, value])\n", + " return self.state" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "09903205", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'user_action': [],\n", + " 'system_action': [],\n", + " 'belief_state': {'food': {'name': '',\n", + " 'type': '',\n", + " 'price range': '',\n", + " 'size': '',\n", + " 'ingredients': ''},\n", + " 'drink': {'name': '', 'price range': '', 'size': ''},\n", + " 'sauce': {'name': '', 'price range': '', 'size': ''},\n", + " 'order': {'type': '',\n", + " 'price range': '',\n", + " 'restaurant_name': '',\n", + " 'area': '',\n", + " 'book time': '',\n", + " 'book day': ''},\n", + " 'booking': {'restaurant_name': '',\n", + " 'area': '',\n", + " 'book time': '',\n", + " 'book day': '',\n", + " 'book people': ''},\n", + " 'payment': {'type': '', 'amount': '', 'vat': ''}},\n", + " 'request_state': {},\n", + " 'terminated': False,\n", + " 'history': []}" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dst = Rules_DST()\n", + "dst.state" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "ec2b40d2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dst.state['user_action']" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ca5ec2f3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'name': '', 'type': '', 'price range': '', 'size': '', 'ingredients': ''}" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dst.update_user([['star_conversation',\"\",\"\",\"\"], ['inform', 'drink', 'size', 'duża']])\n", + "dst.state['belief_state']['food']" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "2a36fa8c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[['inform', 'drink', 'size', 'duża']]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dst.state['user_action']" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "67fd77b2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'drink': {'price range': 0}}" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dst.update_user([['request', 'drink', 'price range', '?']])\n", + "dst.state['request_state']" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "834ebb03", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'name': '',\n", + " 'type': 'pizza',\n", + " 'price range': '',\n", + " 'size': 'duża',\n", + " 'ingredients': ''}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dst.update_user([['inform', 'food', 'type', 'pizza'], ['inform', 'food', 'size', 'duża']])\n", + "dst.state['belief_state']['food']" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "4b61083c", + "metadata": {}, + "outputs": [], + "source": [ + "from collections import defaultdict\n", + "import jmespath\n", + "\n", + "class DP():\n", + " def __init__(self):\n", + " with open('database.json', encoding='utf-8-sig') as json_file:\n", + " self.db = json.load(json_file)\n", + " \n", + "\n", + " def predict(self, state):\n", + " self.results = []\n", + " system_action = defaultdict(list)\n", + " user_action = defaultdict(list)\n", + " system_acts = []\n", + " for idx in range(len(state['user_action'])):\n", + " intent, domain, slot, value = state['user_action'][idx]\n", + " user_action[(domain, intent)].append((slot, value))\n", + "\n", + " for user_act in user_action:\n", + " system_acts.append(self.update_system_action(user_act, user_action, state, system_action))\n", + " state['system_action'] = system_acts\n", + " return system_acts[-1]\n", + "\n", + "\n", + " def update_system_action(self, user_act, user_action, state, system_action):\n", + " \n", + " domain, intent = user_act \n", + " \n", + " #Reguła 3\n", + " if intent == 'end_conversation':\n", + " return None\n", + " \n", + " constraints = [(slot, value) for slot, value in state['belief_state'][domain].items() if value != '']\n", + " \n", + " # Reguła 1\n", + " if intent == 'request':\n", + " if len(self.results) == 0:\n", + " system_action[(domain, 'NoOffer')] = []\n", + " else:\n", + " for slot in user_action[user_act]: \n", + " if slot[0] in self.results[0]:\n", + " system_action[(domain, 'Inform')].append([slot[0], self.results[0].get(slot[0], 'unknown')])\n", + "\n", + " # Reguła 2\n", + " elif intent == 'inform':\n", + " if len(constraints)>1:\n", + " arg=f\"{constraints[0]}\".replace(f\"\\'{constraints[0][0]}\\'\",f\"{constraints[0][0]}\")\n", + " arg = arg.replace(\"[\",\"\").replace(\"]\",\"\")\n", + " for cons in constraints[1:]:\n", + " arg+=f\" && contains{cons}\".replace(f\"\\'{cons[0]}\\'\",f\"{cons[0]}\").replace(\"[\",\"\").replace(\"]\",\"\")\n", + " else:\n", + " arg=f\"{constraints}\".replace(f\"\\'{constraints[0]}\\'\",f\"{constraints[0]}\").replace(\"[\",\"\").replace(\"]\",\"\").replace(\"(\\'\",\"(\").replace(\"\\',\",\",\") \n", + " self.results = jmespath.search(f\"database.{domain}[?contains{arg} == `true` ]\", self.db) \n", + " if len(self.results) == 0:\n", + " system_action[(domain, 'NoOffer')] = []\n", + " else:\n", + " system_action[(domain, 'Inform')].append(['Choice', str(len(self.results))])\n", + " choice = self.results[0]\n", + "\n", + " if domain in [\"food\", \"drink\", \"sauce\"]:\n", + " system_action[(domain, 'Recommend')].append(['Name', choice['name']])\n", + " elif domain in [\"order\", \"booking\", \"payment\"]:\n", + " system_action[(domain, 'Recommend')].append(['Type', choice['type']])\n", + " return system_action\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "e587661a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "defaultdict(list,\n", + " {('drink', 'Inform'): [['Choice', '1'],\n", + " ['price range', 'średnia']],\n", + " ('drink', 'Recommend'): [['Name', 'lemoniada']],\n", + " ('food', 'Inform'): [['Choice', '4']],\n", + " ('food', 'Recommend'): [['Name', 'pizza margherita']]})" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dp= DP()\n", + "dp.predict(dst.state)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/DST_DP.ipynb b/DST_DP.ipynb index 6f78442..837fdf5 100644 --- a/DST_DP.ipynb +++ b/DST_DP.ipynb @@ -282,8 +282,10 @@ " system_action[(domain, 'Inform')].append(['Choice', str(len(self.results))])\n", " choice = self.results[0]\n", "\n", - " if domain in [\"food\", \"drink\", \"police\", \"sauce\", \"order\", \"booking\", \"payment\"]:\n", + " if domain in [\"food\", \"drink\", \"sauce\"]:\n", " system_action[(domain, 'Recommend')].append(['Name', choice['name']])\n", + " elif domain in [\"order\", \"booking\", \"payment\"]:\n", + " system_action[(domain, 'Recommend')].append(['Type', choice['type']])\n", " return system_action\n", " \n" ] @@ -332,7 +334,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.16" + "version": "3.11.2" } }, "nbformat": 4, diff --git a/NLG.ipynb b/NLG.ipynb index db519a2..9e5045e 100644 --- a/NLG.ipynb +++ b/NLG.ipynb @@ -3,7 +3,7 @@ { "cell_type": "code", "execution_count": 17, - "id": "9a772dd1", + "id": "c31a012f", "metadata": {}, "outputs": [], "source": [ @@ -12,8 +12,8 @@ }, { "cell_type": "code", - "execution_count": 21, - "id": "54502dca", + "execution_count": 28, + "id": "21c8e0a4", "metadata": {}, "outputs": [], "source": [ @@ -69,21 +69,36 @@ " if slot == \"book time\":\n", " return f\"Godzina Twojego zamówienia to: {value}.\"\n", " if slot == \"book day\":\n", - " return f\"Dzień Twojego zamówienia to: {value}.\"\n", - " \n", + " return f\"Dzień Twojego zamówienia to: {value}.\" \n", "\n", " elif intent == 'end_conversation':\n", " return random.choice(['Żegnam i dziękuję za skorzystanie z usługi.', 'Do widzenia.', \"Dziękuję za skorzystanie z usługi, do kolejnego razu.\"])\n", " elif intent == 'start_conversation':\n", " return random.choice(['Witaj, jestem asystentem dialogowym, dzięki któremu złożysz zamówienie w restuaracji.', 'Cześć, w czym mogę pomóc?', \"Witam w usłudze, w czym mogę pomóc?\"])\n", - " \n", - " " + " elif intent == 'affirm':\n", + " return random.choice(['Tak.', 'Potwierdzam.', \"Mozliwe.\"])\n", + " elif intent == 'deny':\n", + " return random.choice(['Nie.', 'Nie potwierdzam.', \"Niemożliwe.\"]) \n", + " elif intent == 'recommend' and slot==\"name\":\n", + " if domain==\"food\":\n", + " return f\"Rekomendowany posiłek to: {value}.\"\n", + " if domain==\"drink\":\n", + " return f\"Rekomendowany napój to: {value}.\" \n", + " if domain==\"sauce\":\n", + " return f\"Rekomendowany sos to: {value}.\"\n", + " elif intent == 'recommend' and slot==\"type\":\n", + " if domain==\"booking\":\n", + " return f\"Rekomenduję następującą rezerwację stolika: {value}.\" \n", + " if domain==\"order\":\n", + " return f\"Rekomenduję takie zamówienie: {value}.\"\n", + " if domain==\"payment\":\n", + " return f\"Rekomendowana metoda płatności to: {value}.\" " ] }, { "cell_type": "code", "execution_count": 11, - "id": "8568ac20", + "id": "185b85f0", "metadata": {}, "outputs": [ { @@ -104,7 +119,7 @@ { "cell_type": "code", "execution_count": 12, - "id": "c3fa168f", + "id": "b276b31f", "metadata": {}, "outputs": [ { @@ -125,7 +140,7 @@ { "cell_type": "code", "execution_count": 13, - "id": "5ac835de", + "id": "3ae7e57f", "metadata": {}, "outputs": [ { @@ -146,7 +161,7 @@ { "cell_type": "code", "execution_count": 19, - "id": "6c06463d", + "id": "90303149", "metadata": {}, "outputs": [ { @@ -167,7 +182,7 @@ { "cell_type": "code", "execution_count": 25, - "id": "aeaa9968", + "id": "b78e9d4a", "metadata": {}, "outputs": [ { @@ -185,10 +200,52 @@ "nlg(['start_conversation', '', '', ''])" ] }, + { + "cell_type": "code", + "execution_count": 29, + "id": "37782f98", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Rekomendowany posiłek to: Zupa grzybowa.'" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nlg(['recommend', 'food', 'name', 'zupa grzybowa'])" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "c74610a3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Rekomendowana metoda płatności to: blik.'" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nlg(['recommend', 'payment', 'type', 'blik'])" + ] + }, { "cell_type": "code", "execution_count": null, - "id": "e48c1960", + "id": "1f6859ea", "metadata": {}, "outputs": [], "source": []