From a1b0d7a8eb4cfd5ea3233211d4683c70f7196a71 Mon Sep 17 00:00:00 2001 From: Eugene Date: Thu, 15 Jun 2023 19:29:26 +0200 Subject: [PATCH] update system3 --- system3.py | 159 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 127 insertions(+), 32 deletions(-) diff --git a/system3.py b/system3.py index 27ceee4..702f2c4 100644 --- a/system3.py +++ b/system3.py @@ -2,6 +2,81 @@ import spacy nlp = spacy.load("pl_core_news_md") +class DialogManager: + def __init__(self, nlu_module, dst_module): + self.nlu_module = nlu_module + self.dst_module = dst_module + + def start_dialog(self): + self.dst_module.update_state([]) # Zerowanie stanu dialogowego + + while True: + user_input = input("Użytkownik: ") + + # Wykorzystanie modułu NLU do ekstrakcji aktywności i slotów + acts = self.nlu_module.extract_acts_and_slots(user_input) + + # Aktualizacja stanu dialogowego za pomocą modułu DST + self.dst_module.update_state(acts) + dialog_state = self.dst_module.get_state() + + # Logika dialogu + if not acts: + response = "Przepraszam, nie rozumiem. Czym mogę Ci pomóc?" + elif "hello" in acts[0].act_type: + response = "Witaj! W czym mogę Ci pomóc?" + elif "inform" in acts[0].act_type: + response = self.generate_response(dialog_state) + elif "bye" in acts[0].act_type: + response = "Dziękuję za rozmowę. Miłego dnia!" + break + else: + response = "Nie rozumiem. Czym mogę Ci pomóc?" + + print("Agent:", response) + + def generate_response(self, dialog_state): + # Logika generowania odpowiedzi na podstawie stanu dialogowego + # Możesz dostosować tę logikę do swoich potrzeb + # Przykład: generowanie odpowiedzi na podstawie aktualnego stanu dialogowego + response = "Rozumiem, potrzebujesz" + + if "product type" in dialog_state and "product" in dialog_state: + product_type = dialog_state["product type"] + product = dialog_state["product"] + response += f" {product} z kategorii {product_type}" + elif "product type" in dialog_state: + product_type = dialog_state["product type"] + response += f" produkty z kategorii {product_type}" + else: + response += " informacji o twoich potrzebach" + + response += ". Jak mogę Ci jeszcze pomóc?" + + return response + +class DialogStateTracker: + def __init__(self): + self.dialog_state = {} + + def update_state(self, acts): + for act in acts: + if act.act_type == "hello": + self.dialog_state = {} + elif act.act_type == "bye": + self.dialog_state = {} + elif act.act_type == "inform": + slots = act.slots + for slot, value in slots.items(): + self.dialog_state[slot] = value + + def get_state(self): + return self.dialog_state + + + + + product_type_rules = { "pieczywo": ["chleb", "bułka", "rogalik", "bagietka"], "owoce": ["jabłko", "banan", "gruszka", "pomarańcza"], @@ -23,45 +98,65 @@ class DialogAct: self.act_type = act_type self.slots = slots if slots else {} -def extract_acts_and_slots(text): - doc = nlp(text) - acts = [] +class NLU: + def __init__(self): + pass - for token in doc: - if token.lower_ == "cześć": - acts.append(DialogAct("hello")) - elif token.lower_ == "do widzenia": - acts.append(DialogAct("bye")) - elif token.lower_ == "dziękuję": - acts.append(DialogAct("thankyou")) - elif token.lower_ == "proszę": - acts.append(DialogAct("request")) - elif token.lower_ == "powtórz": - acts.append(DialogAct("repeat")) - elif token.lower_ == "reset": - acts.append(DialogAct("restart")) - elif token.lower_ in ["tak", "oczywiście"]: - acts.append(DialogAct("affirm")) - elif token.lower_ in ["nie", "nie chcę"]: - acts.append(DialogAct("deny")) - elif token.pos_ == "NOUN": - product_type, product = find_product_type(token.lemma_) - if product_type and product: - act = DialogAct("inform", {"product type": product_type, "product": product}) - acts.append(act) + def extract_acts_and_slots(text): + doc = nlp(text) + acts = [] - return acts + for token in doc: + if token.lower_ == "cześć": + acts.append(DialogAct("hello")) + elif token.lower_ == "do widzenia": + acts.append(DialogAct("bye")) + elif token.lower_ == "dziękuję": + acts.append(DialogAct("thankyou")) + elif token.lower_ == "proszę": + acts.append(DialogAct("request")) + elif token.lower_ == "powtórz": + acts.append(DialogAct("repeat")) + elif token.lower_ == "reset": + acts.append(DialogAct("restart")) + elif token.lower_ in ["tak", "oczywiście"]: + acts.append(DialogAct("affirm")) + elif token.lower_ in ["nie", "nie chcę"]: + acts.append(DialogAct("deny")) + elif token.pos_ == "NOUN": + product_type, product = self.find_product_type(token.lemma_) + if product_type and product: + act = DialogAct("inform", {"product type": product_type, "product": product}) + acts.append(act) -def find_product_type(product): - for product_type, products in product_type_rules.items(): - if product in products: - return product_type, product + return acts - return None, None + def find_product_type(product): + for product_type, products in product_type_rules.items(): + if product in products: + return product_type, product + + return None, None text = "Cześć, chciałbym kupić mleko, sok oraz chleb" -acts = extract_acts_and_slots(text) +nlu = NLU() +acts = nlu.extract_acts_and_slots(text) for act in acts: print(f"Type: {act.act_type}") print(f"Slots: {act.slots}") print() + + +dst = DialogStateTracker() + +text = "Cześć, chciałbym kupić mleko, sok oraz chleb" +acts = nlu.extract_acts_and_slots(text) + + +dst.update_state(acts) + + +dialog_state = dst.get_state() + +print("Dialog State:") +print(dialog_state) \ No newline at end of file