update system3
This commit is contained in:
parent
5d9ea60358
commit
a1b0d7a8eb
99
system3.py
99
system3.py
@ -2,6 +2,81 @@ import spacy
|
|||||||
|
|
||||||
nlp = spacy.load("pl_core_news_md")
|
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 = {
|
product_type_rules = {
|
||||||
"pieczywo": ["chleb", "bułka", "rogalik", "bagietka"],
|
"pieczywo": ["chleb", "bułka", "rogalik", "bagietka"],
|
||||||
"owoce": ["jabłko", "banan", "gruszka", "pomarańcza"],
|
"owoce": ["jabłko", "banan", "gruszka", "pomarańcza"],
|
||||||
@ -23,6 +98,10 @@ class DialogAct:
|
|||||||
self.act_type = act_type
|
self.act_type = act_type
|
||||||
self.slots = slots if slots else {}
|
self.slots = slots if slots else {}
|
||||||
|
|
||||||
|
class NLU:
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
def extract_acts_and_slots(text):
|
def extract_acts_and_slots(text):
|
||||||
doc = nlp(text)
|
doc = nlp(text)
|
||||||
acts = []
|
acts = []
|
||||||
@ -45,7 +124,7 @@ def extract_acts_and_slots(text):
|
|||||||
elif token.lower_ in ["nie", "nie chcę"]:
|
elif token.lower_ in ["nie", "nie chcę"]:
|
||||||
acts.append(DialogAct("deny"))
|
acts.append(DialogAct("deny"))
|
||||||
elif token.pos_ == "NOUN":
|
elif token.pos_ == "NOUN":
|
||||||
product_type, product = find_product_type(token.lemma_)
|
product_type, product = self.find_product_type(token.lemma_)
|
||||||
if product_type and product:
|
if product_type and product:
|
||||||
act = DialogAct("inform", {"product type": product_type, "product": product})
|
act = DialogAct("inform", {"product type": product_type, "product": product})
|
||||||
acts.append(act)
|
acts.append(act)
|
||||||
@ -60,8 +139,24 @@ def find_product_type(product):
|
|||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
text = "Cześć, chciałbym kupić mleko, sok oraz chleb"
|
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:
|
for act in acts:
|
||||||
print(f"Type: {act.act_type}")
|
print(f"Type: {act.act_type}")
|
||||||
print(f"Slots: {act.slots}")
|
print(f"Slots: {act.slots}")
|
||||||
print()
|
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)
|
Loading…
Reference in New Issue
Block a user