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": [
{
"cell_type": "code",
"execution_count": 75,
"execution_count": 202,
"metadata": {},
"outputs": [],
"source": [
@ -39,12 +39,14 @@
" self.state['request_state'][domain][slot] = 0\n",
" \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",
"execution_count": 170,
"execution_count": 203,
"metadata": {},
"outputs": [],
"source": [
@ -55,14 +57,14 @@
},
{
"cell_type": "code",
"execution_count": 171,
"execution_count": 204,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"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"
]
},
@ -81,6 +83,7 @@
" 'product': {'name': '',\n",
" 'type': 'telefon',\n",
" 'brand': '',\n",
" 'price_range': '',\n",
" 'price': '',\n",
" 'quantity': '',\n",
" 'quality': ''}},\n",
@ -89,7 +92,7 @@
" 'history': []}"
]
},
"execution_count": 171,
"execution_count": 204,
"metadata": {},
"output_type": "execute_result"
}
@ -102,15 +105,17 @@
},
{
"cell_type": "code",
"execution_count": 172,
"execution_count": 223,
"metadata": {},
"outputs": [],
"source": [
"from collections import defaultdict\n",
"from convlab.policy.policy import Policy\n",
"import json\n",
"\n",
"class SimpleRulePolicy():\n",
"class SimpleRulePolicy(Policy):\n",
" def __init__(self):\n",
" Policy.__init__(self)\n",
" self.db = json.load(open('product_db.json'))\n",
"\n",
" def predict(self, state):\n",
@ -147,39 +152,79 @@
" system_action[(domain, 'NoOffer')] = []\n",
" else:\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",
" if all(product.get(slot, '').lower() == value.lower() for slot, value in constraints):\n",
" system_action[(domain, 'Recommend')].append(['Name', product['name']])\n",
" break\n",
" for product in self.results:\n",
" if all(product.get(slot, '').lower() == value.lower() for slot, value in constraints):\n",
" system_action[(domain, 'Recommend')].append(['Name', product['name']])\n",
" break\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 184,
"execution_count": 224,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['Inform', 'product', 'Choice', '3'],\n",
" ['Inform', 'product', 'name', 'banan'],\n",
" ['Recommend', 'product', 'Name', 'banan']]"
"[['Inform', 'product', 'Choice', '11'],\n",
" ['Recommend', 'product', 'Name', 'RedBull']]"
]
},
"execution_count": 184,
"execution_count": 224,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"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",
"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",
"type":"warzywo",
"brand":"warzywpol",
"price_range":"tani",
"price":"15.37",
"quantity":"1 kg",
"quality":"premium"
"quality":"exquisite"
},
{
"name":"papryka",
"type":"warzywo",
"brand":"warzywpol",
"price_range":"tani",
"price":"15.37",
"quantity":"1 kg",
"quality":"premium"