From e5fecb5716e885346c76e4b3d0bcc98706014dc8 Mon Sep 17 00:00:00 2001 From: Adrian Date: Sun, 5 Jun 2022 18:59:48 +0200 Subject: [PATCH] add modules --- modules/AJN.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ modules/DST.py | 27 +++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 modules/AJN.py create mode 100644 modules/DST.py diff --git a/modules/AJN.py b/modules/AJN.py new file mode 100644 index 0000000..4ace0dd --- /dev/null +++ b/modules/AJN.py @@ -0,0 +1,50 @@ +import jsgf +from os import listdir +from os.path import isfile, join + +mypath = "./grammar/" +onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))] + +grammars = [] + +for grammarFile in onlyfiles: + grammar = jsgf.parse_grammar_file(mypath + grammarFile) + grammars.append(grammar) + +class Ajn: + + + + def __init__(self, grammars = None): + self.grammars = grammars + + def get_dialog_act(rule): + slots = [] + get_slots(rule.expansion, slots) + return {'act': rule.grammar.name, 'slots': slots} + + def get_slots(expansion, slots): + if expansion.tag != '': + slots.append((expansion.tag, expansion.current_match)) + return + + for child in expansion.children: + get_slots(child, slots) + + if not expansion.children and isinstance(expansion, jsgf.NamedRuleRef): + get_slots(expansion.referenced_rule.expansion, slots) + + def nlu(utterance): + matched = None + for grammar in grammars: + matched = grammar.find_matching_rules(utterance) + if matched: + break + + if matched: + return get_dialog_act(matched[0]) + else: + return {'act': 'null', 'slots': []} + + +ajn = Ajn() \ No newline at end of file diff --git a/modules/DST.py b/modules/DST.py new file mode 100644 index 0000000..7b516f6 --- /dev/null +++ b/modules/DST.py @@ -0,0 +1,27 @@ +class Dst: + def __init__(self): + self.messages = [] + self.checklist = { + "ilosc": None, + "tytul": None, + "dzien": None, + "godzina": None + } + self.history = [] + + def store(self, message): + self.messages.append(message) + + def get_messages(self): + return self.messages + + def get_next_question(self): + for key, value in self.checklist.items(): + if value == None: + return key + + def save_answer(self, slots): + for slot in slots: + self.checklist[slot[0]] = slot[1] + + self.messages.append(slots) \ No newline at end of file