Update 'nlu.py'

This commit is contained in:
Patryk Gałka 2023-04-20 22:38:00 +02:00
parent 2f0d3350e9
commit 0595bba4d8

28
nlu.py
View File

@ -1,11 +1,16 @@
import jsgf import jsgf
from os import listdir
order_grammar = jsgf.parse_grammar_file('order.jsgf') from os.path import isfile, join
order_grammar
utterance = 'chciałbym zamowic pizze vesuvio XXL na dwie osoby' gram_dir = './gramatyki/'
matched = order_grammar.find_matching_rules(utterance) 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): def get_dialog_act(rule):
@ -26,12 +31,19 @@ def get_slots(expansion, slots):
get_slots(expansion.referenced_rule.expansion, slots) get_slots(expansion.referenced_rule.expansion, slots)
def nlu(_utterance): def nlu(utterance):
_matched = order_grammar.find_matching_rules(_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]) return get_dialog_act(matched[0])
else: else:
return {'act': 'null', 'slots': []} return {'act': 'null', 'slots': []}
print(nlu('chciałbym zamowic pizze vesuvio XXL na dwie osoby')) print(nlu('chciałbym zamowic pizze vesuvio XXL na dwie osoby'))
print(nlu('na dowoz'))
print(nlu('dowoz'))