From 02e30f53e026e93307559493559cd5daf7e72a4b Mon Sep 17 00:00:00 2001 From: s459312 Date: Thu, 25 May 2023 13:40:17 +0200 Subject: [PATCH] added unfinished policy --- DialogManager.ipynb | 79 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 18 deletions(-) diff --git a/DialogManager.ipynb b/DialogManager.ipynb index e20ff14..864972d 100644 --- a/DialogManager.ipynb +++ b/DialogManager.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 14, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ @@ -37,24 +37,25 @@ " self.state['request_state'][domain] = {}\n", " if slot not in self.state['request_state'][domain]:\n", " self.state['request_state'][domain][slot] = 0\n", - "\n", + " \n", + " self.state['user_action'] = user_act\n", " return self.state" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "dst = DST()\n", - "user_act = [('inform', 'payment', 'type', 'credit card'), ('inform', 'product', 'name', 'iPhone')]\n", + "user_act = [('inform', 'payment', 'type', 'credit card'), ('inform', 'product', 'name', 'iPhone'), ('request', 'product', 'type', 'warzywo')]\n", "state = dst.update(user_act)" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 59, "metadata": {}, "outputs": [ { @@ -62,18 +63,46 @@ "output_type": "stream", "text": [ "{'payment': {'type': 'credit card', 'amount': '', 'loyalty_card': ''}, 'delivery': {'type': '', 'address': '', 'time': ''}, 'product': {'name': 'iPhone', 'type': '', 'brand': '', 'price': '', 'quantity': '', 'quality': ''}}\n", - "{}\n" + "{'product': {'type': 0}}\n" ] + }, + { + "data": { + "text/plain": [ + "{'user_act': [],\n", + " 'system_act': [],\n", + " 'belief_state': {'payment': {'type': 'credit card',\n", + " 'amount': '',\n", + " 'loyalty_card': ''},\n", + " 'delivery': {'type': '', 'address': '', 'time': ''},\n", + " 'product': {'name': 'iPhone',\n", + " 'type': '',\n", + " 'brand': '',\n", + " 'price': '',\n", + " 'quantity': '',\n", + " 'quality': ''}},\n", + " 'request_state': {'product': {'type': 0}},\n", + " 'terminated': False,\n", + " 'history': [],\n", + " 'user_action': [('inform', 'payment', 'type', 'credit card'),\n", + " ('inform', 'product', 'name', 'iPhone'),\n", + " ('request', 'product', 'type', 'warzywo')]}" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "print(state['belief_state'])\n", - "print(state['request_state'])" + "print(state['request_state'])\n", + "dst.state" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": {}, "outputs": [], "source": [ @@ -82,14 +111,11 @@ "import json\n", "from copy import deepcopy\n", "\n", - "from convlab.policy.policy import Policy\n", - "from convlab.util.multiwoz.dbquery import Database\n", "\n", "\n", - "class SimpleRulePolicy(Policy):\n", + "class SimpleRulePolicy():\n", " def __init__(self):\n", - " Policy.__init__(self)\n", - " self.db = Database()\n", + " self.db = json.load(open('product_db.json'))\n", "\n", " def predict(self, state):\n", " self.results = []\n", @@ -103,9 +129,7 @@ " self.update_system_action(user_act, user_action, state, system_action)\n", "\n", " # Reguła 3\n", - " if any(True for slots in user_action.values() for (slot, _) in slots if slot in ['book stay', 'book day', 'book people']):\n", - " if self.results:\n", - " system_action = {('Booking', 'Book'): [[\"Ref\", self.results[0].get('Ref', 'N/A')]]}\n", + " \n", "\n", " system_acts = [[intent, domain, slot, value] for (domain, intent), slots in system_action.items() for slot, value in slots]\n", " state['system_action'] = system_acts\n", @@ -113,8 +137,6 @@ "\n", " def update_system_action(self, user_act, user_action, state, system_action):\n", " domain, intent = user_act\n", - " constraints = [(slot, value) for slot, value in state['belief_state'][domain.lower()].items() if value != '']\n", - " self.results = deepcopy(self.db.query(domain.lower(), constraints))\n", "\n", " # Reguła 1\n", " if intent == 'request':\n", @@ -136,6 +158,27 @@ " if domain in [\"product\"]:\n", " system_action[(domain, 'Recommend')].append(['Name', choice['name']])" ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "policy = SimpleRulePolicy()\n", + "policy.predict(dst.state)" + ] } ], "metadata": {