System_Dialogowy_Janet/Code/Modules/DP_module.py

122 lines
5.7 KiB
Python
Raw Permalink Normal View History

from collections import defaultdict
class DP():
2021-05-27 15:11:28 +02:00
"""
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 = []
2021-06-07 09:30:35 +02:00
system_action = defaultdict(dict)
user_action = defaultdict(list)
2021-06-06 12:09:20 +02:00
system_acts = []
while len(state['user_action']) > 0:
intent, domain, slot, value = state['user_action'].pop(0)
user_action[(domain, intent)].append((slot, value))
for user_act in user_action:
2021-06-06 12:09:20 +02:00
system_acts.append(self.update_system_action(user_act, user_action, state, system_action))
2021-05-31 15:28:48 +02:00
state['system_action'] = system_acts
2021-06-07 15:57:29 +02:00
print(state)
print("\n")
2021-05-31 15:28:48 +02:00
return system_acts
# # 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
# Reguła 1
if domain == 'appointment':
2021-05-31 15:28:48 +02:00
constraints = [(slot, value) for slot, value in state['belief_state'][domain.lower()].items() if value != '']
if len(constraints) == 0:
system_action[(domain, 'NoOffer')] = []
elif intent == 'appointment/create_appointment':
for x in constraints:
if(x[0] == 'doctor'):
2021-06-07 09:30:35 +02:00
system_action[(domain, 'book_appointent')][x[0]] = x[1]
for y in constraints:
if(y[0] == 'datetime'):
2021-06-07 09:30:35 +02:00
system_action[(domain, 'book_appointent')][y[0]] = y[1]
elif intent == 'appointment/set_date':
for y in constraints:
if(y[0] == 'datetime'):
2021-06-07 09:30:35 +02:00
system_action[(domain, 'set_appointment_date')][y[0]] = y[1]
2021-05-31 00:01:31 +02:00
#################################################################################################
# Reguła 2
if domain == 'request_information':
2021-05-31 00:01:31 +02:00
constraints_v2 = [(slot, value) for slot, value in state['request_state'][domain.lower()].items() if value != '']
if intent == 'request_information/available_dates':
2021-05-31 00:01:31 +02:00
for x in constraints_v2:
if(x[0] == 'doctor'):
2021-06-07 09:30:35 +02:00
system_action[(domain, 'date_info')][x[0]] = x[1]
2021-05-31 00:01:31 +02:00
for y in constraints_v2:
if(y[0] == 'datetime'):
2021-06-07 09:30:35 +02:00
system_action[(domain, 'date_info')][y[0]] = y[1]
elif intent == 'request_information/doctors':
2021-06-06 12:09:20 +02:00
system_action[("inform", 'Doctors_list')] = {"doctors": "dr. Kolano - okulista, dr. Kowalski - internista, dr. Kaszak - ginekolog"}
elif intent == 'request_information/location':
2021-06-06 12:09:20 +02:00
system_action[("inform", 'Location')] = {"street": "Marszałkowska", "number": "45", "city": "Poznań"}
elif intent == 'request_information/cost':
2021-06-06 12:09:20 +02:00
system_action[("inform", 'Cost')] = {"cost": "100 zł"}
elif intent == 'request_information/opening_hours':
2021-06-07 09:30:35 +02:00
system_action[("inform", 'Opening_Hours')] = {"hours": "'8:00 - 16:00"}
2021-06-14 09:00:32 +02:00
elif intent == 'request_information/medical_services':
system_action[("inform", 'Medical_services')] = {"services": "Okulista, Internista, Ginekolog"}
elif len(constraints) == 0:
2021-06-07 09:30:35 +02:00
system_action[("inform", 'NoOffer')] = {'0': '0'}
#################################################################################################
# Reguła 3
if domain == 'end_conversation':
2021-06-07 09:30:35 +02:00
system_action[(domain, 'End_Conversation')] = {'0': '0'}
#################################################################################################
# Reguła 4
if domain == 'greeting':
2021-06-07 09:30:35 +02:00
system_action[(domain, 'Greeting')] = {'0': '0'}
#################################################################################################
# Reguła 5
if domain == 'prescription':
2021-05-31 00:01:31 +02:00
constraints_v2 = [(slot, value) for slot, value in state['request_state'][domain.lower()].items() if value != '']
if intent == 'prescription/request':
2021-05-31 00:01:31 +02:00
for x in constraints_v2:
if(x[0] == 'prescription/type'):
2021-06-07 09:30:35 +02:00
system_action[(domain, 'create_prescription')][x[0]] = x[1]
2021-05-31 00:01:31 +02:00
for y in constraints_v2:
if(y[0] == 'prescription/medicine'):
2021-06-07 09:30:35 +02:00
system_action[(domain, 'create_prescription')][y[0]] = y[1]
elif intent == 'prescription/collect':
2021-05-31 00:01:31 +02:00
for x in constraints_v2:
if(x[0] == 'prescription/type'):
2021-06-07 09:30:35 +02:00
system_action[(domain, 'collect_prescription')][x[0]] = x[1]
elif intent == 'prescription/type':
2021-05-31 00:01:31 +02:00
for x in constraints_v2:
if(x[0] == 'prescription/type_info'):
2021-06-07 09:30:35 +02:00
system_action[(domain, 'prescription type')][x[0]] = x[1]
2021-05-31 00:01:31 +02:00
if len(constraints_v2) == 0:
system_action[(domain, 'NoOffer')] = []
2021-06-06 12:09:20 +02:00
return system_action
2021-05-31 15:28:48 +02:00
## Brakuje: Rejestracja, Login, Hasło, affirm, deny