105 lines
3.1 KiB
Python
105 lines
3.1 KiB
Python
from apimock import *
|
|
|
|
gramar_slots = {
|
|
'pullrequest': (('action', 'optional'),
|
|
('repo', 'required')
|
|
),
|
|
'brefing': (('action', 'optional'),
|
|
('repo', 'required')
|
|
),
|
|
'notifications': (('liczba', 'optional'),
|
|
('repo', 'required')
|
|
),
|
|
'tests': (('test', 'optional'),
|
|
('repozytoria', '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':
|
|
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':
|
|
return state['current_context']
|
|
else:
|
|
return state['current_context']
|
|
|
|
def validate_repo(repo):
|
|
if repo not in listRepositories():
|
|
return 'repo not in listRepositories'
|
|
if repo not in listPublicRepositories():
|
|
return 'repo not in listPublicRepositories'
|
|
|
|
def long_thread(state):
|
|
act = state['current_context']
|
|
if act == None:
|
|
return 'null'
|
|
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 "QUESTION", required_empty[0] #slot do uzupełnienia
|
|
return "ANSWER", act, 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
|
|
|