From d0f9db73bddfda76dbc412005793268722cc45e3 Mon Sep 17 00:00:00 2001 From: Piotr Kopycki Date: Thu, 3 Jun 2021 17:48:12 +0200 Subject: [PATCH] NaturalLanguageGeneration update --- DialoguePolicy.py | 28 +++---- NaturalLanguageGeneration.py | 154 ++++++++++++++++++++++++++++++++--- main.py | 7 +- 3 files changed, 161 insertions(+), 28 deletions(-) diff --git a/DialoguePolicy.py b/DialoguePolicy.py index abd7595..fe46bcb 100644 --- a/DialoguePolicy.py +++ b/DialoguePolicy.py @@ -33,14 +33,14 @@ class DP: system_act = SystemAct(SystemActType.REQUEST, ['place']) self.DST.system_update(system_act) return system_act - elif 'description' not in slots: - system_act = SystemAct(SystemActType.REQUEST, ['description']) - self.DST.system_update(system_act) - return system_act elif 'participants' not in slots: system_act = SystemAct(SystemActType.REQUEST, ['participants']) self.DST.system_update(system_act) return system_act + elif 'description' not in slots: + system_act = SystemAct(SystemActType.REQUEST, ['description']) + self.DST.system_update(system_act) + return system_act else: system_act = SystemAct(SystemActType.CONFIRM_DOMAIN, slots) self.DST.system_update(system_act) @@ -62,14 +62,14 @@ class DP: system_act = SystemAct(SystemActType.REQUEST, ['place']) self.DST.system_update(system_act) return system_act - elif 'description' not in slots: - system_act = SystemAct(SystemActType.REQUEST, ['description']) - self.DST.system_update(system_act) - return system_act elif 'participants' not in slots: system_act = SystemAct(SystemActType.REQUEST, ['participants']) self.DST.system_update(system_act) return system_act + elif 'description' not in slots: + system_act = SystemAct(SystemActType.REQUEST, ['description']) + self.DST.system_update(system_act) + return system_act else: system_act = SystemAct(SystemActType.CONFIRM_DOMAIN, slots) self.DST.system_update(system_act) @@ -82,7 +82,7 @@ class DP: return system_act elif last_user_act == UserActType.NEGATE: self.DST.clear() - return SystemAct(SystemActType.REQMORE, []) + return SystemAct(SystemActType.REQMORE, ['create_meeting']) else: return SystemAct(SystemActType.NOT_UNDERSTOOD, []) # stan edycji spotkania @@ -174,7 +174,7 @@ class DP: return SystemAct(SystemActType.AFFIRM, ['update_meeting']) elif last_user_act == UserActType.NEGATE: self.DST.clear() - return SystemAct(SystemActType.REQMORE, []) + return SystemAct(SystemActType.REQMORE, ['meeting_to_update']) self.meeting_to_update = False if not self.meeting_to_update: if last_user_act == UserActType.CONFIRM: @@ -185,7 +185,7 @@ class DP: return system_act elif last_user_act == UserActType.NEGATE: self.DST.clear() - return SystemAct(SystemActType.REQMORE, []) + return SystemAct(SystemActType.REQMORE, ['meeting_to_update']) else: return SystemAct(SystemActType.NOT_UNDERSTOOD, []) # stan anulowania spotkania @@ -226,14 +226,14 @@ class DP: return system_act elif last_user_act == UserActType.NEGATE: self.DST.clear() - return SystemAct(SystemActType.REQMORE, []) + return SystemAct(SystemActType.REQMORE, ['cancel_meeting']) else: return SystemAct(SystemActType.NOT_UNDERSTOOD, []) # stan prośby o listę spotkań elif dialogue_state == UserActType.MEETING_LIST: if last_user_act == UserActType.NEGATE: self.DST.clear() - return SystemAct(SystemActType.REQMORE, []) + return SystemAct(SystemActType.REQMORE, ['meeting_list']) else: if 'date' in slots: system_act = SystemAct(SystemActType.MEETING_LIST, slots) @@ -247,7 +247,7 @@ class DP: elif dialogue_state == UserActType.FREE_TIME: if last_user_act == UserActType.NEGATE: self.DST.clear() - return SystemAct(SystemActType.REQMORE, []) + return SystemAct(SystemActType.REQMORE, ['free_time']) else: if 'date' in slots: system_act = SystemAct(SystemActType.FREE_TIME, slots) diff --git a/NaturalLanguageGeneration.py b/NaturalLanguageGeneration.py index 6e3e1fa..cbe8f1c 100644 --- a/NaturalLanguageGeneration.py +++ b/NaturalLanguageGeneration.py @@ -1,5 +1,6 @@ from SystemAct import SystemAct from SystemActType import SystemActType +from UserActType import UserActType class NLG: """ @@ -8,17 +9,146 @@ class NLG: Wyjście: Tekst """ - def __init__(self): - pass + def __init__(self, dst): + self.DST = dst - def toText(self, systemAct: SystemAct) -> str: - if systemAct.getActType() == SystemActType.WELCOME_MSG: - return "Cześć" - if systemAct.getActType() == SystemActType.INFORM: - if "name" in systemAct.getActParams(): - return "Nazywam się Janusz" - if systemAct.getActType() == SystemActType.BYE: - return "Do widzenia." - if systemAct.getActType() == SystemActType.NOT_UNDERSTOOD: - return "Nie rozumiem o czym mówisz." + def generateResponse(self, systemAct: SystemAct) -> str: + dialogue_state, last_user_act, _ = self.DST.get_dialogue_state() + slots = self.DST.get_dialogue_slots() + if dialogue_state == UserActType.CREATE_MEETING: + if systemAct.getActType() == SystemActType.REQUEST: + if "date" in systemAct.getActParams(): + return "W jakim dniu ma się odbyć to spotkanie?" + if "time" in systemAct.getActParams(): + return "W jakim czasie ma się odbyć to spotkanie?" + if "place" in systemAct.getActParams(): + return "Gdzie ma się odbyć to spotkanie?" + if "description" in systemAct.getActParams(): + return "Czy mam dodać jakiś opis do tego spotkania?" + if "participants" in systemAct.getActParams(): + return "Kto ma wziąć udział w spotkaniu?" + if systemAct.getActType() == SystemActType.CONFIRM_DOMAIN: + date = slots['date'] + time = slots['time'] + place = slots['place'] + part_list = slots['participants'] + part = "" + for p in part_list: + part += p + part += ", " + part = part[:-2] + desc = slots['description'] + return f'Czy mam dodać te spotkanie do kalendarza?\n' \ + f'Dzień: {date}\nCzas: {time}\nMiejsce: {place}\nUczestnicy: {part}\nOpis: {desc}' + + # TODO: nie sprawdzone - trudno wejść do tego stanu + elif dialogue_state == UserActType.UPDATE_MEETING: + if systemAct.getActType() == SystemActType.REQUEST: + if "date" in systemAct.getActParams(): + return "W jakim dniu miało się odbyć to spotkanie?" + if "time" in systemAct.getActParams(): + return "W jakim czasie miało się odbyć to spotkanie?" + # TODO dopracować po dodaniu DB + if systemAct.getActType() == SystemActType.CONFIRM_DOMAIN: + date = slots['date'] + time = slots['time'] + place = slots['place'] + part_list = slots['participants'] + part = "" + for p in part_list: + part += p + part += ", " + part = part[:-2] + desc = slots['description'] + return f'Spotkanie:\n' \ + f'Dzień: {date}\nCzas: {time}\nMiejsce: {place}\nUczestnicy: {part}\nOpis: {desc}' + + elif dialogue_state == UserActType.CANCEL_MEETING: + if systemAct.getActType() == SystemActType.REQUEST: + if "date" in systemAct.getActParams(): + return "W jakim dniu miało się odbyć to spotkanie?" + if "time" in systemAct.getActParams(): + return "W jakim czasie miało się odbyć to spotkanie?" + # TODO dopracować po dodaniu DB + if systemAct.getActType() == SystemActType.CONFIRM_DOMAIN: + date = slots['date'] + time = slots['time'] + # place = slots['place'] + # part_list = slots['participants'] + # part = "" + # for p in part_list: + # part += p + # part += ", " + # part = part[:-2] + # desc = slots['description'] + return f'Spotkanie:\n' \ + f'Dzień: {date}\nCzas: {time}' + + elif dialogue_state == UserActType.MEETING_LIST: + if systemAct.getActType() == SystemActType.REQUEST: + if "date" in systemAct.getActParams(): + return "Z jakiego okresu chcesz przejrzeć spotkania?" + # TODO: dopracować po dodaniu DB + if systemAct.getActType() == SystemActType.MEETING_LIST: + response = "" + for s in slots: + date = s['date'] + time = s['time'] + place = s['place'] + part_list = s['participants'] + part = "" + for p in part_list: + part += p + part += ", " + part = part[:-2] + desc = s['description'] + response += f'Spotkanie:\nDzień: {date}\nCzas: {time}\nMiejsce: {place}\nUczestnicy: {part}\nOpis: {desc}\n' + response += "--------------------" + return response + + elif dialogue_state == UserActType.FREE_TIME: + if systemAct.getActType() == SystemActType.REQUEST: + if "date" in systemAct.getActParams(): + return "W jakim okresie chcesz znaleźć wolny czas?" + # TODO: dopracować po dodaniu DB + if systemAct.getActType() == SystemActType.FREE_TIME: + response = "" + for s in slots: + date = s['date'] + time = s['time'] + response += f'Spotkanie:\nDzień: {date}\nCzas: {time}\n' + return response + + elif systemAct.getActType() == SystemActType.AFFIRM: + if "create_meeting" in systemAct.getActParams(): + return "Spotkanie zostało dodane" + if "update_meeting" in systemAct.getActParams(): + return "Spotkanie zostało zaktualizowane" + if "cancel_meeting" in systemAct.getActParams(): + return "Spotkanie zostało odwołane" + + elif systemAct.getActType() == SystemActType.REQMORE: + if "create_meeting" in systemAct.getActParams(): + return "Spotkanie zostało odrzucone. Mogę pomóc w czymś jeszcze?" + if "update_meeting" in systemAct.getActParams(): + return "Aktualizacja spotkania została anulowana. Mogę pomóc w czymś jeszcze?" + if "cancel_meeting" in systemAct.getActParams(): + return "Odwoływanie spotkania zostało anulowane. Mogę pomóc w czymś jeszcze?" + if "meeting_list" in systemAct.getActParams() or "free_time" in systemAct.getActParams(): + return "Mogę pomóc w czymś jeszcze?" + + else: + if systemAct.getActType() == SystemActType.WELCOME_MSG: + return "Cześć" + if systemAct.getActType() == SystemActType.REQMORE: + return "Czy mogę Ci w czymś jeszcze pomóc?" + if systemAct.getActType() == SystemActType.BYE: + return "Do widzenia." + if systemAct.getActType() == SystemActType.NOT_UNDERSTOOD: + return "Nie rozumiem o czym mówisz." + + # TODO: Not implemented in DP + # if systemAct.getActType() == SystemActType.INFORM: + # if "name" in systemAct.getActParams(): + # return "Nazywam się Janusz" raise Exception("SystemAct:{} not recognized".format(systemAct)) diff --git a/main.py b/main.py index 30b0867..7673aa1 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,7 @@ if __name__ == "__main__": nlu = NLU() dst = DST() dp = DP(dst) - nlg = NLG() + nlg = NLG(dst) while(1): user_input = input("Wpisz tekst: ") @@ -21,15 +21,18 @@ if __name__ == "__main__": state, last_user_act, last_system_act = dst.get_dialogue_state() slots = dst.get_dialogue_slots() system_act = dp.chooseTactic() + print('------ stan ------') print(state, last_user_act, last_system_act) print('------ przechowywane sloty ------') print(slots) print('------ wybrana akcja systemu ------') print(system_act) + system_response = nlg.generateResponse(system_act) + print('------ wygenerowana odpowiedź systemu ------') + print(system_response) print('-----------------------------------') print('-----------------------------------') - #text = nlg.toText(system_act) if system_act.getActType() == SystemActType.BYE: break