Systemy_dialogowe/semantic_parser/parser.py
2022-05-24 17:43:31 +02:00

58 lines
1.5 KiB
Python

import jsgf
from os import listdir
from os.path import isfile, join
mypath = "./gramatics/"
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
grammars = []
for grammarFile in onlyfiles:
grammar = jsgf.parse_grammar_file(mypath + grammarFile)
grammars.append(grammar)
def get_dialog_act(rule):
slots = []
get_slots(rule.expansion, slots)
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 = None
for grammar in grammars:
matched = grammar.find_matching_rules(utterance)
if matched:
break
if matched:
return get_dialog_act(matched[0])
else:
return {'act': 'null', 'slots': []}
#result = nlu('jakie są dostępne funkcje')
#result = nlu('w czym możesz mi pomóc')
#result = nlu('jakie są nowe pull requesty')
#result = nlu('ile jest nowych pull requestów')
#result = nlu('pokaż mi nowe pow z lipca')
#result = nlu('pokaż mi nowe issues z dwóch dni w Zajęcia AI')
#esult = nlu('cześć')
#result = nlu('pokaż mi testy od lutego w Zajęcia AI')
#result = nlu('wyświetl mi test w sierpniu w Projekt - sklep')
#print(result)