From 240ba123977d09fd05c23ed1aa28fe7617f05bc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20=C5=81ukasiewicz?= Date: Thu, 20 Apr 2023 21:25:55 +0200 Subject: [PATCH] Zaktualizuj 'nlu.py' --- nlu.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/nlu.py b/nlu.py index 066675c..4dfa29b 100644 --- a/nlu.py +++ b/nlu.py @@ -11,4 +11,27 @@ matched = order_grammar.find_matching_rules(utterance) def get_dialog_act(rule): slots = [] get_slots(rule.expansion, slots) - return {'act': rule.grammar.name, 'slots': slots} \ No newline at end of file + 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 = order_grammar.find_matching_rules(_utterance) + + if _matched: + return get_dialog_act(matched[0]) + else: + return {'act': 'null', 'slots': []} + +print(nlu('chciałbym zamowic pizze vesuvio XXL na dwie osoby')) \ No newline at end of file