DST and DP

This commit is contained in:
Wojciech Lidwin 2023-05-16 23:31:26 +02:00
parent 61cbd2d005
commit 2d0ff0561e
3 changed files with 961 additions and 0 deletions

340
DST_DP.ipynb Normal file
View File

@ -0,0 +1,340 @@
{
"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
}

53
data.json Normal file
View File

@ -0,0 +1,53 @@
{
"user_action":[
],
"system_action":[
],
"belief_state":{
"food":{
"name":"",
"type":"",
"price range":"",
"size":"",
"ingredients":""
},
"drink":{
"name":"",
"price range":"",
"size":""
},
"sauce":{
"name":"",
"price range":"",
"size":""
},
"order":{
"type":"",
"price range":"",
"restaurant_name":"",
"area":"",
"book time":"",
"book day":""
},
"booking":{
"restaurant_name":"",
"area":"",
"book time":"",
"book day":"",
"book people":""
},
"payment":{
"type":"",
"amount":"",
"vat":""
}
},
"request_state":{
},
"terminated":false,
"history":[
]
}

568
database.json Normal file
View File

@ -0,0 +1,568 @@
{
"database":{
"food":[
{
"name":"pizza margherita",
"type":"pizza",
"price range":"średnia",
"size":"duża",
"ingredients":"sos pomidorowy, ser mozzarella, bazylia"
},
{
"name":"sałatka cezar",
"type":"sałatka",
"price range":"niska",
"size":"mała",
"ingredients":"sałata rzymska, grzanki, ser Parmezan, sos Cezar"
},
{
"name":"spaghetti bolognese",
"type":"makaron",
"price range":"średnia",
"size":"standardowa",
"ingredients":"mięso mielone, pomidory, cebula, czosnek"
},
{
"name":"kotlet schabowy",
"type":"mięso",
"price range":"wysoka",
"size":"standardowy",
"ingredients":"mięso schabowe, jajko, bułka tarta, mąka"
},
{
"name":"zupa pomidorowa",
"type":"zupa",
"price range":"niska",
"size":"mała",
"ingredients":"pomidory, cebula, czosnek, śmietana"
},
{
"name":"pierogi ruskie",
"type":"pierogi",
"price range":"średnia",
"size":"małe",
"ingredients":"ziemniaki, ser, cebula"
},
{
"name":"ryba Dorsz",
"type":"ryba",
"price range":"wysoka",
"size":"średnia",
"ingredients":"filet z dorsza, cytryna, masło"
},
{
"name":"krewetki na grillu",
"type":"owoce morza",
"price range":"wysoka",
"size":"małe",
"ingredients":"krewetki, oliwa, czosnek, przyprawy"
},
{
"name":"kurczak curry",
"type":"kurczak",
"price range":"średnia",
"size":"standardowy",
"ingredients":"kurczak, cebula, pomidory, przyprawy"
},
{
"name":"tarta szpinakowa",
"type":"tarta",
"price range":"średnia",
"size":"duża",
"ingredients":"szpinak, ser, jajka, śmietana"
},
{
"name":"kebab",
"type":"fast food",
"price range":"średnia",
"size":"duża",
"ingredients":"mięso drobiowe, warzywa, sos czosnkowy"
},
{
"name":"zupa ogórkowa",
"type":"zupa",
"price range":"niska",
"size":"średnia",
"ingredients":"ogórki kiszone, ziemniaki, śmietana, przyprawy"
},
{
"name":"sushi maki",
"type":"sushi",
"price range":"wysoka",
"size":"mała",
"ingredients":"ryż sushi, wodorosty nori, krewetki, awokado, ogórek"
},
{
"name":"pierogi z serem",
"type":"pierogi",
"price range":"niska",
"size":",ały",
"ingredients":"ciasto pierogowe, ser biały"
},
{
"name":"calamari",
"type":"owoce morza",
"price range":"wysoka",
"size":"mała",
"ingredients":"kalmary, mąka, jajko, przyprawy"
},
{
"name":"karp smażony",
"type":"ryba",
"price range":"średnia",
"size":"duża",
"ingredients":"karp, mąka, olej, przyprawy"
},
{
"name":"pizza vegetariana",
"type":"pizza",
"price range":"wysoka",
"size":"duża",
"ingredients":"sos pomidorowy, mozzarella, warzywa (papryka, cebula, pomidory, pieczarki), oregano"
},
{
"name":"sałatka z grillowanym kurczakiem",
"type":"sałatka",
"price range":"średnia",
"size":"średnia",
"ingredients":"mieszana sałata, grillowany filet z kurczaka, pomidory, ogórki, papryka, dressing"
},
{
"name":"steak z wieprzowiny",
"type":"mięso",
"price range":"wysoka",
"size":"duża",
"ingredients":"filet z wieprzowiny, masło, przyprawy"
},
{
"name":"ravioli ze szpinakiem i ricottą",
"type":"makaron",
"price range":"średnia",
"size":"średnia",
"ingredients":"makaron ravioli, nadzienie ze szpinaku i ricotty, sos pomidorowy"
},
{
"name":"kotlet mielony",
"type":"mięso",
"price range":"niska",
"size":"mała",
"ingredients":"mięso mielone, bułka tarta, jajko, przyprawy"
},
{
"name":"zupa grzybowa",
"type":"zupa",
"price range":"średnia",
"size":"duża",
"ingredients":"grzyby, cebula, śmietana, przyprawy"
},
{
"name":"sushi nigiri",
"type":"sushi",
"price range":"wysoka",
"size":"mała",
"ingredients":"ryż sushi, kawałek świeżego ryby"
},
{
"name":"pierogi z mięsem",
"type":"pierogi",
"price range":"średnia",
"size":"średnia",
"ingredients":"ciasto pierogowe, mięso mielone, cebula"
},
{
"name":"krewetki w panierce",
"type":"owoce morza",
"price range":"wysoka",
"size":"mała",
"ingredients":"krewetki, jajko, bułka tarta, przyprawy"
},
{
"name":"filet z dorsza",
"type":"ryba",
"price range":"średnia",
"size":"duża",
"ingredients":"dorsz, masło, cytryna, przyprawy"
},
{
"name":"pizza hawajska",
"type":"pizza",
"price range":"wysoka",
"size":"duża",
"ingredients":"sos pomidorowy, mozzarella, szynka, ananas, oregano"
},
{
"name":"sałatka cezar",
"type":"sałatka",
"price range":"średnia",
"size":"średnia",
"ingredients":"sałata rzymska, grzanki, Parmezan, sos Cezar"
},
{
"name":"stek z kurczaka",
"type":"mięso",
"price range":"średnia",
"size":"średnia",
"ingredients":"filet z kurczaka, oliwa, przyprawy"
},
{
"name":"lasagne",
"type":"makaron",
"price range":"średnia",
"size":"duża",
"ingredients":"makaron, sos pomidorowy, mięso mielone, ser, przyprawy"
},
{
"name":"dushi california roll",
"type":"sushi",
"price range":"wysoka",
"size":"mała",
"ingredients":"ryż sushi, wodorosty nori, krewetki, ogórek, awokado"
},
{
"name":"krewetki w sosie czosnkowym",
"type":"owoce morza",
"price range":"wysoka",
"size":"mała",
"ingredients":"krewetki, czosnek, masło, śmietana, natka pietruszki"
},
{
"name":"filet z łososia",
"type":"ryba",
"price range":"średnia",
"size":"duża",
"ingredients":"łosoś, oliwa z oliwek, cytryna, przyprawy"
},
{
"name":"pizza capricciosa",
"type":"pizza",
"price range":"wysoka",
"size":"duża",
"ingredients":"sos pomidorowy, mozzarella, szynka, pieczarki, oregano"
},
{
"name":"sałatka grecka",
"type":"sałatka",
"price range":"średnia",
"size":"średnia",
"ingredients":"sałata mieszana, pomidory, ogórki, cebula, oliwki, ser feta"
},
{
"name":"stek z wołowiny",
"type":"mięso",
"price range":"wysoka",
"size":"duża",
"ingredients":"wołowina, masło, przyprawy"
}
],
"drink":[
{
"name":"kawa",
"price range":"niska",
"size":"mała"
},
{
"name":"herbata",
"price range":"niska",
"size":"mała"
},
{
"name":"cola",
"price range":"średnia",
"size":"mała"
},
{
"name":"sok pomarańczowy",
"price range":"średnia",
"size":"mała"
},
{
"name":"woda gazowana",
"price range":"niska",
"size":"mała"
},
{
"name":"piwo jasne",
"price range":"średnia",
"size":"małe"
},
{
"name":"wino czerwone",
"price range":"wysoka",
"size":"standardowa"
},
{
"name":"koktajl owocowy",
"price range":"wysoka",
"size":"standardowy"
},
{
"name":"lemoniada",
"price range":"średnia",
"size":"duża"
},
{
"name":"piwo ciemne",
"price range":"średnia",
"size":"małe"
}
],
"sauce":[
{
"name":"sos pomidorowy",
"price range":"niska",
"size":"mała"
},
{
"name":"sos czosnkowy",
"price range":"niska",
"size":"mała"
},
{
"name":"sos bbq",
"price range":"średnia",
"size":"mała"
},
{
"name":"sos tatarski",
"price range":"średnia",
"size":"mała"
},
{
"name":"sos Meksykański",
"price range":"niska",
"size":"mała"
},
{
"name":"sos aioli",
"price range":"średnia",
"size":"mała"
},
{
"name":"sos śmietanowo-koperkowy",
"price range":"wysoka",
"size":"standardowa"
},
{
"name":"sos pikantny",
"price range":"wysoka",
"size":"standardowa"
},
{
"name":"sos curry",
"price range":"średnia",
"size":"duża"
},
{
"name":"sos hollandaise",
"price range":"średnia",
"size":"mała"
}
],
"order":[
{
"type":"dostawa",
"price range":"średnia",
"restaurant_name":"pizzeria bella",
"area":"centrum",
"book time":"19:00",
"book day":"25-05-2023"
},
{
"type":"na wynos",
"price range":"niska",
"restaurant_name":"restauracja smakosz",
"area":"praga",
"book time":"17:30",
"book day":"27-05-2023"
},
{
"type":"na miejscu",
"price range":"wysoka",
"restaurant_name":"grill & steakhouse",
"area":"śródmieście",
"book time":"20:00",
"book day":"29-05-2023"
},
{
"type":"dostawa",
"price range":"wysoka",
"restaurant_name":"restauracja orient",
"area":"mokotów",
"book time":"19:30",
"book day":"01-06-2023"
},
{
"type":"na wynos",
"price range":"niska",
"restaurant_name":"pizzeria italia",
"area":"ursynów",
"book time":"18:00",
"book day":"04-06-2023"
},
{
"type":"na miejscu",
"price range":"średnia",
"restaurant_name":"restauracja smaki azji",
"area":"wilanów",
"book time":"21:00",
"book day":"06-06-2023"
},
{
"type":"dostawa",
"price range":"wysoka",
"restaurant_name":"restauracja gusto",
"area":"bemowo",
"book time":"19:30",
"book day":"09-06-2023"
},
{
"type":"na wynos",
"price range":"średnia",
"restaurant_name":"restauracja la dolce vita",
"area":"żoliborz",
"book time":"18:30",
"book day":"12-06-2023"
},
{
"type":"na miejscu",
"price range":"niska",
"restaurant_name":"restauracja tradycja",
"area":"Oochota",
"book time":"20:30",
"book day":"15-06-2023"
},
{
"type":"dostawa",
"price range":"średnia",
"restaurant_name":"restauracja smażalnia",
"area":"wola",
"book time":"19:00",
"book day":"18-06-2023"
}
],
"booking":[
{
"restaurant_name":"restauracja finezja",
"area":"centrum",
"book time":"19:00",
"book day":"25-05-2023",
"book people":"4"
},
{
"restaurant_name":"trattoria bella",
"area":"praga",
"book time":"17:30",
"book day":"27-05-2023",
"book people":"2"
},
{
"restaurant_name":"steakhouse grill",
"area":"śródmieście",
"book time":"20:00",
"book day":"29-05-2023",
"book people":"6"
},
{
"restaurant_name":"sushi express",
"area":"mokotów",
"book time":"19:30",
"book day":"01-06-2023",
"book people":"3"
},
{
"restaurant_name":"pasta e pizza",
"area":"ursynów",
"book time":"18:00",
"book day":"04-06-2023",
"book people":"5"
},
{
"restaurant_name":"thai house",
"area":"wilanów",
"book time":"21:00",
"book day":"06-06-2023",
"book people":"2"
},
{
"restaurant_name":"restauracja u kowala",
"area":"bemowo",
"book time":"19:30",
"book day":"09-06-2023",
"book people":"4"
},
{
"restaurant_name":"trattoria italiana",
"area":"żoliborz",
"book time":"18:30",
"book day":"12-06-2023",
"book people":"3"
},
{
"restaurant_name":"kuchnia polska",
"area":"ochota",
"book time":"20:30",
"book day":"15-06-2023",
"book people":"6"
},
{
"restaurant_name":"restauracja mediterranean",
"area":"wola",
"book time":"19:00",
"book day":"18-06-2023",
"book people":"2"
}
],
"payment":[
{
"type":"gotówka",
"amount":"50 PLN",
"vat":"wliczony"
},
{
"type":"karta kredytowa",
"amount":"120 PLN",
"vat":"wliczony"
},
{
"type":"przelew bankowy",
"amount":"80 PLN",
"vat":"wliczony"
},
{
"type":"blik",
"amount":"25 PLN",
"vat":"wliczony"
},
{
"type":"apple pay",
"amount":"65 PLN",
"vat":"wliczony"
},
{
"type":"google pay",
"amount":"90 PLN",
"vat":"wliczony"
},
{
"type":"paypal",
"amount":"55 PLN",
"vat":"wliczony"
},
{
"type":"bon upominkowy",
"amount":"70 PLN",
"vat":"wliczony"
},
{
"type":"karta restauracyjna",
"amount":"40 PLN",
"vat":"wliczony"
},
{
"type":"gotówka",
"amount":"100 PLN",
"vat":"wliczony"
}
]
}
}