changed database + some fixes

This commit is contained in:
s459312 2023-05-25 14:18:57 +02:00
parent 02e30f53e0
commit 46d6f28114

View File

@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 57, "execution_count": 75,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -38,58 +38,58 @@
" 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", " self.state['user_act'] = user_act\n",
" return self.state" " return self.state"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 58, "execution_count": 109,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"dst = DST()\n", "dst = DST()\n",
"user_act = [('inform', 'payment', 'type', 'credit card'), ('inform', 'product', 'name', 'iPhone'), ('request', 'product', 'type', 'warzywo')]\n", "user_act = [('inform', 'payment', 'type', 'karta'), ('inform', 'delivery', 'type','paczkomat'), ('inform', 'product', 'type', 'telefon'), ('request', 'product', 'name', '?')]\n",
"state = dst.update(user_act)" "state = dst.update(user_act)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 59, "execution_count": 110,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"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': 'karta', 'amount': '', 'loyalty_card': ''}, 'delivery': {'type': 'paczkomat', 'address': '', 'time': ''}, 'product': {'name': '', 'type': 'telefon', 'brand': '', 'price': '', 'quantity': '', 'quality': ''}}\n",
"{'product': {'type': 0}}\n" "{'product': {'name': 0}}\n"
] ]
}, },
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"{'user_act': [],\n", "{'user_act': [('inform', 'payment', 'type', 'karta'),\n",
" ('inform', 'delivery', 'type', 'paczkomat'),\n",
" ('inform', 'product', 'type', 'telefon'),\n",
" ('request', 'product', 'name', '?')],\n",
" 'system_act': [],\n", " 'system_act': [],\n",
" 'belief_state': {'payment': {'type': 'credit card',\n", " 'belief_state': {'payment': {'type': 'karta',\n",
" 'amount': '',\n", " 'amount': '',\n",
" 'loyalty_card': ''},\n", " 'loyalty_card': ''},\n",
" 'delivery': {'type': '', 'address': '', 'time': ''},\n", " 'delivery': {'type': 'paczkomat', 'address': '', 'time': ''},\n",
" 'product': {'name': 'iPhone',\n", " 'product': {'name': '',\n",
" 'type': '',\n", " 'type': 'telefon',\n",
" 'brand': '',\n", " 'brand': '',\n",
" 'price': '',\n", " 'price': '',\n",
" 'quantity': '',\n", " 'quantity': '',\n",
" 'quality': ''}},\n", " 'quality': ''}},\n",
" 'request_state': {'product': {'type': 0}},\n", " 'request_state': {'product': {'name': 0}},\n",
" 'terminated': False,\n", " 'terminated': False,\n",
" 'history': [],\n", " 'history': []}"
" 'user_action': [('inform', 'payment', 'type', 'credit card'),\n",
" ('inform', 'product', 'name', 'iPhone'),\n",
" ('request', 'product', 'type', 'warzywo')]}"
] ]
}, },
"execution_count": 59, "execution_count": 110,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -102,39 +102,37 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 60, "execution_count": 122,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from collections import defaultdict\n", "from collections import defaultdict\n",
"import copy\n",
"import json\n", "import json\n",
"from copy import deepcopy\n", "\n",
"\n", "\n",
"\n", "\n",
"\n", "\n",
"class SimpleRulePolicy():\n", "class SimpleRulePolicy():\n",
" def __init__(self):\n", " def __init__(self):\n",
" self.db = json.load(open('product_db.json'))\n", " with open('product_db.json', encoding='utf-8') as json_file:\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",
" system_action = defaultdict(list)\n", " system_action = defaultdict(list)\n",
" user_action = defaultdict(list)\n", " user_action = defaultdict(list)\n",
"\n", " system_acts = []\n",
" for intent, domain, slot, value in state['user_action']:\n", " for intent, domain, slot, value in state['user_act']:\n",
" user_action[(domain.lower(), intent.lower())].append((slot.lower(), value))\n", " user_action[(domain.lower(), intent.lower())].append((slot.lower(), value))\n",
"\n", " print(user_action)\n",
" for user_act in user_action:\n", " for user_act in user_action:\n",
" 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", " print(self.update_system_action(user_act, user_action, state, system_action))\n",
" # Reguła 3\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_act'] = system_acts\n",
" return system_acts\n", " return system_acts\n",
"\n", "\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",
"\n", "\n",
@ -155,22 +153,33 @@
" system_action[(domain, 'Inform')].append(['Choice', str(len(self.results))])\n", " system_action[(domain, 'Inform')].append(['Choice', str(len(self.results))])\n",
" choice = self.results[0]\n", " choice = self.results[0]\n",
"\n", "\n",
" if domain in [\"product\"]:\n", " if domain in [\"payment\", \"delivery\", \"product\"]:\n",
" system_action[(domain, 'Recommend')].append(['Name', choice['name']])" " system_action[(domain, 'Recommend')].append(['Name', choice['name']])"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 61, "execution_count": 123,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"defaultdict(<class 'list'>, {('payment', 'inform'): [('type', 'karta')], ('delivery', 'inform'): [('type', 'paczkomat')], ('product', 'inform'): [('type', 'telefon')], ('product', 'request'): [('name', '?')]})\n",
"None\n",
"None\n",
"None\n",
"None\n"
]
},
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"[]" "[]"
] ]
}, },
"execution_count": 61, "execution_count": 123,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }