From 9a33831c4c5906f78ce4f1d92666d3afb0c0081c Mon Sep 17 00:00:00 2001 From: s464962 Date: Tue, 23 Apr 2024 13:04:36 +0200 Subject: [PATCH] update NLU --- Main.py | 24 ++++++++++++------------ NaturalLanguageAnalyzer.py | 37 +++++++++++++++++++++++++++++++++---- grammar.jsgf | 21 +++++++++++++++++++++ 3 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 grammar.jsgf diff --git a/Main.py b/Main.py index 169e8a9..75dbbba 100644 --- a/Main.py +++ b/Main.py @@ -4,21 +4,21 @@ from DialoguePolicy import DialoguePolicy from NaturalLanguageGeneration import NaturalLanguageGeneration if __name__ == "__main__": - text = "Cześć, jak masz na imię?" + text = "chciałbym zarezerwować hotel" nla = NaturalLanguageAnalyzer() user_act = nla.process(text) print(user_act) - dst = DialogueStateTracker() - state = dst.dst(user_act) - print(state) - - dp = DialoguePolicy() - system_act = dp.policy(state) - print(system_act) - - nlg = NaturalLanguageGeneration() - response = nlg.nlg(system_act) - print(response) + # dst = DialogueStateTracker() + # state = dst.dst(user_act) + # print(state) + # + # dp = DialoguePolicy() + # system_act = dp.policy(state) + # print(system_act) + # + # nlg = NaturalLanguageGeneration() + # response = nlg.nlg(system_act) + # print(response) diff --git a/NaturalLanguageAnalyzer.py b/NaturalLanguageAnalyzer.py index 5ff3c06..2b94134 100644 --- a/NaturalLanguageAnalyzer.py +++ b/NaturalLanguageAnalyzer.py @@ -1,6 +1,35 @@ +import jsgf + + class NaturalLanguageAnalyzer: + # def process(self, text): + # user_act = None + # if ("imie" in text or "imię" in text) and "?" in text: + # user_act = "request(firstname)" + # return user_act + def process(self, text): - user_act = None - if ("imie" in text or "imię" in text) and "?" in text: - user_act = "request(firstname)" - return user_act + with open('grammar.jsgf', 'r', encoding='utf-8') as f: + content = f.read() + book_grammar = jsgf.parse_grammar_string(content) + matched = book_grammar.find_matching_rules(text) + if matched: + return self.get_dialog_act(matched[0]) + else: + return {'act': 'null', 'slots': []} + + def get_slots(self, expansion, slots): + if expansion.tag != '': + slots.append((expansion.tag, expansion.current_match)) + return + + for child in expansion.children: + self.get_slots(child, slots) + + if not expansion.children and isinstance(expansion, jsgf.NamedRuleRef): + self.get_slots(expansion.referenced_rule.expansion, slots) + + def get_dialog_act(self, rule): + slots = [] + self.get_slots(rule.expansion, slots) + return {'act': rule.grammar.name, 'slots': slots} diff --git a/grammar.jsgf b/grammar.jsgf new file mode 100644 index 0000000..1c768af --- /dev/null +++ b/grammar.jsgf @@ -0,0 +1,21 @@ +#JSGF V1.0 UTF-8 pl; + +grammar book; + +public = [chciałbym] zarezerwować [hotel] [] [] [] ; + + = na {day}; + + = dzisiaj | jutro | poniedziałek | wtorek | środę | czwartek | piątek | sobotę | niedzielę; + + = na [godzinę] {hour}; + + = []; + + = dziewiątą | dziesiątą | jedenastą | dwunastą; + + = pietnaście | trzydzieści; + + = (na | dla) {size} (osób | osoby); + + = dwie | dwóch | trzy | trzech | cztery | czterech | pięć | pieciu;