systemy_dialogowe/SDMockup.ipynb
2023-04-18 19:57:56 +02:00

8.4 KiB

ASR

def asr(inputText: str) -> str:
    # Do something
    inputText

NLU

class NaturalLanguageUnderstanding:
    acts: dict[list[str], str] = {
        ( "potwierdzam", "dobrze", "ok" ): "ack",
        ("do widziena", "czesc", "koniec", "do zobaczenia"): "bye",
        ("cześć", "dzień dobry", "hello", "hej"): "hello",
        ("pomóc", "pomocy", "pomoc"): "help",
        ("zaprzeczam", "odrzucam"): "negate",
        ("alternatywny", "inne", "alternatywa", "inna"): "requalts",
        ("szczegółów", "informacji", "info", "informacje"): "reqmore",
        ("restart"): "restart",
        ("dziękuję", "dzięki"): "thankyou",
        ("tak", "chcę"): "confirm",
        ("nie chce"): "deny",
        ("basen", "parking", "śniadania", "osoby"): "inform",
        ("jaki","?", "czy", "jak", "ile", "co", "gdzie"): "request"
    }
    def __init__(self, text: str):
        self.text = text
        self.act = ""
        
        
    def get_dialog_act(self): 
        for word in self.text.lower().split():
            for key in NaturalLanguageUnderstanding.acts:
                if word in key:
                    self.act = NaturalLanguageUnderstanding.acts[key]
                    return
        self.act = "null"
            

nlu = NaturalLanguageUnderstanding("Jaki pokój proponujesz w tym hotelu?")
nlu.get_dialog_act()
nlu.act
'request'

DST

class DialogueStateTracker:
    
    slots_dict: dict[tuple[str], str] = {
        ("osoby", "ludzie", "osób", "osobowy"): "people",
        ("miasto", "miasta", "miejsowość", "poznań", "warszawa", "warszawie", "poznaniu", "kraków", "krakowie"): "city",
        ("basen", "parking", "śniadania"): "facilities",
        ("data", "datę"): "date",
        ("pokój", "pokoje"): "room"
    }
    
    def __init__(self, nlu: NaturalLanguageUnderstanding):
        self.slots = []
        self.act = nlu.act
        self.text = nlu.text
    
    def get_dialog_slots(self):
        for word in self.text.lower().split():
            for key in DialogueStateTracker.slots_dict:
                if word in key:
                    self.slots.append(DialogueStateTracker.slots_dict[key])
    
dst: DialogueStateTracker = DialogueStateTracker(nlu)
dst.get_dialog_slots()
dst.slots
['room']

Dialogue Policy

class DialoguePolicy:
    user_act_to_system_act_dict: dict[str, str] = {
        "ack": "reqmore",
        "bye": "bye",
        "hello": "welcomemsg",
        "help": "inform",
        "negate": "offer",
        "requalts": "offer",
        "reqmore": "inform",
        "restart": "welcomemsg",
        "thankyou": "reqmore",
        "confirm": "reqmore",
        "deny": "offer",
        "inform": "offer",
        "request": "inform",
        "null": "null"
    }
    
    def __init__(self, dst: DialogueStateTracker):
        self.user_text = dst.text
        self.user_act = dst.act
        self.user_slots = dst.slots
        self.system_act = ""
    
    def get_system_act(self):
        self.system_act = DialoguePolicy.user_act_to_system_act_dict[self.user_act]
        
dp: DialoguePolicy = DialoguePolicy(dst)
dp.get_system_act()
dp.system_act
'inform'

NLG

class NaturalLanguageGeneration:
    system_act_to_text = {
        "reqmore": "Informuje więcej o ",
        "bye": "Do widzenia",
        "welcomemsg": "Witaj w systemie rezerwacji hotelowych. W czym mogę pomóc?",
        "inform": "Informuje cię o ",
        "offer": "Co myślisz o hotlu z ",
        "reqmore": "Czy mogę jeszcze jakoś Ci pomóc?",
        "null": ""
    }
    user_slots_to_text = {
        "people": "pojemności pokoju",
        "city": "mieście",
        "facilities": "udogodnieniach",
        "date": "dacie",
        "room": "pokoju"
    }
    
    def __init__(self, dp: DialoguePolicy):
        self.user_text = dp.user_text
        self.user_act = dp.user_act
        self.user_slots = dp.user_slots
        self.system_act = dp.system_act
        self.system_text = ""
    
    def generate_system_text(self):
        text: str = NaturalLanguageGeneration.system_act_to_text[self.system_act]
        slots_transformed = [NaturalLanguageGeneration.user_slots_to_text[slot] for slot in self.user_slots]
        self.system_text = text + " i ".join(slots_transformed)
        
nlg: NaturalLanguageGeneration = NaturalLanguageGeneration(dp)
nlg.generate_system_text()
nlg.system_text
'Informuje cię o pokoju'