341 lines
10 KiB
Plaintext
341 lines
10 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"id": "4af8e091",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import json\n",
|
||
|
"\n",
|
||
|
"class Rules_DST(): \n",
|
||
|
"\n",
|
||
|
" def __init__(self):\n",
|
||
|
" self.state = json.load(open('data.json'))\n",
|
||
|
"\n",
|
||
|
" def update_user(self, user_acts=None):\n",
|
||
|
" for intent, domain, slot, value in user_acts:\n",
|
||
|
" domain = domain.lower()\n",
|
||
|
" intent = intent.lower()\n",
|
||
|
" slot = slot.lower()\n",
|
||
|
" if intent == 'start_conversation':\n",
|
||
|
" continue\n",
|
||
|
"\n",
|
||
|
" elif intent == 'end_conversation':\n",
|
||
|
" self.state = json.load(open('data.json'))\n",
|
||
|
" elif domain not in self.state['belief_state']:\n",
|
||
|
" continue\n",
|
||
|
" \n",
|
||
|
" \n",
|
||
|
" elif 'inform' in intent:\n",
|
||
|
" if (slot == 'inform'):\n",
|
||
|
" continue\n",
|
||
|
" \n",
|
||
|
" if(domain in slot):\n",
|
||
|
" slot.replace(domain + \"/\", '')\n",
|
||
|
"\n",
|
||
|
" domain_dic = self.state['belief_state'][domain]\n",
|
||
|
" if slot in domain_dic:\n",
|
||
|
" self.state['belief_state'][domain][slot] = value\n",
|
||
|
" \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",
|
||
|
" else:\n",
|
||
|
" self.state['request_state'][domain][slot] = value\n",
|
||
|
" \n",
|
||
|
" elif intent == 'start_conversation':\n",
|
||
|
" self.state[\"user_action\"].append([intent, domain, slot, value])\n",
|
||
|
" continue\n",
|
||
|
"\n",
|
||
|
" elif intent == 'end_conversation':\n",
|
||
|
" self.state = json.load(open('data.json'))\n",
|
||
|
" \n",
|
||
|
" self.state[\"user_action\"].append([intent, domain, slot, value])\n",
|
||
|
" return self.state"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"id": "09903205",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"{'user_action': [],\n",
|
||
|
" 'system_action': [],\n",
|
||
|
" 'belief_state': {'food': {'name': '',\n",
|
||
|
" 'type': '',\n",
|
||
|
" 'price range': '',\n",
|
||
|
" 'size': '',\n",
|
||
|
" 'ingredients': ''},\n",
|
||
|
" 'drink': {'name': '', 'price range': '', 'size': ''},\n",
|
||
|
" 'sauce': {'name': '', 'price range': '', 'size': ''},\n",
|
||
|
" 'order': {'type': '',\n",
|
||
|
" 'price range': '',\n",
|
||
|
" 'restaurant_name': '',\n",
|
||
|
" 'area': '',\n",
|
||
|
" 'book time': '',\n",
|
||
|
" 'book day': ''},\n",
|
||
|
" 'booking': {'restaurant_name': '',\n",
|
||
|
" 'area': '',\n",
|
||
|
" 'book time': '',\n",
|
||
|
" 'book day': '',\n",
|
||
|
" 'book people': ''},\n",
|
||
|
" 'payment': {'type': '', 'amount': '', 'vat': ''}},\n",
|
||
|
" 'request_state': {},\n",
|
||
|
" 'terminated': False,\n",
|
||
|
" 'history': []}"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 2,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"dst = Rules_DST()\n",
|
||
|
"dst.state"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"id": "ec2b40d2",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"[]"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 3,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"dst.state['user_action']"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"id": "ca5ec2f3",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"{'name': '', 'type': '', 'price range': '', 'size': '', 'ingredients': ''}"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"dst.update_user([['star_conversation',\"\",\"\",\"\"], ['inform', 'drink', 'size', 'duża']])\n",
|
||
|
"dst.state['belief_state']['food']"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"id": "2a36fa8c",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"[['inform', 'drink', 'size', 'duża']]"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"dst.state['user_action']"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"id": "67fd77b2",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"{'drink': {'price range': 0}}"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"dst.update_user([['request', 'drink', 'price range', '?']])\n",
|
||
|
"dst.state['request_state']"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"id": "834ebb03",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"{'name': '',\n",
|
||
|
" 'type': 'pizza',\n",
|
||
|
" 'price range': '',\n",
|
||
|
" 'size': 'duża',\n",
|
||
|
" 'ingredients': ''}"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 7,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"dst.update_user([['inform', 'food', 'type', 'pizza'], ['inform', 'food', 'size', 'duża']])\n",
|
||
|
"dst.state['belief_state']['food']"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"id": "4b61083c",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"from collections import defaultdict\n",
|
||
|
"import jmespath\n",
|
||
|
"\n",
|
||
|
"class DP():\n",
|
||
|
" def __init__(self):\n",
|
||
|
" with open('database.json', encoding='utf-8-sig') as json_file:\n",
|
||
|
" self.db = json.load(json_file)\n",
|
||
|
" \n",
|
||
|
"\n",
|
||
|
" def predict(self, state):\n",
|
||
|
" self.results = []\n",
|
||
|
" system_action = defaultdict(list)\n",
|
||
|
" user_action = defaultdict(list)\n",
|
||
|
" system_acts = []\n",
|
||
|
" for idx in range(len(state['user_action'])):\n",
|
||
|
" intent, domain, slot, value = state['user_action'][idx]\n",
|
||
|
" user_action[(domain, intent)].append((slot, value))\n",
|
||
|
"\n",
|
||
|
" for user_act in user_action:\n",
|
||
|
" system_acts.append(self.update_system_action(user_act, user_action, state, system_action))\n",
|
||
|
" state['system_action'] = system_acts\n",
|
||
|
" return system_acts[-1]\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
" def update_system_action(self, user_act, user_action, state, system_action):\n",
|
||
|
" \n",
|
||
|
" domain, intent = user_act \n",
|
||
|
" \n",
|
||
|
" #Reguła 3\n",
|
||
|
" if intent == 'end_conversation':\n",
|
||
|
" return None\n",
|
||
|
" \n",
|
||
|
" constraints = [(slot, value) for slot, value in state['belief_state'][domain].items() if value != '']\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(constraints)>1:\n",
|
||
|
" arg=f\"{constraints[0]}\".replace(f\"\\'{constraints[0][0]}\\'\",f\"{constraints[0][0]}\")\n",
|
||
|
" arg = arg.replace(\"[\",\"\").replace(\"]\",\"\")\n",
|
||
|
" for cons in constraints[1:]:\n",
|
||
|
" arg+=f\" && contains{cons}\".replace(f\"\\'{cons[0]}\\'\",f\"{cons[0]}\").replace(\"[\",\"\").replace(\"]\",\"\")\n",
|
||
|
" else:\n",
|
||
|
" arg=f\"{constraints}\".replace(f\"\\'{constraints[0]}\\'\",f\"{constraints[0]}\").replace(\"[\",\"\").replace(\"]\",\"\").replace(\"(\\'\",\"(\").replace(\"\\',\",\",\") \n",
|
||
|
" self.results = jmespath.search(f\"database.{domain}[?contains{arg} == `true` ]\", self.db) \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 [\"food\", \"drink\", \"police\", \"sauce\", \"order\", \"booking\", \"payment\"]:\n",
|
||
|
" system_action[(domain, 'Recommend')].append(['Name', choice['name']])\n",
|
||
|
" return system_action\n",
|
||
|
" \n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"id": "e587661a",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"defaultdict(list,\n",
|
||
|
" {('drink', 'Inform'): [['Choice', '1'],\n",
|
||
|
" ['price range', 'średnia']],\n",
|
||
|
" ('drink', 'Recommend'): [['Name', 'lemoniada']],\n",
|
||
|
" ('food', 'Inform'): [['Choice', '4']],\n",
|
||
|
" ('food', 'Recommend'): [['Name', 'pizza margherita']]})"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 9,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"dp= DP()\n",
|
||
|
"dp.predict(dst.state)"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3 (ipykernel)",
|
||
|
"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"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 5
|
||
|
}
|