def interpret(data): if 'kb' in data and '(' in data['kb']: kb = data['kb'].encode() else: kb = open('/interpret/kb/kb.lisp').read().encode() if str(kb).count('(') != str(kb).count(')'): print('Mismatched parentheses in KB.', file=sys.stderr) return {'error': 'Mismatched parentheses in KB.'} out, err = run_commands(['compile kb'], kb) if 'error' in err: print(err, file=sys.stderr) return {'error': err} if 's' in data: sent = process_text(data['s']) out, err = run_commands(['tokenize', 'candc', 'boxer'], sent) parse = process_boxer(out, nonmerge) elif 'p' in data: if '(O ' not in data['p']: print('Parse does not contain \'(O \'.', file=sys.stderr) return {'error': 'Parse does not contain \'(O \'.'} parse = data['p'] else: return {'error': 'No sentence or parse found.'} cmd = 'phillip-lpsolve' if os.path.isfile('/interpret/ext/gurobi/license/gurobi.lic'): cmd = 'phillip-gurobi' data = parse.encode() + b'\n' out, err = run_commands([cmd], data) if 'error' in err: print('Running inference:\n', err, file=sys.stderr) return {'error': err} interpret = process_phillip(out) path = visualize_output(out) if path == 'error': return {'parse': parse, 'interpret': interpret, 'error': 'Failed to generate proof graph.'} j = {'parse': parse, 'interpret': interpret, 'graph': request.url_root + 'graph/' + path} with open(tempfile.gettempdir() + '/' + path + '.json', 'w') as jout: json.dump(j, jout) return j
def parse_api(): data = process_text(request.get_json(force=True)['s']) out, err = run_commands(['tokenize', 'candc', 'boxer'], data) #if err: # return jsonify({'error': err}) return jsonify({'parse': process_boxer(out, nonmerge)})
def interpret(data): # For simplicity of code, we recompile the KB regardless of whether # one is passed as input. if 'kb' in data and '(' in data['kb']: kb = data['kb'].encode() else: kb = open('/interpret/kb/kb.lisp').read().encode() out, err = run_commands(['compile kb'], kb) #if err: # return {'error': err} if 's' in data: sent = process_text(data['s']) out, err = run_commands(['tokenize', 'candc', 'boxer'], sent) #if err: # return {'error': err} parse = process_boxer(out, nonmerge) elif 'p' in data: parse = data['p'] else: return {'error': 'No sentence or parse found.'} cmd = 'phillip-lpsolve' if os.path.isfile('/interpret/ext/gurobi/license/gurobi.lic'): #cmd = 'phillip-gurobi-kbest' cmd = 'phillip-gurobi' data = parse.encode() + b'\n' out, err = run_commands([cmd], data) #if err: # # Phillip prints trivial messages to stderr. # sys.stderr.write('Running inference:\n%s\n' % err) interpret = process_phillip(out) path = visualize_output(out) if path == 'error': return {'parse': parse, 'interpret': interpret, 'error': 'Failed to generate proof graph.'} j = {'parse': parse, 'interpret': interpret, 'graph': request.url_root + 'graph/' + path} with open(tempfile.gettempdir() + '/' + path + '.json', 'w') as jout: json.dump(j, jout) return j