Generic_DialogSystem/system3.py

223 lines
8.8 KiB
Python
Raw Permalink Normal View History

2023-06-15 18:55:41 +02:00
import spacy
2023-06-15 20:11:35 +02:00
import random
2023-06-15 18:55:41 +02:00
nlp = spacy.load("pl_core_news_md")
2023-06-15 19:29:26 +02:00
class DialogManager:
def __init__(self, nlu_module, dst_module):
self.nlu_module = nlu_module
self.dst_module = dst_module
2023-06-15 20:40:07 +02:00
def promotion(self):
r = random.randint(1, 4)
if r == 1:
promotion = "Mamy dzisiaj w promocji ser!"
elif r == 2:
promotion = "Aktualnie w promocji mamy jabłka!"
2023-06-15 21:01:59 +02:00
elif r == 3:
2023-06-15 20:40:07 +02:00
promotion = "Mleko w super cenie! Tylko dzisiaj!"
2023-06-15 21:01:59 +02:00
elif r == 4:
2023-06-15 20:40:07 +02:00
promotion = "Chipsy na imprezę w promocji!"
return promotion
2023-06-15 19:29:26 +02:00
def start_dialog(self):
self.dst_module.update_state([]) # Zerowanie stanu dialogowego
2023-06-15 20:40:07 +02:00
i = 0
2023-06-15 19:29:26 +02:00
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:
2023-06-15 20:11:35 +02:00
r = random.randint(1, 4)
if r == 1:
response = "Przepraszam, nie rozumiem. W czym mogę Ci pomóc?"
elif r == 2:
response = "Czy mógłbyś powtórzyć?"
elif r == 3:
response = "Nie rozumiem. Mógłbyś powtórzyć?"
elif r == 4:
response = "Nie umiem na to odpowiedzieć."
2023-06-15 19:29:26 +02:00
elif "hello" in acts[0].act_type:
2023-06-15 20:11:35 +02:00
r = random.randint(1, 4)
if r == 1:
response = "Witaj! W czym mogę Ci pomóc?"
elif r == 2:
response = "Dzień dobry! W czym mogę Ci pomóc?"
elif r == 3:
response = "Witaj! W czym mogę służyć?"
elif r == 4:
response = "Dzień dobry! Czego potrzebujesz?"
2023-06-15 19:29:26 +02:00
elif "inform" in acts[0].act_type:
response = self.generate_response(dialog_state)
elif "bye" in acts[0].act_type:
2023-06-15 20:11:35 +02:00
r = random.randint(1, 4)
if r == 1:
response = "Dziękuję za rozmowę. Miłego dnia!"
elif r == 2:
response = "Dziękuję. Miłego dnia!"
elif r == 3:
response = "Miłego dnia i do zobaczenia!"
elif r == 4:
response = "Dziękuję i do zobaczenia!"
print("Agent:", response)
2023-06-15 19:29:26 +02:00
break
2023-06-15 21:01:59 +02:00
elif "thankyou" in acts[0].act_type:
r = random.randint(1, 4)
if r == 1:
response = "Cieszę się, że mogę pomóc."
elif r == 2:
response = "Nie ma za problemu."
elif r == 3:
response = "Przyjemność po mojej stronie."
elif r == 4:
response = "Czy mógłbym pomóc w czymś jeszcze?"
elif "affirm" in acts[0].act_type:
response = "Czy mógłbym pomóc w czymś jeszcze?"
elif "deny" in acts[0].act_type:
response = "Czy mógłbym pomóc w czymś jeszcze?"
2023-06-16 11:56:34 +02:00
elif "request" in acts[0].act_type:
r = random.randint(1, 10)
response = f"Cena produktu wynosi {r}.99zł."
2023-06-15 19:29:26 +02:00
else:
response = "Nie rozumiem. Czym mogę Ci pomóc?"
print("Agent:", response)
2023-06-15 20:40:07 +02:00
i += 1
if i % 5 == 0:
print("Agent:", self.promotion())
2023-06-15 19:29:26 +02:00
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
2023-06-15 20:11:35 +02:00
r = random.randint(1, 4)
if r == 1:
response = "Rozumiem, potrzebujesz"
elif r == 2:
response = "Znalazłem produkt"
elif r == 3:
response = "Posiadamy"
elif r == 4:
response = "Wybieram"
2023-06-15 19:29:26 +02:00
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"
2023-06-15 20:11:35 +02:00
r = random.randint(1, 4)
if r == 1:
response += ". Jak mogę Ci jeszcze pomóc?"
elif r == 2:
response += ". Co mogę jeszcze dla Ciebie zrobić?"
elif r == 3:
response += ". W czym mogę jeszcze pomóc?"
elif r == 4:
response += ". Czy potrzebujesz czegoś jeszcze?"
2023-06-15 19:29:26 +02:00
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
2023-06-15 18:55:41 +02:00
product_type_rules = {
2023-06-15 21:01:59 +02:00
"pieczywo": ["chleb", "bułka", "bulka", "bulki", "rogalik", "rogalika", "bagietka", "bagietke"],
"owoce": ["jabłko", "jablko", "banan", "gruszka", "gruszke", "pomarańcza","pomarancze"],
"warzywa": ["marchewka", "ziemniak", "cebula", "cebule", "pomidor", "pomidora"],
"mięso": ["kurczak", "wołowina", "wolowine", "wieprzowina", "wieprzowine", "indyk"],
2023-06-15 20:31:43 +02:00
"produkty mrożone": ["lody", "frytki", "pierogi", "nuggetsy"],
2023-06-15 21:01:59 +02:00
"słodycze": ["czekolada", "czekolade","czekolady", "ciastko", "lizak", "guma do żucia", "gume do zucia"],
"przekąski": ["talarki", "paluszki","orzeszki", "chipsy"],
2023-06-15 20:31:43 +02:00
"przyprawy": ["sól", "sol", "pieprz", "oregano", "cynamon"],
2023-06-15 21:01:59 +02:00
"napoje": ["woda", "wode", "sok", "herbata", "herbate", "kawa", "kawe", "energetyk"],
"napoje alkoholowe": ["piwo", "wino", "wódka","wodke", "whisky"],
"higiena": ["pasta do zębów", "paste do zebow", "mydło", "mydlo", "szampon", "papier toaletowy"],
2023-06-15 20:31:43 +02:00
"chemia gospodarcza": ["płyn do naczyń", "plyn do naczyn", "proszek do prania", "odświeżacz powietrza", "odswiezacz powietrza"],
2023-06-15 21:01:59 +02:00
"inne": ["długopis", "dlugopis", "baterie", "srubokret", "śrubokręt", "nożyczki", "nozyczki"],
"nabiał": ["mleko", "ser", "śmietana","smietane"]
2023-06-15 18:55:41 +02:00
}
class DialogAct:
def __init__(self, act_type, slots=None):
self.act_type = act_type
self.slots = slots if slots else {}
2023-06-15 19:29:26 +02:00
class NLU:
def __init__(self):
pass
2023-06-15 19:50:31 +02:00
def extract_acts_and_slots(self, text):
2023-06-15 19:29:26 +02:00
doc = nlp(text)
acts = []
for token in doc:
2023-06-15 21:01:59 +02:00
if token.lower_ == "cześć" or token.lower_ == "witaj" or token.lower_ == "hej" or token.lower_ == "siema" or token.lower_ == "witam" or token.lower_ == "dzień":
2023-06-15 19:29:26 +02:00
acts.append(DialogAct("hello"))
2023-06-15 21:01:59 +02:00
elif token.lower_ == "widzenia" or token.lower_ == "żegnaj" or token.lower_ == "zobaczenia" or token.lower_ == "wszystko":
2023-06-15 19:29:26 +02:00
acts.append(DialogAct("bye"))
2023-06-15 21:01:59 +02:00
elif token.lower_ == "dziękuję" or token.lower_ == "dzięki" or token.lower_ == "podziękowania" or token.lower_ == "bóg":
2023-06-15 19:29:26 +02:00
acts.append(DialogAct("thankyou"))
2023-06-16 11:56:34 +02:00
elif token.lower_ == "proszę" or token.lower_ == "ile" or token.lower_ == "cena":
2023-06-15 19:29:26 +02:00
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"))
2023-06-15 21:01:59 +02:00
elif token.pos_ == "NOUN":
2023-06-15 19:29:26 +02:00
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)
return acts
2023-06-15 19:50:31 +02:00
def find_product_type(self, product):
2023-06-15 19:29:26 +02:00
for product_type, products in product_type_rules.items():
if product in products:
return product_type, product
return None, None
2023-06-15 18:55:41 +02:00
2023-06-15 19:29:26 +02:00
nlu = NLU()
dst = DialogStateTracker()
2023-06-15 19:50:31 +02:00
dm = DialogManager(nlu ,dst)
dm.start_dialog()