1
0
This commit is contained in:
s459312 2023-05-25 16:22:48 +02:00
parent e61b55dbaa
commit f73a796df1
2 changed files with 69 additions and 22 deletions

View File

@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 75, "execution_count": 202,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -39,12 +39,14 @@
" self.state['request_state'][domain][slot] = 0\n", " self.state['request_state'][domain][slot] = 0\n",
" \n", " \n",
" self.state['user_act'] = user_act\n", " self.state['user_act'] = user_act\n",
" return self.state" " return self.state\n",
" def init_session(self):\n",
" self.state = json.load(open('dictionary.json'))"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 170, "execution_count": 203,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -55,14 +57,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 171, "execution_count": 204,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"{'payment': {'type': 'karta', 'amount': '', 'loyalty_card': ''}, 'delivery': {'type': 'paczkomat', 'address': '', 'time': ''}, 'product': {'name': '', 'type': 'telefon', 'brand': '', 'price': '', 'quantity': '', 'quality': ''}}\n", "{'payment': {'type': 'karta', 'amount': '', 'loyalty_card': ''}, 'delivery': {'type': 'paczkomat', 'address': '', 'time': ''}, 'product': {'name': '', 'type': 'telefon', 'brand': '', 'price_range': '', 'price': '', 'quantity': '', 'quality': ''}}\n",
"{'product': {'type': 0}}\n" "{'product': {'type': 0}}\n"
] ]
}, },
@ -81,6 +83,7 @@
" 'product': {'name': '',\n", " 'product': {'name': '',\n",
" 'type': 'telefon',\n", " 'type': 'telefon',\n",
" 'brand': '',\n", " 'brand': '',\n",
" 'price_range': '',\n",
" 'price': '',\n", " 'price': '',\n",
" 'quantity': '',\n", " 'quantity': '',\n",
" 'quality': ''}},\n", " 'quality': ''}},\n",
@ -89,7 +92,7 @@
" 'history': []}" " 'history': []}"
] ]
}, },
"execution_count": 171, "execution_count": 204,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -102,15 +105,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 172, "execution_count": 223,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from collections import defaultdict\n", "from collections import defaultdict\n",
"from convlab.policy.policy import Policy\n",
"import json\n", "import json\n",
"\n", "\n",
"class SimpleRulePolicy():\n", "class SimpleRulePolicy(Policy):\n",
" def __init__(self):\n", " def __init__(self):\n",
" Policy.__init__(self)\n",
" self.db = json.load(open('product_db.json'))\n", " self.db = json.load(open('product_db.json'))\n",
"\n", "\n",
" def predict(self, state):\n", " def predict(self, state):\n",
@ -147,8 +152,6 @@
" system_action[(domain, 'NoOffer')] = []\n", " system_action[(domain, 'NoOffer')] = []\n",
" else:\n", " else:\n",
" system_action[(domain, 'Inform')].append(['Choice', str(len(self.results))])\n", " system_action[(domain, 'Inform')].append(['Choice', str(len(self.results))])\n",
" if domain == \"product\":\n",
" # Select the first product that matches the user's constraints\n",
" for product in self.results:\n", " for product in self.results:\n",
" if all(product.get(slot, '').lower() == value.lower() for slot, value in constraints):\n", " if all(product.get(slot, '').lower() == value.lower() for slot, value in constraints):\n",
" system_action[(domain, 'Recommend')].append(['Name', product['name']])\n", " system_action[(domain, 'Recommend')].append(['Name', product['name']])\n",
@ -158,28 +161,70 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 184, "execution_count": 224,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"[['Inform', 'product', 'Choice', '3'],\n", "[['Inform', 'product', 'Choice', '11'],\n",
" ['Inform', 'product', 'name', 'banan'],\n", " ['Recommend', 'product', 'Name', 'RedBull']]"
" ['Recommend', 'product', 'Name', 'banan']]"
] ]
}, },
"execution_count": 184, "execution_count": 224,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"dst = DST()\n", "dst = DST()\n",
"user_act = [('inform', 'product', 'type', 'energol'), ('inform', 'product', 'type', 'owoc')]\n", "user_act = [('inform', 'product', 'type', 'energol')]\n",
"state = dst.update(user_act)\n", "state = dst.update(user_act)\n",
"policy = SimpleRulePolicy()\n", "policy = SimpleRulePolicy()\n",
"policy.predict(dst.state)" "policy.predict(state)"
]
},
{
"cell_type": "code",
"execution_count": 225,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:root:nlu info_dict is not initialized\n",
"WARNING:root:dst info_dict is not initialized\n",
"WARNING:root:policy info_dict is not initialized\n",
"WARNING:root:nlg info_dict is not initialized\n"
]
}
],
"source": [
"from convlab.dialog_agent import PipelineAgent\n",
"policy = SimpleRulePolicy()\n",
"agent = PipelineAgent(nlu=None, dst=dst, policy=policy, nlg=None, name='sys')"
]
},
{
"cell_type": "code",
"execution_count": 228,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['Inform', 'product', 'Choice', '11'],\n",
" ['Recommend', 'product', 'Name', 'pomidor']]"
]
},
"execution_count": 228,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent.response([('inform', 'product', 'type', 'warzywo'), ('inform', 'product', 'price_range', 'tani'), ('inform', 'product', 'quality', 'exquisite')])"
] ]
} }
], ],

View File

@ -23,15 +23,17 @@
"name":"pomidor", "name":"pomidor",
"type":"warzywo", "type":"warzywo",
"brand":"warzywpol", "brand":"warzywpol",
"price_range":"tani",
"price":"15.37", "price":"15.37",
"quantity":"1 kg", "quantity":"1 kg",
"quality":"premium" "quality":"exquisite"
}, },
{ {
"name":"papryka", "name":"papryka",
"type":"warzywo", "type":"warzywo",
"brand":"warzywpol", "brand":"warzywpol",
"price_range":"tani",
"price":"15.37", "price":"15.37",
"quantity":"1 kg", "quantity":"1 kg",
"quality":"premium" "quality":"premium"