Skip to content
This repository has been archived by the owner on Aug 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from jgordon/develop
Browse files Browse the repository at this point in the history
Better error handling.
  • Loading branch information
jgordon authored Jun 20, 2018
2 parents bacda08 + 5bde287 commit 362f357
Show file tree
Hide file tree
Showing 5 changed files with 5,542 additions and 5,622 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ENV PATH /opt/conda/bin:$PATH

RUN conda update -y conda

RUN conda install -y flask beautifulsoup4 lxml ftfy
RUN conda install -y flask beautifulsoup4 lxml ftfy ansi2html


# Add the C&C pipeline and compile.
Expand Down
25 changes: 19 additions & 6 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from flask import Flask, request, jsonify, send_file, send_from_directory, \
render_template
from ansi2html import Ansi2HTMLConverter

from process import process_phillip, process_boxer

Expand Down Expand Up @@ -130,7 +131,7 @@ def interpret_html():
except KeyError:
print('Bad interpretation:', file=sys.stderr)
print(interp, file=sys.stderr)
return 'error'
return error_html(interp.get('error', ''))

return graph_html(graph_id)

Expand All @@ -148,10 +149,14 @@ def interpret(data):
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 err:
if 'error' in err:
print(err, file=sys.stderr)
# return {'error': err}
return {'error': err}

if 's' in data:
sent = process_text(data['s'])
Expand All @@ -162,6 +167,9 @@ def interpret(data):

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.'}
Expand All @@ -173,9 +181,9 @@ def interpret(data):

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)
if 'error' in err:
print('Running inference:\n', err, file=sys.stderr)
return {'error': err}

interpret = process_phillip(out)

Expand Down Expand Up @@ -247,3 +255,8 @@ def license():
shell=True, stdout=sub.PIPE, stderr=sub.PIPE)

return jsonify({'response': p.stdout.decode()})


def error_html(err):
conv = Ansi2HTMLConverter(inline=True, scheme='mint-terminal')
return render_template('error.html', error=conv.convert(err, full=False))
24 changes: 24 additions & 0 deletions app/templates/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>

<html lang="en">

<head>
<meta charset="utf-8">
<title>Error</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400">
<link rel="stylesheet" type="text/css" media="all" href="/static/style.css">
</head>

<body>

<h1>Error</h1>

<section>
<pre>
{{error|safe}}
</pre>
</section>

</body>

</html>
101 changes: 67 additions & 34 deletions kb/kb.lisp
Original file line number Diff line number Diff line change
@@ -1,49 +1,82 @@
(B (name criminals-steal)
(=> (steal-vb e1 x y u)
(criminal-nn e2 x) :0.6))

(B (name criminals-kill)
(=> (kill e1 x y u)
(criminal-nn e2 x) :0.6))
;; Hypernym

(B (name kill-vb)
(=> (kill e1 x y u)
(kill-vb e1 x y u) :0.9))
(B (name poodle-is-dog)
(=> (poodle-nn e1 x :0.9)
(dog-nn e1 x)))

(B (name people-eat-meals)
(=> (meal e3 x2)
(^ (person e1 x1) (eat e2 x1 x2)) :0.9))
(B (name dog-is-animal)
(=> (dog-nn e1 x :0.9)
(animal-nn e2 x)))

;; Flatten the role and throw away the extraneous argument to 'eat'.
(B (name eat-vb)
(=> (eat e3 x1 x2)
(^ (vn-agent e1 e2 x1) (eat-vb e2 u1 x2 u2)) :0.9))
(B (name cat-is-animal)
(=> (cat-nn e1 x :0.9)
(animal-nn e2 x)))

(B (name dinner-is-a-meal)
(=> (meal e1 x)
(dinner-nn e2 x) :0.6))
(B (name dinner-is-meal)
(=> (dinner-nn e1 x :0.9)
(meal-nn e2 x)))

(B (name financial-bank)
(=> (financial-institution e1 x)
(bank-nn e2 x) :0.2))
(=> (financial-institution e1 x :0.4)
(bank-nn e2 x)))

(B (name river-bank)
(=> (river-bank e1 x)
(bank-nn e2 x) :0.8))
(=> (river-bank e1 x :0.6)
(bank-nn e2 x)))

;; Properties

(B (name friendly-adj)
(=> (friendly e2 x1)
(friendly-adj e1 x1) :0.9))
(=> (friendly e2 x1 :0.9)
(friendly-adj e1 x1)))

(B (name nice-people-are-friendly)
(=> (nice e2 x1 :0.9)
(friendly e1 x1)))


;; Typical actions

(B (name criminals-steal)
(=> (steal-vb e1 x y u :0.9)
(criminal-nn e2 x)))

(B (name criminals-kill)
(=> (kill e1 x y :0.9)
(criminal-nn e2 x)))

(B (name friendly-people-wave)
(=> (friendly e3 x1)
(^ (person e1 x1) (wave e2 x1)) :0.9))
(=> (^ (person e1 x1 :0.45) (friendly e3 x1 :0.45))
(wave e2 x1)))

;; Actions

(B (name kill-die)
(=> (kill e1 x y :0.9)
(die e2 y)))

(B (name die-vb)
(=> (die e1 x :0.9)
(die-vb e2 x)))

(B (name kill-vb)
(=> (kill e1 x y :0.9)
(kill-vb e1 x y u)))

(B (name people-eat-meals)
(=> (meal e3 x2 :0.9)
(^ (person e1 x1) (eat e2 x1 x2))))

;; Flatten the role and throw away the extraneous argument to 'eat'.
(B (name eat-vb)
(=> (eat e3 x1 x2 :0.9)
(^ (vn-agent e1 e2 x1) (eat-vb e2 u1 x2 u2))))

(B (name shoot-kill)
(=> (shoot-vb e1 x y :0.9)
(kill e2 x y)))

;; 'Theme' may be wrong, but it's what Boxer gives.
(B (name wave-vb)
(=> (wave e3 x1)
(^ (vn-theme e1 e2 x1) (wave-vb e2 u1 u2 u3)) :0.9))

(B (name nice-people-are-friendly)
(=> (nice e2 x1)
(friendly e1 x1) :0.9))
(=> (wave e3 x1 :0.9)
(^ (vn-theme e1 e2 x1) (wave-vb e2 u1 u2 u3))))
Loading

0 comments on commit 362f357

Please sign in to comment.