added parser to system mockup
This commit is contained in:
parent
3f02c41b1f
commit
9d3f61db38
@ -1,34 +1,49 @@
|
||||
import re
|
||||
import jsgf
|
||||
from os import listdir
|
||||
from os.path import isfile, join
|
||||
|
||||
from acts import hello_act, name_request_act
|
||||
mypath = "../semantic_parser/gramatics/"
|
||||
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
|
||||
|
||||
hello = ['dzie[ńn] dobry', 'dobry wiecz[oó]r', 'witam', 'witaj', 'siema', 'elo', 'cze[śs][ćc]']
|
||||
request_name = ['imi[eę]', 'nazywasz']
|
||||
question = ['\?$']
|
||||
grammars = []
|
||||
|
||||
acts = {hello_act: [hello],
|
||||
name_request_act: [request_name, question]}
|
||||
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': []}
|
||||
|
||||
def analizator_jezyka_naturalnego(text):
|
||||
text = text_preprocess(text)
|
||||
frame = act_check(text)
|
||||
frame = nlu(text)
|
||||
return frame
|
||||
|
||||
def text_preprocess(text):
|
||||
text = text.lower()
|
||||
return text
|
||||
|
||||
def act_check(text):
|
||||
frame = []
|
||||
for act, requirements in acts.items():
|
||||
req = [check_synonyms(synonyms, text) for synonyms in requirements]
|
||||
if all(req):
|
||||
frame.append(act)
|
||||
return frame
|
||||
|
||||
def check_synonyms(synonyms, text):
|
||||
for expression in synonyms:
|
||||
match = re.search(expression, text)
|
||||
if match:
|
||||
return True
|
||||
return False
|
||||
|
@ -1,17 +1,22 @@
|
||||
from acts import hello_act
|
||||
|
||||
|
||||
dialogue_state = []
|
||||
iterator = 1
|
||||
|
||||
def monitor_stanu_dialogowego(frame):
|
||||
global iterator
|
||||
# Some frames can reset the dialogue state, like saying hello.
|
||||
reset_state_if_needed(frame)
|
||||
|
||||
list(map(lambda x: dialogue_state.append(x), frame))
|
||||
singleState = {
|
||||
"iteration": iterator,
|
||||
"frame": frame
|
||||
}
|
||||
dialogue_state.append(singleState)
|
||||
iterator += 1
|
||||
return dialogue_state
|
||||
|
||||
|
||||
def reset_state_if_needed(frame):
|
||||
if hello_act in frame:
|
||||
global iterator
|
||||
if frame['act'] == "bye":
|
||||
dialogue_state.clear()
|
||||
iterator = 1
|
||||
|
||||
|
@ -9,7 +9,9 @@ def main():
|
||||
while running:
|
||||
text = input('>>>')
|
||||
frame = analizator_jezyka_naturalnego(text)
|
||||
#print("Analizator: {}".format(frame))
|
||||
state = monitor_stanu_dialogowego(frame)
|
||||
#print("Stan: {}".format(state))
|
||||
response_frames = taktyka_dialogu(state, frame)
|
||||
generator_jezyka_nautalnego(response_frames)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user