2022-06-07 20:06:36 +02:00
|
|
|
dialogue_state = {
|
|
|
|
# current context should be set to an act that is currently taking place
|
|
|
|
'current_context' : None,
|
|
|
|
# topics will be an array of dicts structured similar to this
|
|
|
|
# {'act': 'notifications', 'slots': [('time_when', None), ('liczba', ''), ('timeunit', None)]}
|
|
|
|
'topics': [],
|
2022-06-08 10:15:44 +02:00
|
|
|
# facts is a dict of contextual data such as selected repo and a timeframe
|
|
|
|
'facts': {
|
2022-06-14 21:51:44 +02:00
|
|
|
'repo' : None,
|
|
|
|
'time': None
|
2022-06-08 10:15:44 +02:00
|
|
|
},
|
2022-06-07 20:06:36 +02:00
|
|
|
'counter': 0
|
|
|
|
}
|
2022-04-19 22:53:05 +02:00
|
|
|
|
2022-06-07 20:06:36 +02:00
|
|
|
|
|
|
|
def monitor_stanu_dialogowego(frame):
|
2022-06-08 12:28:33 +02:00
|
|
|
not_pivot_acts = ['time',
|
|
|
|
'repo',
|
|
|
|
'howmany']
|
2022-06-07 20:06:36 +02:00
|
|
|
global dialogue_state
|
2022-04-19 22:53:05 +02:00
|
|
|
# Some frames can reset the dialogue state, like saying hello.
|
2022-06-07 20:06:36 +02:00
|
|
|
|
|
|
|
incr_counter(dialogue_state)
|
|
|
|
|
|
|
|
# Discarding acts that were not identified properly, but lets keep increasing the counter
|
|
|
|
if frame['act'] == 'null':
|
|
|
|
return dialogue_state
|
|
|
|
|
2022-06-08 08:21:55 +02:00
|
|
|
reset_state_if_needed(frame, dialogue_state)
|
2022-06-08 12:28:33 +02:00
|
|
|
if frame['act'] not in not_pivot_acts:
|
|
|
|
dialogue_state['current_context'] = frame['act']
|
2022-06-07 20:06:36 +02:00
|
|
|
append_or_merge_frame(frame, dialogue_state)
|
2022-06-08 12:28:33 +02:00
|
|
|
|
2022-04-19 22:53:05 +02:00
|
|
|
return dialogue_state
|
|
|
|
|
|
|
|
|
2022-06-07 20:06:36 +02:00
|
|
|
def append_or_merge_frame(frame, dialogue_state):
|
|
|
|
act = frame['act']
|
|
|
|
|
|
|
|
act_from_state = list(filter(lambda x: x['act'] == act, dialogue_state['topics']))
|
|
|
|
if len(act_from_state) > 0:
|
|
|
|
append_or_merge_slots(frame['slots'], act_from_state[0]['slots'])
|
|
|
|
else:
|
|
|
|
dialogue_state['topics'].append(frame)
|
|
|
|
|
|
|
|
|
2022-06-08 10:15:44 +02:00
|
|
|
def update_facts_if_needed(slot):
|
|
|
|
global dialogue_state
|
|
|
|
|
|
|
|
if slot[1] is None or slot[1] == '':
|
|
|
|
return
|
2022-06-08 12:28:33 +02:00
|
|
|
|
2022-06-08 10:15:44 +02:00
|
|
|
if slot[0] == 'repo':
|
|
|
|
dialogue_state['facts']['repo'] = slot[1]
|
|
|
|
elif slot[0] == 'time':
|
|
|
|
dialogue_state['facts']['time'] = slot[1]
|
|
|
|
|
|
|
|
|
2022-06-07 20:06:36 +02:00
|
|
|
def append_or_merge_slots(frame_slots, state_slots):
|
|
|
|
"""
|
|
|
|
This function expects both parameters to be an array
|
|
|
|
of tuples (slot_name, slot_value), and will merge
|
|
|
|
frame slots into state_slots (on unique slot names).
|
|
|
|
"""
|
|
|
|
for frame_slot in frame_slots:
|
|
|
|
merged = False
|
|
|
|
for i in range(len(state_slots)):
|
2022-06-08 10:15:44 +02:00
|
|
|
update_facts_if_needed(frame_slot[i])
|
2022-06-07 20:06:36 +02:00
|
|
|
if state_slots[i][0] == frame_slot[0]:
|
|
|
|
merged = True
|
|
|
|
# Do not merge empty incomming slots
|
|
|
|
if frame_slot[1] is not None and not len(frame_slot[1]) == 0:
|
|
|
|
state_slots[i] = frame_slot
|
|
|
|
break
|
|
|
|
|
|
|
|
if not merged:
|
|
|
|
state_slots.append(frame_slot)
|
|
|
|
|
|
|
|
|
|
|
|
def incr_counter(dialogue_state):
|
|
|
|
current_val = dialogue_state['counter']
|
|
|
|
dialogue_state['counter'] = current_val + 1
|
|
|
|
|
2022-06-08 08:20:49 +02:00
|
|
|
def reset_state_if_needed(frame, dialogue_state):
|
2022-05-25 12:52:02 +02:00
|
|
|
if frame['act'] == "hello" or frame['act'] == "bye":
|
2022-06-08 08:20:49 +02:00
|
|
|
dialogue_state['topics'] = []
|
|
|
|
dialogue_state['counter'] = 0
|
2022-06-08 10:15:44 +02:00
|
|
|
# should we reset 'facts' aswell?
|