From 0595bba4d8ba8692179714ea3c1f30768d5ea6f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Ga=C5=82ka?= Date: Thu, 20 Apr 2023 22:38:00 +0200 Subject: [PATCH] Update 'nlu.py' --- nlu.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/nlu.py b/nlu.py index 4dfa29b..ab2c20a 100644 --- a/nlu.py +++ b/nlu.py @@ -1,11 +1,16 @@ import jsgf - -order_grammar = jsgf.parse_grammar_file('order.jsgf') -order_grammar +from os import listdir +from os.path import isfile, join -utterance = 'chciałbym zamowic pizze vesuvio XXL na dwie osoby' -matched = order_grammar.find_matching_rules(utterance) +gram_dir = './gramatyki/' +grammar_files = [file for file in listdir(gram_dir) if isfile(join(gram_dir, file))] + +grammars = [] + +for grammarFile in grammar_files: + grammar = jsgf.parse_grammar_file(gram_dir + grammarFile) + grammars.append(grammar) def get_dialog_act(rule): @@ -26,12 +31,19 @@ def get_slots(expansion, slots): get_slots(expansion.referenced_rule.expansion, slots) -def nlu(_utterance): - _matched = order_grammar.find_matching_rules(_utterance) +def nlu(utterance): + matched = None + for _grammar in grammars: + matched = _grammar.find_matching_rules(utterance) + if matched: + break - if _matched: + 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 + +print(nlu('chciałbym zamowic pizze vesuvio XXL na dwie osoby')) +print(nlu('na dowoz')) +print(nlu('dowoz'))