From c9f0dfeb3a3a2c10c862a7bb4f3c0920feb9e2d3 Mon Sep 17 00:00:00 2001 From: Anna Nowak Date: Sun, 6 Jun 2021 12:09:20 +0200 Subject: [PATCH 1/3] wip --- Code/Janet.py | 4 ++-- Code/Modules/DP_module.py | 27 ++++++++++++--------------- Code/Modules/DST_module.py | 20 +++++--------------- Code/Modules/NLG_module.py | 24 ++++++++++++++++++++---- 4 files changed, 39 insertions(+), 36 deletions(-) diff --git a/Code/Janet.py b/Code/Janet.py index 8c54497..a7c99a2 100644 --- a/Code/Janet.py +++ b/Code/Janet.py @@ -28,8 +28,8 @@ class Janet: def process(self, command): act = self.nlu_v2.test_nlu(command) - return self.dp.predict(self.dst.update(act)) - #return self.nlg.change_to_text(dest_act) + system_acts = self.dp.predict(self.dst.update_user(act)) + return self.nlg.change_to_text(system_acts) def main(): diff --git a/Code/Modules/DP_module.py b/Code/Modules/DP_module.py index 6a9b08b..95839ca 100644 --- a/Code/Modules/DP_module.py +++ b/Code/Modules/DP_module.py @@ -1,7 +1,4 @@ from collections import defaultdict -import copy -import json -from copy import deepcopy class DP(): """ @@ -17,14 +14,14 @@ class DP(): self.results = [] system_action = defaultdict(list) user_action = defaultdict(list) - - for intent, domain, slot, value in state['user_action']: + 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: - self.update_system_action(user_act, user_action, state, system_action) + system_acts.append(self.update_system_action(user_act, user_action, state, system_action)) - 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 @@ -74,25 +71,25 @@ class DP(): if(y[0] == 'datetime'): system_action[(domain, 'date_info')].append([y[0], y[1]]) elif intent == 'request_information/doctors': - system_action[(domain, 'Doctors_list')] = [] + system_action[("inform", 'Doctors_list')] = {"doctors": "dr. Kolano - okulista, dr. Kowalski - internista, dr. Kaszak - ginekolog"} elif intent == 'request_information/location': - system_action[(domain, 'Location')] = [] + system_action[("inform", 'Location')] = {"street": "Marszałkowska", "number": "45", "city": "Poznań"} elif intent == 'request_information/cost': - system_action[(domain, 'Cost')] = [] + system_action[("inform", 'Cost')] = {"cost": "100 zł"} elif intent == 'request_information/opening_hours': - system_action[(domain, 'Opening_Hours')] = [] + system_action[("inform", 'Opening_Hours')] = ["hours", ['8:00 - 16:00']] elif len(constraints) == 0: - system_action[(domain, 'NoOffer')] = [] + system_action[("inform", 'NoOffer')] = [] ################################################################################################# # Reguła 3 if domain == 'end_conversation': - system_action[(domain, 'End_Conversation')].append(['0', '0']) + system_action[(domain, 'End_Conversation')] = ['0', '0'] ################################################################################################# # Reguła 4 if domain == 'greeting': - system_action[(domain, 'Greeting')].append(['0', '0']) + system_action[(domain, 'Greeting')] = ['0', '0'] ################################################################################################# # Reguła 5 @@ -116,6 +113,6 @@ class DP(): if len(constraints_v2) == 0: system_action[(domain, 'NoOffer')] = [] - #return system_action + return system_action ## Brakuje: Rejestracja, Login, Hasło, affirm, deny \ No newline at end of file diff --git a/Code/Modules/DST_module.py b/Code/Modules/DST_module.py index ffeda1d..ea03bca 100644 --- a/Code/Modules/DST_module.py +++ b/Code/Modules/DST_module.py @@ -11,7 +11,7 @@ class Rules_DST(): #Dialogue State Tracker def __init__(self): self.state = json.load(open('default_state.json')) - def update(self, user_acts=None): + def update_user(self, user_acts=None): for intent, domain, slot, value in user_acts: self.state["user_action"].append([intent, domain, slot, value]) if domain in ['password', 'name', 'email', 'enter_email', 'enter_name']: @@ -27,18 +27,6 @@ class Rules_DST(): #Dialogue State Tracker domain_dic = self.state['belief_state'][domain] if slot in domain_dic: self.state['belief_state'][domain][slot] = value - - if 'prescription' in intent: - if (slot == 'prescription'): - continue - - if(domain in slot): - slot.replace(domain + "/", '') - - if domain not in self.state['request_state']: - self.state['request_state'][domain] = {} - if slot not in self.state['request_state'][domain]: - self.state['request_state'][domain][slot] = value if 'request_information' in intent: if (slot == 'request_information'): @@ -51,7 +39,9 @@ class Rules_DST(): #Dialogue State Tracker self.state['request_state'][domain] = {} if slot not in self.state['request_state'][domain]: self.state['request_state'][domain][slot] = value - + + elif intent == 'end_conversation': + self.state = json.load(open('default_state.json')) print(self.state) print("\n") - return self.state + return self.state \ No newline at end of file diff --git a/Code/Modules/NLG_module.py b/Code/Modules/NLG_module.py index 50da059..e50c421 100644 --- a/Code/Modules/NLG_module.py +++ b/Code/Modules/NLG_module.py @@ -1,3 +1,5 @@ +import random + class NLG: """ Moduł, który tworzy reprezentację tekstową aktu systemowego wybranego przez taktykę dialogu. @@ -9,10 +11,24 @@ class NLG: def __init__(self): pass - def change_to_text(self, act_vector): + def change_to_text(self, system_acts): """ Funkcja zamieniająca akt systemu na tekst rozumiany przez użytkownika. """ - if(act_vector == [0, 0]): - return "Cześć, mam na imię Janet" - return "Nie rozumiem" \ No newline at end of file + if(len(system_acts) == 0): + return "Nie mam już nic do powiedzenia :(" + + act = system_acts.pop(0) + if(len(act) == 0): + return "Nie mam już nic do powiedzenia :(" + + for variable in act: + domain, intent = variable + + if (domain == "inform"): + return "test" + if(domain == "greeting"): + return random.choice(["Cześć, mam na imię Janet", "Hej, jestem Janet. W czym mogę pomóc?", "Dzień dobry, nazywam się Janet"]) + + elif(domain == "end_conversation"): + return random.choice(["Dziękujemy za skorzystanie z naszych usług!", "Do widzenia!", "Do zobaczenia!"]) From f201155d1a1cfb8ceab3576c7e08cb51781eb08d Mon Sep 17 00:00:00 2001 From: Anna Nowak Date: Mon, 7 Jun 2021 08:20:21 +0200 Subject: [PATCH 2/3] still wip --- Code/Janet.py | 6 +++--- Code/Modules/ML_NLU_module.py | 2 -- Code/Modules/NLG_module.py | 22 ++++++++++++++-------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Code/Janet.py b/Code/Janet.py index a7c99a2..f241e29 100644 --- a/Code/Janet.py +++ b/Code/Janet.py @@ -1,9 +1,9 @@ -import jsgf +#import jsgf from Modules.NLG_module import NLG from Modules.DP_module import DP from Modules.DST_module import Rules_DST -from Modules.Book_NLU_module import Book_NLU +# from Modules.Book_NLU_module import Book_NLU from Modules.ML_NLU_module import ML_NLU import random @@ -23,7 +23,7 @@ class Janet: self.nlg = NLG() self.dp = DP() self.dst = Rules_DST() - self.nlu = Book_NLU(jsgf.parse_grammar_file('book.jsgf')) + #self.nlu = Book_NLU(jsgf.parse_grammar_file('book.jsgf')) self.nlu_v2 = ML_NLU() def process(self, command): diff --git a/Code/Modules/ML_NLU_module.py b/Code/Modules/ML_NLU_module.py index c3aea1d..6c56529 100644 --- a/Code/Modules/ML_NLU_module.py +++ b/Code/Modules/ML_NLU_module.py @@ -1,5 +1,3 @@ -import jsgf -from tabulate import tabulate from flair.data import Sentence, Token from flair.datasets import SentenceDataset from flair.models import SequenceTagger diff --git a/Code/Modules/NLG_module.py b/Code/Modules/NLG_module.py index e50c421..e6dfd60 100644 --- a/Code/Modules/NLG_module.py +++ b/Code/Modules/NLG_module.py @@ -22,13 +22,19 @@ class NLG: if(len(act) == 0): return "Nie mam już nic do powiedzenia :(" - for variable in act: - domain, intent = variable + domain, intent = act[0] - if (domain == "inform"): - return "test" - if(domain == "greeting"): - return random.choice(["Cześć, mam na imię Janet", "Hej, jestem Janet. W czym mogę pomóc?", "Dzień dobry, nazywam się Janet"]) + if (domain == "inform"): + return "test" - elif(domain == "end_conversation"): - return random.choice(["Dziękujemy za skorzystanie z naszych usług!", "Do widzenia!", "Do zobaczenia!"]) + if(domain == "greeting"): + return random.choice(["Cześć, mam na imię Janet", "Hej, jestem Janet. W czym mogę pomóc?", "Dzień dobry, nazywam się Janet"]) + + if(domain == "end_conversation"): + return random.choice(["Dziękujemy za skorzystanie z naszych usług!", "Do widzenia!", "Do zobaczenia!"]) + + if(domain == "appointment"): + if(intent == "book_appointent"): + answer = "Zarezerwowano wizytę" + print(variable) + \ No newline at end of file From 39bdbe4cdbb59b39150b39813ef988d62c317ffe Mon Sep 17 00:00:00 2001 From: Anna Nowak Date: Mon, 7 Jun 2021 09:30:35 +0200 Subject: [PATCH 3/3] NLG --- Code/Modules/DP_module.py | 28 +++++++++---------- Code/Modules/DST_module.py | 16 +++++++++-- Code/Modules/NLG_module.py | 57 ++++++++++++++++++++++++++++++-------- default_state.json | 4 --- 4 files changed, 73 insertions(+), 32 deletions(-) diff --git a/Code/Modules/DP_module.py b/Code/Modules/DP_module.py index 95839ca..0db136c 100644 --- a/Code/Modules/DP_module.py +++ b/Code/Modules/DP_module.py @@ -12,7 +12,7 @@ class DP(): def predict(self, state): self.results = [] - system_action = defaultdict(list) + system_action = defaultdict(dict) user_action = defaultdict(list) system_acts = [] while len(state['user_action']) > 0: @@ -47,14 +47,14 @@ class DP(): elif intent == 'appointment/create_appointment': for x in constraints: if(x[0] == 'doctor'): - system_action[(domain, 'book_appointent')].append([x[0], x[1]]) + system_action[(domain, 'book_appointent')][x[0]] = x[1] for y in constraints: if(y[0] == 'datetime'): - system_action[(domain, 'book_appointent')].append([y[0], y[1]]) + system_action[(domain, 'book_appointent')][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]]) + system_action[(domain, 'set_appointment_date')][y[0]] = y[1] @@ -66,10 +66,10 @@ class DP(): if intent == 'request_information/available_dates': for x in constraints_v2: if(x[0] == 'doctor'): - system_action[(domain, 'date_info')].append([x[0], x[1]]) + system_action[(domain, 'date_info')][x[0]] = x[1] for y in constraints_v2: if(y[0] == 'datetime'): - system_action[(domain, 'date_info')].append([y[0], y[1]]) + system_action[(domain, 'date_info')][y[0]] = y[1] elif intent == 'request_information/doctors': system_action[("inform", 'Doctors_list')] = {"doctors": "dr. Kolano - okulista, dr. Kowalski - internista, dr. Kaszak - ginekolog"} elif intent == 'request_information/location': @@ -77,19 +77,19 @@ class DP(): elif intent == 'request_information/cost': system_action[("inform", 'Cost')] = {"cost": "100 zł"} elif intent == 'request_information/opening_hours': - system_action[("inform", 'Opening_Hours')] = ["hours", ['8:00 - 16:00']] + system_action[("inform", 'Opening_Hours')] = {"hours": "'8:00 - 16:00"} elif len(constraints) == 0: - system_action[("inform", 'NoOffer')] = [] + system_action[("inform", 'NoOffer')] = {'0': '0'} ################################################################################################# # Reguła 3 if domain == 'end_conversation': - system_action[(domain, 'End_Conversation')] = ['0', '0'] + system_action[(domain, 'End_Conversation')] = {'0': '0'} ################################################################################################# # Reguła 4 if domain == 'greeting': - system_action[(domain, 'Greeting')] = ['0', '0'] + system_action[(domain, 'Greeting')] = {'0': '0'} ################################################################################################# # Reguła 5 @@ -98,18 +98,18 @@ class DP(): if intent == 'prescription/request': for x in constraints_v2: if(x[0] == 'prescription/type'): - system_action[(domain, 'create_prescription')].append([x[0], x[1]]) + system_action[(domain, 'create_prescription')][x[0]] = x[1] for y in constraints_v2: if(y[0] == 'prescription/medicine'): - system_action[(domain, 'create_prescription')].append([y[0], y[1]]) + system_action[(domain, 'create_prescription')][y[0]] = y[1] elif intent == 'prescription/collect': for x in constraints_v2: if(x[0] == 'prescription/type'): - system_action[(domain, 'collect_prescription')].append([x[0], x[1]]) + system_action[(domain, 'collect_prescription')][x[0]] = x[1] elif intent == 'prescription/type': for x in constraints_v2: if(x[0] == 'prescription/type_info'): - system_action[(domain, 'prescription type')].append([x[0], x[1]]) + system_action[(domain, 'prescription type')][x[0]] = x[1] if len(constraints_v2) == 0: system_action[(domain, 'NoOffer')] = [] diff --git a/Code/Modules/DST_module.py b/Code/Modules/DST_module.py index ea03bca..fb64d92 100644 --- a/Code/Modules/DST_module.py +++ b/Code/Modules/DST_module.py @@ -27,6 +27,18 @@ class Rules_DST(): #Dialogue State Tracker domain_dic = self.state['belief_state'][domain] if slot in domain_dic: self.state['belief_state'][domain][slot] = value + + if 'prescription' in intent: + if (slot == 'prescription'): + continue + + if(domain in slot): + slot.replace(domain + "/", '') + + if domain not in self.state['request_state']: + self.state['request_state'][domain] = {} + if slot not in self.state['request_state'][domain]: + self.state['request_state'][domain][slot] = value if 'request_information' in intent: if (slot == 'request_information'): @@ -42,6 +54,6 @@ class Rules_DST(): #Dialogue State Tracker elif intent == 'end_conversation': self.state = json.load(open('default_state.json')) - print(self.state) - print("\n") + # print(self.state) + # print("\n") return self.state \ No newline at end of file diff --git a/Code/Modules/NLG_module.py b/Code/Modules/NLG_module.py index e6dfd60..778ba8c 100644 --- a/Code/Modules/NLG_module.py +++ b/Code/Modules/NLG_module.py @@ -1,4 +1,5 @@ import random +import uuid class NLG: """ @@ -22,19 +23,51 @@ class NLG: if(len(act) == 0): return "Nie mam już nic do powiedzenia :(" - domain, intent = act[0] + for action in act: + domain, intent = action + if(domain == "greeting"): + return random.choice(["Cześć, mam na imię Janet", "Hej, jestem Janet. W czym mogę pomóc?", "Dzień dobry, nazywam się Janet"]) - if (domain == "inform"): - return "test" + if(domain == "end_conversation"): + return random.choice(["Dziękujemy za skorzystanie z naszych usług!", "Do widzenia!", "Do zobaczenia!"]) - if(domain == "greeting"): - return random.choice(["Cześć, mam na imię Janet", "Hej, jestem Janet. W czym mogę pomóc?", "Dzień dobry, nazywam się Janet"]) + if(domain == "appointment"): + if (intent == "NoOffer"): + return "Proszę sprecyzować zapis na wizytę (lekarz, godzina)" + if (intent == "book_appointent"): + answer = "Zarezerwowano wizytę" + for key in act[action]: + value = act[action][key] + if(key == "doctor"): + answer += f" do lekarza {value}" + if(key== "datetime"): + answer += f" umówiona na: {value}" + return answer + if (intent == "set_appointment_date"): + value = act[action]["datetime"] + return f"Ustawiono datę wizyty na {value}" + + if(domain == "inform"): + if (intent == "Doctors_list"): + value = act[action]["doctors"] + return f"Lista doktorów w naszej placówce: \n{value}" + if (intent == "Location"): + city = act[action]["city"] + street = act[action]["street"] + number = act[action]["number"] + return f"Placówka znajduje się w {city}, {street} {number}" + if (intent == "Opening_Hours"): + value = act[action]["hours"] + return f"Placówka jest otwarta w godzinach {value}" + if (intent == "NoOffer"): + return f"Nie posiadam takiej wiedzy, żeby odpowiedzieć na to pytanie :(" - if(domain == "end_conversation"): - return random.choice(["Dziękujemy za skorzystanie z naszych usług!", "Do widzenia!", "Do zobaczenia!"]) + if(domain == "prescription"): + if(intent == "create_prescription" or intent == "collect_prescription"): + answer = f"Odebrano receptę {uuid.uuid4().hex}" + return answer + if(intent == "prescription type"): + return "Wszystkie recepty w naszej placówce są elektroniczne" + if (intent == "NoOffer"): + return f"Nie rozumiem :(" - if(domain == "appointment"): - if(intent == "book_appointent"): - answer = "Zarezerwowano wizytę" - print(variable) - \ No newline at end of file diff --git a/default_state.json b/default_state.json index 8d13a01..79e4881 100644 --- a/default_state.json +++ b/default_state.json @@ -16,10 +16,6 @@ "username": "", "password": "" }, - "prescription": { - "prescription/type": "", - "prescription/medicine": "" - }, "request_information":{ "doctor": "", "datetime": "",