change dialog policy

This commit is contained in:
s459312 2023-05-25 14:49:44 +02:00
parent 43115895e5
commit 53d498fb2c

View File

@ -102,20 +102,16 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 122, "execution_count": 132,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from collections import defaultdict\n", "from collections import defaultdict\n",
"import json\n", "import json\n",
"\n", "\n",
"\n",
"\n",
"\n",
"class SimpleRulePolicy():\n", "class SimpleRulePolicy():\n",
" def __init__(self):\n", " def __init__(self):\n",
" with open('product_db.json', encoding='utf-8') as json_file:\n", " self.db = json.load(open('product_db.json'))\n",
" self.db = json.load(json_file)\n",
"\n", "\n",
" def predict(self, state):\n", " def predict(self, state):\n",
" self.results = []\n", " self.results = []\n",
@ -135,6 +131,7 @@
"\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",
" self.results = self.db['database'][domain]\n",
"\n", "\n",
" # Reguła 1\n", " # Reguła 1\n",
" if intent == 'request':\n", " if intent == 'request':\n",
@ -142,7 +139,7 @@
" system_action[(domain, 'NoOffer')] = []\n", " system_action[(domain, 'NoOffer')] = []\n",
" else:\n", " else:\n",
" for slot in user_action[user_act]:\n", " for slot in user_action[user_act]:\n",
" if slot[0] in self.results[0]:\n", " if self.results and slot[0] in self.results[0]:\n",
" system_action[(domain, 'Inform')].append([slot[0], self.results[0].get(slot[0], 'unknown')])\n", " system_action[(domain, 'Inform')].append([slot[0], self.results[0].get(slot[0], 'unknown')])\n",
"\n", "\n",
" # Reguła 2\n", " # Reguła 2\n",
@ -151,6 +148,8 @@
" 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",
"\n",
" if self.results and 'name' in self.results[0]:\n",
" choice = self.results[0]\n", " choice = self.results[0]\n",
"\n", "\n",
" if domain in [\"payment\", \"delivery\", \"product\"]:\n", " if domain in [\"payment\", \"delivery\", \"product\"]:\n",
@ -159,7 +158,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 123, "execution_count": 133,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -176,10 +175,19 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"[]" "[['Inform', 'payment', 'Choice', '1'],\n",
" ['Inform', 'payment', 'Choice', '1'],\n",
" ['Inform', 'delivery', 'Choice', '1'],\n",
" ['Inform', 'delivery', 'Choice', '1'],\n",
" ['Inform', 'product', 'Choice', '3'],\n",
" ['Inform', 'product', 'Choice', '3'],\n",
" ['Inform', 'product', 'name', 'banan'],\n",
" ['Inform', 'product', 'name', 'banan'],\n",
" ['Recommend', 'product', 'Name', 'banan'],\n",
" ['Recommend', 'product', 'Name', 'banan']]"
] ]
}, },
"execution_count": 123, "execution_count": 133,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }