added unfinished policy

This commit is contained in:
s459312 2023-05-25 13:40:17 +02:00
parent 9001dfae12
commit 02e30f53e0

View File

@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 57,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -38,23 +38,24 @@
" if slot not in self.state['request_state'][domain]:\n", " if slot not in self.state['request_state'][domain]:\n",
" self.state['request_state'][domain][slot] = 0\n", " self.state['request_state'][domain][slot] = 0\n",
" \n", " \n",
" self.state['user_action'] = user_act\n",
" return self.state" " return self.state"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 58,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"dst = DST()\n", "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)" "state = dst.update(user_act)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 59,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -62,18 +63,46 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"{'payment': {'type': 'credit card', 'amount': '', 'loyalty_card': ''}, 'delivery': {'type': '', 'address': '', 'time': ''}, 'product': {'name': 'iPhone', 'type': '', 'brand': '', 'price': '', 'quantity': '', 'quality': ''}}\n", "{'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": [ "source": [
"print(state['belief_state'])\n", "print(state['belief_state'])\n",
"print(state['request_state'])" "print(state['request_state'])\n",
"dst.state"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 60,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -82,14 +111,11 @@
"import json\n", "import json\n",
"from copy import deepcopy\n", "from copy import deepcopy\n",
"\n", "\n",
"from convlab.policy.policy import Policy\n",
"from convlab.util.multiwoz.dbquery import Database\n",
"\n", "\n",
"\n", "\n",
"class SimpleRulePolicy(Policy):\n", "class SimpleRulePolicy():\n",
" def __init__(self):\n", " def __init__(self):\n",
" Policy.__init__(self)\n", " self.db = json.load(open('product_db.json'))\n",
" self.db = Database()\n",
"\n", "\n",
" def predict(self, state):\n", " def predict(self, state):\n",
" self.results = []\n", " self.results = []\n",
@ -103,9 +129,7 @@
" self.update_system_action(user_act, user_action, state, system_action)\n", " self.update_system_action(user_act, user_action, state, system_action)\n",
"\n", "\n",
" # Reguła 3\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", " \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", " 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", " state['system_action'] = system_acts\n",
@ -113,8 +137,6 @@
"\n", "\n",
" def update_system_action(self, user_act, user_action, state, system_action):\n", " def update_system_action(self, user_act, user_action, state, system_action):\n",
" domain, intent = user_act\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", "\n",
" # Reguła 1\n", " # Reguła 1\n",
" if intent == 'request':\n", " if intent == 'request':\n",
@ -136,6 +158,27 @@
" if domain in [\"product\"]:\n", " if domain in [\"product\"]:\n",
" system_action[(domain, 'Recommend')].append(['Name', choice['name']])" " 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": { "metadata": {