DP is kinda working for "appointment" and "prescription"

This commit is contained in:
Dominik Strzako 2021-05-30 19:17:45 +02:00
parent f58a1b47c7
commit af7b8a8045
6 changed files with 176 additions and 39 deletions

View File

@ -28,9 +28,8 @@ class Janet:
def process(self, command): def process(self, command):
act = self.nlu_v2.test_nlu(command) act = self.nlu_v2.test_nlu(command)
self.dst.update(act) return self.dp.predict(self.dst.update(act))
dest_act = self.dp.choose_tactic(self.dst.transfer()) #return self.nlg.change_to_text(dest_act)
return self.nlg.change_to_text(dest_act)
def main(): def main():

View File

@ -1,4 +1,9 @@
class DP: 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ę. Moduł decydujący o wyborze kolejnego aktu, który ma podjąć system prowadząc rozmowę.
@ -6,12 +11,131 @@ class DP:
Wyjście: Akt systemu (rama) Wyjście: Akt systemu (rama)
""" """
def __init__(self):
pass
def choose_tactic(self, frame_list):
""" def predict(self, state):
Obieranie taktyki na podstawie aktów usera. Bardzo ważna jest kolejność dodawanych do frame_list wartości. self.results = []
""" system_action = defaultdict(list)
act_vector = [0, 0] user_action = defaultdict(list)
return act_vector
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']])

View File

@ -27,10 +27,20 @@ class Rules_DST(): #Dialogue State Tracker
domain_dic = self.state['belief_state'][domain] domain_dic = self.state['belief_state'][domain]
if slot in domain_dic: if slot in domain_dic:
self.state['belief_state'][domain][slot] = value self.state['belief_state'][domain][slot] = value
else:
print(slot) if 'prescription' in intent:
print(domain) if (slot == 'prescription'):
continue
if(domain in slot):
slot.replace(domain + "/", '')
domain_dic = self.state['belief_state'][domain]
if slot in domain_dic:
self.state['belief_state'][domain][slot] = value
elif intent == 'end_conversation': elif intent == 'end_conversation':
self.state = json.load(open('default_state.json')) self.state = json.load(open('default_state.json'))
print(self.state) print(self.state)
return self.state return self.state

View File

@ -69,7 +69,7 @@ slot_trainer = ModelTrainer(slot_tagger, slot_corpus)
slot_trainer.train('slot-model', slot_trainer.train('slot-model',
learning_rate=0.1, learning_rate=0.1,
mini_batch_size=32, mini_batch_size=32,
max_epochs=100, max_epochs=30,
train_with_dev=False) train_with_dev=False)
@ -77,5 +77,5 @@ frame_trainer = ModelTrainer(frame_tagger, frame_corpus)
frame_trainer.train('frame-model', frame_trainer.train('frame-model',
learning_rate=0.1, learning_rate=0.1,
mini_batch_size=32, mini_batch_size=32,
max_epochs=100, max_epochs=30,
train_with_dev=False) train_with_dev=False)

View File

@ -1,22 +1,22 @@
# text: Chciałem prosić o wypisanie kolejnej recepty na lek X # text: Chciałem prosić o wypisanie kolejnej recepty na lek X
# intent: appointment/request_prescription # intent: prescription/request
# slots: # slots:
1 chciałem appointment/request_prescription NoLabel 1 chciałem prescription/request NoLabel
2 prosić appointment/request_prescription NoLabel 2 prosić prescription/request NoLabel
3 o appointment/request_prescription NoLabel 3 o prescription/request NoLabel
4 wypisanie appointment/request_prescription NoLabel 4 wypisanie prescription/request NoLabel
5 kolejnej appointment/request_prescription NoLabel 5 kolejnej prescription/request NoLabel
6 recepty appointment/request_prescription B-prescription 6 recepty prescription/request B-prescription/type
7 na appointment/request_prescription NoLabel 7 na prescription/request NoLabel
8 lek appointment/request_prescription B-prescription/type 8 lek prescription/request B-prescription/medicine
9 x appointment/request_prescription I-prescription/type 9 x prescription/request I-prescription/medicine
# text: proszę o E-receptę # text: proszę o E-receptę
# intent: appointment/request_prescription # intent: prescription/request
# slots: # slots:
1 proszę appointment/request_prescription NoLabel 1 proszę prescription/request NoLabel
2 o appointment/request_prescription NoLabel 2 o prescription/request NoLabel
3 ereceptę appointment/request_prescription B-prescription 3 ereceptę prescription/request B-prescription/type
# text: dziękuję # text: dziękuję
# intent: end_conversation # intent: end_conversation
@ -97,7 +97,7 @@
# slots: # slots:
1 jakie request_information/available_dates NoLabel 1 jakie request_information/available_dates NoLabel
2 są request_information/available_dates NoLabel 2 są request_information/available_dates NoLabel
3 dostępne rrequest_information/available_datesquest_information/available_dates NoLabel 3 dostępne request_information/available_dates NoLabel
4 terminy request_information/available_dates NoLabel 4 terminy request_information/available_dates NoLabel
5 wizyt request_information/available_dates B-appointment 5 wizyt request_information/available_dates B-appointment
6 na request_information/available_dates NoLabel 6 na request_information/available_dates NoLabel
@ -117,8 +117,8 @@
6 na appointment/create_appointment NoLabel 6 na appointment/create_appointment NoLabel
7 12:00 appointment/create_appointment B-datetime 7 12:00 appointment/create_appointment B-datetime
8 dziękuję appointment/create_appointment B-end_conversation 8 dziękuję appointment/create_appointment B-end_conversation
9 to appointment/create_appointment NoLabel I-end_conversation 9 to appointment/create_appointment I-end_conversation
10 wszystko appointment/create_appointment NoLabel I-end_conversation 10 wszystko appointment/create_appointment I-end_conversation
# text: Witam Chciałbym zarezerwować wizytę u lekarza # text: Witam Chciałbym zarezerwować wizytę u lekarza
# intent: appointment/create_appointment # intent: appointment/create_appointment
@ -348,14 +348,14 @@
# slots: # slots:
1 chciałabym prescription/collect NoLabel 1 chciałabym prescription/collect NoLabel
2 odebrać prescription/collect NoLabel 2 odebrać prescription/collect NoLabel
3 receptę prescription/collect B-prescription 3 receptę prescription/collect B-prescription/type
# text: Chciałabym zamówić receptę # text: Chciałabym zamówić receptę
# intent: prescription/request # intent: prescription/request
# slots: # slots:
1 chciałabym prescription/request NoLabel 1 chciałabym prescription/request NoLabel
2 zamówić prescription/request NoLabel 2 zamówić prescription/request NoLabel
3 receptę prescription/request B-prescription 3 receptę prescription/request B-prescription/type
# text: Tak # text: Tak
# intent: affirm # intent: affirm
@ -735,12 +735,12 @@
# slots: # slots:
1 chcialbym prescription/collect NoLabel 1 chcialbym prescription/collect NoLabel
2 odebrac prescription/collect NoLabel 2 odebrac prescription/collect NoLabel
3 receptę prescription/collect B-prescription 3 receptę prescription/collect B-prescription/type
# text: e-receptę # text: e-receptę
# intent: prescription/type # intent: prescription/type_info
# slots: # slots:
1 e-receptę prescription/type B-prescription 1 e-receptę prescription/type_info B-prescription/type
# text: Tak # text: Tak
# intent: affirm # intent: affirm

View File

@ -15,6 +15,10 @@
"secondname": "", "secondname": "",
"username": "", "username": "",
"password": "" "password": ""
},
"prescription": {
"prescription/type": "",
"prescription/medicine": ""
} }
}, },
"request_state": {}, "request_state": {},