Systemy_dialogowe/system/taktyka_dialogu.py
2022-06-14 22:26:42 +02:00

119 lines
3.4 KiB
Python

from apimock import *
gramar_slots = {
# 'act': ((slot0, 'required'),
# (slot1, 'optional')
# )
'pullrequest': (('action', 'optional'),
('repo', 'required')
),
'brefing': (('action', 'optional'),
('repo', 'required')
),
'notifications': (('liczba', 'optional'),
('repo', 'required')
),
'tests': (('test', 'optional'),
('repo', 'required')
),
'time': (('time_when', 'required'),
('timeunit', 'required'),
('liczba','optional')
),
'issues': (('time_when','optional'),
('liczba','optional'),
('timeunit','optional'),
('repozytoria', 'required')
),
'number': (('liczba', 'required')
),
'help': (('functions', 'optional'),
('help', 'optional')
),
'repo': (('repozytoria', 'required')
),
'hello': (),
'bye': (),
}
def taktyka_dialogu(state, frame):
if frame['act'] == 'null':
print("Nie rozumiem :/")
return 'null'
if state['current_context'] in ['hello', 'pomoc', 'bye']:
return short_thread(state)
else:
return long_thread(state)
def short_thread(state):
if state['current_context'] == 'pomoc':
print(state['current_context'])
return state['current_context']
#TODO?
else:
print(state['current_context'])
return state['current_context']
def validate_repo(repo):
if repo not in listRepositories():
print('repo not in listRepositories')
if repo not in listPublicRepositories():
print('repo not in listPublicRepositories')
#validate_date
#if zakres dat
#if test/notification/issues/pullRequest non empty
def long_thread(state):
act = state['current_context']
print(act)
# slots = []
for topic in state['topics']:
if act == topic['act']:
slots = topic['slots']
required = gramar_slots[act]
req_args, opt_args = agregate(required, slots)
facts = state['facts']# osobno validuj repo z req i z facts
req_args = add_facts(req_args, facts)
opt_args = add_facts(opt_args, facts)
required_empty = check_req(req_args)
if required_empty:
return "QESTION", required_empty[0] #slot do uzupełnienia
return "ANSWER", req_args, opt_args #wyświetl użytkownikowi
def check_req(req_args):
req = []
for item in req_args:
if item[1] == None:
req.append(item)
return req
def add_facts(args, facts):
for key, value in facts.items():
for idx, item in enumerate(args):
if key == item[0] and value != None and item[1] == None:
args[idx] = value
if key == 'repo':
validate_repo(value)
return args
def agregate(grammar, slots):
req_args, opt_args = [], []
for item in grammar:
value = None
for slot in slots:
if slot[0] == item[0]:
value = None if slot[1] == '' else slot[1]
if item[1] == 'required':
req_args.append([item[0], value])
elif item[1] == 'optional':
opt_args.append([item[0], value])
return req_args, opt_args
#powiedz, że może dopytać o 'optional'
#gramar_slots musi mieć 100% gramatyk albo będzie pomijał sloty