systemy_dialogowe/DST_DP.ipynb

400 lines
12 KiB
Plaintext
Raw Normal View History

2023-05-16 23:31:26 +02:00
{
"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",
2023-06-02 10:01:05 +02:00
" 'price_range': '',\n",
2023-05-16 23:31:26 +02:00
" 'size': '',\n",
" 'ingredients': ''},\n",
2023-06-02 10:01:05 +02:00
" 'drink': {'name': '', 'price_range': '', 'size': ''},\n",
" 'sauce': {'name': '', 'price_range': '', 'size': ''},\n",
2023-05-16 23:31:26 +02:00
" 'order': {'type': '',\n",
2023-06-02 10:01:05 +02:00
" 'price_range': '',\n",
2023-05-16 23:31:26 +02:00
" 'restaurant_name': '',\n",
" 'area': '',\n",
2023-06-02 10:01:05 +02:00
" 'book_time': '',\n",
" 'book_day': ''},\n",
2023-05-16 23:31:26 +02:00
" 'booking': {'restaurant_name': '',\n",
" 'area': '',\n",
2023-06-02 10:01:05 +02:00
" 'book_time': '',\n",
" 'book_day': '',\n",
" 'book_people': ''},\n",
2023-05-16 23:31:26 +02:00
" '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": [
2023-06-02 10:01:05 +02:00
"{'name': '', 'type': '', 'price_range': '', 'size': '', 'ingredients': ''}"
2023-05-16 23:31:26 +02:00
]
},
"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,
2023-06-02 10:01:05 +02:00
"id": "325a39f3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'name': '', 'price_range': '', 'size': 'duża'}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dst.state['belief_state']['drink']"
]
},
{
"cell_type": "code",
"execution_count": 6,
2023-05-16 23:31:26 +02:00
"id": "2a36fa8c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['inform', 'drink', 'size', 'duża']]"
]
},
2023-06-02 10:01:05 +02:00
"execution_count": 6,
2023-05-16 23:31:26 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dst.state['user_action']"
]
},
{
"cell_type": "code",
2023-06-02 10:01:05 +02:00
"execution_count": 7,
2023-05-16 23:31:26 +02:00
"id": "67fd77b2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'drink': {'price range': 0}}"
]
},
2023-06-02 10:01:05 +02:00
"execution_count": 7,
2023-05-16 23:31:26 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dst.update_user([['request', 'drink', 'price range', '?']])\n",
"dst.state['request_state']"
]
},
{
"cell_type": "code",
2023-06-02 10:01:05 +02:00
"execution_count": 8,
2023-05-16 23:31:26 +02:00
"id": "834ebb03",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'name': '',\n",
" 'type': 'pizza',\n",
2023-06-02 10:01:05 +02:00
" 'price_range': '',\n",
2023-05-16 23:31:26 +02:00
" 'size': 'duża',\n",
" 'ingredients': ''}"
]
},
2023-06-02 10:01:05 +02:00
"execution_count": 8,
2023-05-16 23:31:26 +02:00
"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",
2023-06-02 10:01:05 +02:00
"execution_count": 9,
"id": "802c9fe4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'name': '', 'price_range': 'średnia', 'size': 'standardowa'}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dst.update_user([['inform', 'sauce', 'size', 'standardowa'], ['inform', 'sauce', 'price_range', 'średnia']])\n",
"dst.state['belief_state']['sauce']"
]
},
{
"cell_type": "code",
"execution_count": 10,
2023-05-16 23:31:26 +02:00
"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",
2023-06-02 10:01:05 +02:00
" print(arg)\n",
" print(jmespath.search(f\"database.{domain}[?contains{arg} == `true` ]\", self.db) )\n",
2023-05-16 23:31:26 +02:00
" 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",
2023-06-01 22:16:19 +02:00
" if domain in [\"food\", \"drink\", \"sauce\"]:\n",
2023-05-16 23:31:26 +02:00
" system_action[(domain, 'Recommend')].append(['Name', choice['name']])\n",
2023-06-01 22:16:19 +02:00
" elif domain in [\"order\", \"booking\", \"payment\"]:\n",
" system_action[(domain, 'Recommend')].append(['Type', choice['type']])\n",
2023-05-16 23:31:26 +02:00
" return system_action\n",
" \n"
]
},
{
"cell_type": "code",
2023-06-02 10:01:05 +02:00
"execution_count": 11,
2023-05-16 23:31:26 +02:00
"id": "e587661a",
"metadata": {},
"outputs": [
2023-06-02 10:01:05 +02:00
{
"name": "stdout",
"output_type": "stream",
"text": [
"(size, 'duża')\n",
"[{'name': 'lemoniada', 'price_range': 'średnia', 'size': 'duża'}]\n",
"(type, 'pizza') && contains(size, 'duża')\n",
"[{'name': 'pizza margherita', 'type': 'pizza', 'price_range': 'średnia', 'size': 'duża', 'ingredients': 'sos pomidorowy, ser mozzarella, bazylia'}, {'name': 'pizza vegetariana', 'type': 'pizza', 'price_range': 'wysoka', 'size': 'duża', 'ingredients': 'sos pomidorowy, mozzarella, warzywa (papryka, cebula, pomidory, pieczarki), oregano'}, {'name': 'pizza hawajska', 'type': 'pizza', 'price_range': 'wysoka', 'size': 'duża', 'ingredients': 'sos pomidorowy, mozzarella, szynka, ananas, oregano'}, {'name': 'pizza capricciosa', 'type': 'pizza', 'price_range': 'wysoka', 'size': 'duża', 'ingredients': 'sos pomidorowy, mozzarella, szynka, pieczarki, oregano'}]\n",
"(price_range, 'średnia') && contains(size, 'standardowa')\n",
"[]\n"
]
},
2023-05-16 23:31:26 +02:00
{
"data": {
"text/plain": [
"defaultdict(list,\n",
2023-06-02 10:01:05 +02:00
" {('drink', 'Inform'): [['Choice', '1']],\n",
2023-05-16 23:31:26 +02:00
" ('drink', 'Recommend'): [['Name', 'lemoniada']],\n",
" ('food', 'Inform'): [['Choice', '4']],\n",
2023-06-02 10:01:05 +02:00
" ('food', 'Recommend'): [['Name', 'pizza margherita']],\n",
" ('sauce', 'NoOffer'): []})"
2023-05-16 23:31:26 +02:00
]
},
2023-06-02 10:01:05 +02:00
"execution_count": 11,
2023-05-16 23:31:26 +02:00
"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",
2023-06-01 22:16:19 +02:00
"version": "3.11.2"
2023-05-16 23:31:26 +02:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}