Compare commits

..

No commits in common. "ce191d864cbb179b3e0b243a96ee5e54bc338812" and "e5a91db50d353e8f052fd511aefce4604bcbfc2c" have entirely different histories.

3 changed files with 15 additions and 416 deletions

View File

@ -1,342 +0,0 @@
{
"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\", \"sauce\"]:\n",
" system_action[(domain, 'Recommend')].append(['Name', choice['name']])\n",
" elif domain in [\"order\", \"booking\", \"payment\"]:\n",
" system_action[(domain, 'Recommend')].append(['Type', choice['type']])\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.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@ -282,10 +282,8 @@
" 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 [\"food\", \"drink\", \"sauce\"]:\n", " if domain in [\"food\", \"drink\", \"police\", \"sauce\", \"order\", \"booking\", \"payment\"]:\n",
" system_action[(domain, 'Recommend')].append(['Name', choice['name']])\n", " system_action[(domain, 'Recommend')].append(['Name', choice['name']])\n",
" elif domain in [\"order\", \"booking\", \"payment\"]:\n",
" system_action[(domain, 'Recommend')].append(['Type', choice['type']])\n",
" return system_action\n", " return system_action\n",
" \n" " \n"
] ]
@ -334,7 +332,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.11.2" "version": "3.8.16"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -3,7 +3,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 17,
"id": "c31a012f", "id": "9a772dd1",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -12,8 +12,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 28, "execution_count": 21,
"id": "21c8e0a4", "id": "54502dca",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -69,36 +69,21 @@
" if slot == \"book time\":\n", " if slot == \"book time\":\n",
" return f\"Godzina Twojego zamówienia to: {value}.\"\n", " return f\"Godzina Twojego zamówienia to: {value}.\"\n",
" if slot == \"book day\":\n", " if slot == \"book day\":\n",
" return f\"Dzień Twojego zamówienia to: {value}.\" \n", " return f\"Dzień Twojego zamówienia to: {value}.\"\n",
" \n",
"\n", "\n",
" elif intent == 'end_conversation':\n", " elif intent == 'end_conversation':\n",
" return random.choice(['Żegnam i dziękuję za skorzystanie z usługi.', 'Do widzenia.', \"Dziękuję za skorzystanie z usługi, do kolejnego razu.\"])\n", " return random.choice(['Żegnam i dziękuję za skorzystanie z usługi.', 'Do widzenia.', \"Dziękuję za skorzystanie z usługi, do kolejnego razu.\"])\n",
" elif intent == 'start_conversation':\n", " elif intent == 'start_conversation':\n",
" return random.choice(['Witaj, jestem asystentem dialogowym, dzięki któremu złożysz zamówienie w restuaracji.', 'Cześć, w czym mogę pomóc?', \"Witam w usłudze, w czym mogę pomóc?\"])\n", " return random.choice(['Witaj, jestem asystentem dialogowym, dzięki któremu złożysz zamówienie w restuaracji.', 'Cześć, w czym mogę pomóc?', \"Witam w usłudze, w czym mogę pomóc?\"])\n",
" elif intent == 'affirm':\n", " \n",
" return random.choice(['Tak.', 'Potwierdzam.', \"Mozliwe.\"])\n", " "
" elif intent == 'deny':\n",
" return random.choice(['Nie.', 'Nie potwierdzam.', \"Niemożliwe.\"]) \n",
" elif intent == 'recommend' and slot==\"name\":\n",
" if domain==\"food\":\n",
" return f\"Rekomendowany posiłek to: {value}.\"\n",
" if domain==\"drink\":\n",
" return f\"Rekomendowany napój to: {value}.\" \n",
" if domain==\"sauce\":\n",
" return f\"Rekomendowany sos to: {value}.\"\n",
" elif intent == 'recommend' and slot==\"type\":\n",
" if domain==\"booking\":\n",
" return f\"Rekomenduję następującą rezerwację stolika: {value}.\" \n",
" if domain==\"order\":\n",
" return f\"Rekomenduję takie zamówienie: {value}.\"\n",
" if domain==\"payment\":\n",
" return f\"Rekomendowana metoda płatności to: {value}.\" "
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 11,
"id": "185b85f0", "id": "8568ac20",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -119,7 +104,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 12,
"id": "b276b31f", "id": "c3fa168f",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -140,7 +125,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 13,
"id": "3ae7e57f", "id": "5ac835de",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -161,7 +146,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 19, "execution_count": 19,
"id": "90303149", "id": "6c06463d",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -182,7 +167,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 25, "execution_count": 25,
"id": "b78e9d4a", "id": "aeaa9968",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -200,52 +185,10 @@
"nlg(['start_conversation', '', '', ''])" "nlg(['start_conversation', '', '', ''])"
] ]
}, },
{
"cell_type": "code",
"execution_count": 29,
"id": "37782f98",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Rekomendowany posiłek to: Zupa grzybowa.'"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nlg(['recommend', 'food', 'name', 'zupa grzybowa'])"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "c74610a3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Rekomendowana metoda płatności to: blik.'"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nlg(['recommend', 'payment', 'type', 'blik'])"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "1f6859ea", "id": "e48c1960",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": []