Systemy_dialogowe/semantic_parser/parser.py

66 lines
1.8 KiB
Python
Raw Permalink Normal View History

2022-05-23 12:32:16 +02:00
import jsgf
2022-05-23 12:50:42 +02:00
from os import listdir
from os.path import isfile, join
2022-05-23 12:32:16 +02:00
2022-05-23 12:50:42 +02:00
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)
2022-05-23 12:32:16 +02:00
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):
2022-05-23 12:50:42 +02:00
matched = None
for grammar in grammars:
matched = grammar.find_matching_rules(utterance)
if matched:
break
2022-05-23 12:32:16 +02:00
if matched:
return get_dialog_act(matched[0])
else:
return {'act': 'null', 'slots': []}
#result = nlu('jakie są dostępne funkcje')
2022-06-14 21:51:44 +02:00
#result = nlu('w czym mozesz mi pomoc')
2022-05-23 12:32:16 +02:00
2022-06-14 21:51:44 +02:00
#result = nlu('jakie sa nowe pull requesty')
2022-06-07 20:48:37 +02:00
#result = nlu('ile jest nowych pull requestow')
2022-05-23 12:50:42 +02:00
2022-05-24 00:15:15 +02:00
#result = nlu('pokaż mi nowe pow z lipca')
2022-06-14 21:51:44 +02:00
#result = nlu('przedstaw mi moje issues w Zajęcia AI')
2022-05-24 00:15:15 +02:00
2022-06-07 20:48:37 +02:00
#result = nlu('siemanko')
#result = nlu('zegnaj')
2022-06-14 21:51:44 +02:00
#result = nlu('Zajęcia AI')
#result = nlu('pokaz mi moje powiadomienia')
2022-05-24 00:15:15 +02:00
#result = nlu('pokaż mi testy od lutego w Zajęcia AI')
2022-06-07 20:48:37 +02:00
#result = nlu('powiedz co nowego na moim repozytorium Zajęcia AI')
2022-06-01 20:39:10 +02:00
#result = nlu('wyświetl mi trzy nowe powiadomienia w lutym')
2022-06-07 20:48:37 +02:00
#result = nlu('przedstaw nowe pull requesty')
#result = nlu('z ostatnich trzech miesiecy')
#result = nlu('ostatnich czterech')
2022-05-23 19:11:24 +02:00
2022-05-24 17:43:31 +02:00
#print(result)