System_Dialogowy_Janet/Code/Modules/DP_module.py

141 lines
5.9 KiB
Python

from collections import defaultdict
import copy
import json
from copy import deepcopy
class DP():
"""
Moduł decydujący o wyborze kolejnego aktu, który ma podjąć system prowadząc rozmowę.
Wejście: Reprezentacja stanu dialogu (rama)
Wyjście: Akt systemu (rama)
"""
def predict(self, state):
self.results = []
system_action = defaultdict(list)
user_action = defaultdict(list)
for intent, domain, slot, value in state['user_action']:
user_action[(domain, intent)].append((slot, value))
for user_act in user_action:
self.update_system_action(user_act, user_action, state, system_action)
# # Reguła 3
# if any(True for slots in user_action.values() for (slot, _) in slots if slot in ['Stay', 'Day', 'People']):
# if self.results:
# system_action = {('Booking', 'Book'): [["Ref", self.results[0].get('Ref', 'N/A')]]}
# system_acts = [[intent, domain, slot, value] for (domain, intent), slots in system_action.items() for slot, value in slots]
# state['system_action'] = system_acts
# return system_acts
def update_system_action(self, user_act, user_action, state, system_action):
domain, intent = user_act
constraints = [(slot, value) for slot, value in state['belief_state'][domain.lower()].items() if value != '']
print(domain)
print(intent)
print(constraints)
# Reguła 1
if domain == 'appointment':
if len(constraints) == 0:
system_action[(domain, 'NoOffer')] = []
elif intent == 'appointment/create_appointment':
for x in constraints:
if(x[0] == 'doctor'):
system_action[(domain, 'book_appointent')].append([x[0], x[1]])
for y in constraints:
if(y[0] == 'datetime'):
system_action[(domain, 'book_appointent')].append([y[0], y[1]])
elif intent == 'appointment/set_date':
for y in constraints:
if(y[0] == 'datetime'):
system_action[(domain, 'set_appointment_date')].append([y[0], y[1]])
#################################################################################################
# Reguła 2
if domain == 'request_information':
if intent == 'request_information/available_dates':
for x in constraints:
if(x[0] == 'doctor'):
system_action[(domain, 'date_info')].append([x[0], x[1]])
for y in constraints:
if(y[0] == 'datetime'):
system_action[(domain, 'date_info')].append([y[0], y[1]])
elif intent == 'request_information/doctors':
system_action[(domain, 'Doctors_list')] = []
elif intent == 'request_information/location':
system_action[(domain, 'Location')] = []
elif intent == 'request_information/cost':
system_action[(domain, 'Cost')] = []
elif intent == 'request_information/opening_hours':
system_action[(domain, 'Opening_Hours')] = []
elif len(constraints) == 0:
system_action[(domain, 'NoOffer')] = []
#################################################################################################
# Reguła 3
if domain == 'end_conversation':
system_action[(domain, 'End_Conversation')] = []
#################################################################################################
# Reguła 4
if domain == 'greeting':
system_action[(domain, 'Greeting')] = []
#################################################################################################
# Reguła 5
if domain == 'prescription':
if intent == 'prescription/request':
for x in constraints:
if(x[0] == 'prescription/type'):
system_action[(domain, 'create_prescription')].append([x[0], x[1]])
for y in constraints:
if(y[0] == 'prescription/medicine'):
system_action[(domain, 'create_prescription')].append([y[0], y[1]])
elif intent == 'prescription/collect':
for x in constraints:
if(x[0] == 'prescription/type'):
system_action[(domain, 'collect_prescription')].append([x[0], x[1]])
elif intent == 'prescription/type':
for x in constraints:
if(x[0] == 'prescription/type_info'):
system_action[(domain, 'prescription type')].append([x[0], x[1]])
if len(constraints) == 0:
system_action[(domain, 'NoOffer')] = []
print(system_action)
## Brakuje: Rejestracja, Login, Hasło, affirm, deny
# if len(constraints) == 0:
# system_action[(domain, 'NoOffer')] = []
# else:
# for slot in user_action[user_act]:
# kb_slot_name = REF_SYS_DA[domain].get(slot[0], slot[0])
# if kb_slot_name in self.results[0]:
# system_action[(domain, 'Inform')].append([slot[0], self.results[0].get(kb_slot_name, 'unknown')])
# # Reguła 2
# elif intent == 'Inform':
# if len(self.results) == 0:
# system_action[(domain, 'NoOffer')] = []
# else:
# system_action[(domain, 'Inform')].append(['Choice', str(len(self.results))])
# choice = self.results[0]
# if domain in ["Hotel", "Attraction", "Police", "Restaurant"]:
# system_action[(domain, 'Recommend')].append(['Name', choice['name']])