forked from s151636/Generic_DialogSystem
207 lines
6.3 KiB
Plaintext
207 lines
6.3 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 57,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import json\n",
|
|
"import os\n",
|
|
"\n",
|
|
"\n",
|
|
"class DST():\n",
|
|
" def __init__(self):\n",
|
|
" self.state = json.load(open('dictionary.json'))\n",
|
|
"\n",
|
|
" def update(self, user_act=None):\n",
|
|
" for intent, domain, slot, value in user_act:\n",
|
|
" domain = domain.lower()\n",
|
|
" intent = intent.lower()\n",
|
|
" slot = slot.lower()\n",
|
|
" \n",
|
|
" if domain not in self.state['belief_state']:\n",
|
|
" continue\n",
|
|
"\n",
|
|
" if intent == 'inform':\n",
|
|
" if slot == 'none' or slot == '':\n",
|
|
" continue\n",
|
|
"\n",
|
|
" domain_dic = self.state['belief_state'][domain]\n",
|
|
"\n",
|
|
" if slot in domain_dic:\n",
|
|
" self.state['belief_state'][domain][slot] = value\n",
|
|
"\n",
|
|
" elif intent == 'request':\n",
|
|
" if domain not in self.state['request_state']:\n",
|
|
" self.state['request_state'][domain] = {}\n",
|
|
" if slot not in self.state['request_state'][domain]:\n",
|
|
" self.state['request_state'][domain][slot] = 0\n",
|
|
" \n",
|
|
" self.state['user_action'] = user_act\n",
|
|
" return self.state"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 58,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"dst = DST()\n",
|
|
"user_act = [('inform', 'payment', 'type', 'credit card'), ('inform', 'product', 'name', 'iPhone'), ('request', 'product', 'type', 'warzywo')]\n",
|
|
"state = dst.update(user_act)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 59,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"{'payment': {'type': 'credit card', 'amount': '', 'loyalty_card': ''}, 'delivery': {'type': '', 'address': '', 'time': ''}, 'product': {'name': 'iPhone', 'type': '', 'brand': '', 'price': '', 'quantity': '', 'quality': ''}}\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": [
|
|
"print(state['belief_state'])\n",
|
|
"print(state['request_state'])\n",
|
|
"dst.state"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 60,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from collections import defaultdict\n",
|
|
"import copy\n",
|
|
"import json\n",
|
|
"from copy import deepcopy\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"class SimpleRulePolicy():\n",
|
|
" def __init__(self):\n",
|
|
" self.db = json.load(open('product_db.json'))\n",
|
|
"\n",
|
|
" def predict(self, state):\n",
|
|
" self.results = []\n",
|
|
" system_action = defaultdict(list)\n",
|
|
" user_action = defaultdict(list)\n",
|
|
"\n",
|
|
" for intent, domain, slot, value in state['user_action']:\n",
|
|
" user_action[(domain.lower(), intent.lower())].append((slot.lower(), value))\n",
|
|
"\n",
|
|
" for user_act in user_action:\n",
|
|
" self.update_system_action(user_act, user_action, state, system_action)\n",
|
|
"\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",
|
|
" state['system_action'] = system_acts\n",
|
|
" return system_acts\n",
|
|
"\n",
|
|
" def update_system_action(self, user_act, user_action, state, system_action):\n",
|
|
" domain, intent = user_act\n",
|
|
"\n",
|
|
" # Reguła 1\n",
|
|
" if intent == 'request':\n",
|
|
" if len(self.results) == 0:\n",
|
|
" system_action[(domain, 'NoOffer')] = []\n",
|
|
" else:\n",
|
|
" for slot in user_action[user_act]: \n",
|
|
" if slot[0] in self.results[0]:\n",
|
|
" system_action[(domain, 'Inform')].append([slot[0], self.results[0].get(slot[0], 'unknown')])\n",
|
|
"\n",
|
|
" # Reguła 2\n",
|
|
" elif intent == 'inform':\n",
|
|
" if len(self.results) == 0:\n",
|
|
" system_action[(domain, 'NoOffer')] = []\n",
|
|
" else:\n",
|
|
" system_action[(domain, 'Inform')].append(['Choice', str(len(self.results))])\n",
|
|
" choice = self.results[0]\n",
|
|
"\n",
|
|
" if domain in [\"product\"]:\n",
|
|
" 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": {
|
|
"kernelspec": {
|
|
"display_name": "py38",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.8.16"
|
|
},
|
|
"orig_nbformat": 4
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|