grammar3 update

This commit is contained in:
Jakub Henyk 2023-05-04 15:28:41 +02:00
commit 16b6ac2794

View File

@ -1,44 +1,44 @@
import jsgf import jsgf
from pathlib import Path from pathlib import Path
import os import os
__location__ = Path().resolve() __location__ = Path().resolve()
book_grammar = jsgf.parse_grammar_file(os.path.join(__location__, "grammar3.jsgf")) book_grammar = jsgf.parse_grammar_file(os.path.join(__location__, "grammar3.jsgf"))
book_grammar book_grammar
def get_dialog_act(rule): def get_dialog_act(rule):
slots = [] slots = []
get_slots(rule.expansion, slots) get_slots(rule.expansion, slots)
return {'act': rule.grammar.name, 'slots': slots} return {'act': rule.grammar.name, 'slots': slots}
def get_slots(expansion, slots): def get_slots(expansion, slots):
if expansion.tag != '': if expansion.tag != '':
slots.append((expansion.tag, expansion.current_match)) slots.append((expansion.tag, expansion.current_match))
return return
for child in expansion.children: for child in expansion.children:
get_slots(child, slots) get_slots(child, slots)
if not expansion.children and isinstance(expansion, jsgf.NamedRuleRef): if not expansion.children and isinstance(expansion, jsgf.NamedRuleRef):
get_slots(expansion.referenced_rule.expansion, slots) get_slots(expansion.referenced_rule.expansion, slots)
def nlu(utterance): def nlu(utterance):
matched = book_grammar.find_matching_rules(utterance) matched = book_grammar.find_matching_rules(utterance)
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': []}
utterance = 'jakie napoje gazowane'.lower() utterance = 'jakie napoje gazowane'.lower()
matched = book_grammar.find_matching_rules(utterance) matched = book_grammar.find_matching_rules(utterance)
print(matched) print(matched)
nlu(utterance) nlu(utterance)
print(get_dialog_act(matched[0])) print(get_dialog_act(matched[0]))