Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Young authored and Michael Young committed Aug 22, 2016
0 parents commit 49dc1cc
Show file tree
Hide file tree
Showing 22 changed files with 1,186 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.DS_Store
*.gz
*.part
*.xml
*.pyc
*.egg-info
*.orig
.cache/
.idea/
stanford*/
data/
cache/
dist/
build/
nlquery-app/env*
17 changes: 17 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from nlquery.nlquery import NLQueryEngine
import os
import sys

def main(argv):
engine = NLQueryEngine('localhost', 9000)

while True:
try:
line = raw_input("Enter line: ")
print engine.query(line, format_='plain')
except EOFError:
print "Bye!"
sys.exit(0)

if __name__ == "__main__":
main(sys.argv[1:])
8 changes: 8 additions & 0 deletions nlquery-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Setup

```
pip install virtualenv
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
```
83 changes: 83 additions & 0 deletions nlquery-app/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import tornado.ioloop
import tornado.web
from tornado.options import define, options, parse_command_line
from nlquery.nlquery import NLQueryEngine
import os
import json

define("port", default=8888, help="run on the given port", type=int)
define("debug", default=False, help="run in debug mode")

nlquery = NLQueryEngine('localhost', 9000)

# https://gist.github.com/mminer/5464753
class JsonHandler(tornado.web.RequestHandler):
"""Request handler where requests and responses speak JSON."""
def prepare(self):
# Incorporate request JSON into arguments dictionary.
if self.request.body:
try:
json_data = json.loads(self.request.body)
self.request.arguments.update(json_data)
except ValueError:
print '%s' % str(self.request.body)
message = 'Unable to parse JSON.'
self.send_error(400, message=message) # Bad Request

# Set up response dictionary.
self.response = dict()

def set_default_headers(self):
self.set_header('Content-Type', 'application/json')

def write_error(self, status_code, **kwargs):
if 'message' not in kwargs:
if status_code == 405:
kwargs['message'] = 'Invalid HTTP method.'
else:
kwargs['message'] = 'Unknown error.'

self.response = kwargs
self.write_json()

def write_json(self):
try:
output = json.dumps(self.response)
except TypeError as e:
raise e

self.write(output)

class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render('home.html')

class QueryHandler(JsonHandler):
def post(self):
query = str(self.request.arguments['q'])
resp = nlquery.query(query, format_='raw')

if not resp:
self.response['data'] = {}

if resp.get('tree'):
resp['tree'] = '<pre>'+resp['tree']+'</pre>'
if resp.get('sparql_query'):
resp['sparql_query'] = '<pre>'+resp['sparql_query']+'</pre>'

self.response['data'] = resp
self.write_json()

def make_app():
return tornado.web.Application([
(r"/", MainHandler),
(r"/query", QueryHandler)],
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
debug=True)

if __name__ == "__main__":
parse_command_line()
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
2 changes: 2 additions & 0 deletions nlquery-app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tornado
..
63 changes: 63 additions & 0 deletions nlquery-app/static/css/home.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* http://bootsnipp.com/snippets/featured/panels-with-nav-tabs*/

#loader {
padding: 5px 14px 6px 14px;
background: white;
border: #ccc solid 1px;
margin-left: -1px;
-webkit-appearance: button;
cursor: pointer
}

.panel {
margin-top: 48px;
}

.panel.with-nav-tabs .panel-heading{
padding: 5px 5px 0 5px;
}
.panel.with-nav-tabs .nav-tabs{
border-bottom: none;
}
.panel.with-nav-tabs .nav-justified{
margin-bottom: -1px;
}

.with-nav-tabs.panel-primary .nav-tabs > li > a,
.with-nav-tabs.panel-primary .nav-tabs > li > a:hover,
.with-nav-tabs.panel-primary .nav-tabs > li > a:focus {
color: #fff;
}
.with-nav-tabs.panel-primary .nav-tabs > .open > a,
.with-nav-tabs.panel-primary .nav-tabs > .open > a:hover,
.with-nav-tabs.panel-primary .nav-tabs > .open > a:focus,
.with-nav-tabs.panel-primary .nav-tabs > li > a:hover,
.with-nav-tabs.panel-primary .nav-tabs > li > a:focus {
color: #fff;
background-color: #3071a9;
border-color: transparent;
}
.with-nav-tabs.panel-primary .nav-tabs > li.active > a,
.with-nav-tabs.panel-primary .nav-tabs > li.active > a:hover,
.with-nav-tabs.panel-primary .nav-tabs > li.active > a:focus {
color: #428bca;
background-color: #fff;
border-color: #428bca;
border-bottom-color: transparent;
}
.with-nav-tabs.panel-primary .nav-tabs > li.dropdown .dropdown-menu {
background-color: #428bca;
border-color: #3071a9;
}
.with-nav-tabs.panel-primary .nav-tabs > li.dropdown .dropdown-menu > li > a {
color: #fff;
}
.with-nav-tabs.panel-primary .nav-tabs > li.dropdown .dropdown-menu > li > a:hover,
.with-nav-tabs.panel-primary .nav-tabs > li.dropdown .dropdown-menu > li > a:focus {
background-color: #3071a9;
}
.with-nav-tabs.panel-primary .nav-tabs > li.dropdown .dropdown-menu > .active > a,
.with-nav-tabs.panel-primary .nav-tabs > li.dropdown .dropdown-menu > .active > a:hover,
.with-nav-tabs.panel-primary .nav-tabs > li.dropdown .dropdown-menu > .active > a:focus {
background-color: #4a9fe9;
}
Binary file added nlquery-app/static/img/ajax_loader.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions nlquery-app/static/js/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
$(document).ready(function(){
var loading = false;
$('#query-form').on('submit', function(e){
e.preventDefault();
if(loading){
return;
}
var query = $('#query').val();
loading = true;
$('#search').hide();
$('#loader').show();
$.post(
'/query',
JSON.stringify({'q': query}),
function(resp){
$('#ans-resp').text(resp.data.plain);
$('#query-resp').text(resp.data.query);
$('#params-resp').text(JSON.stringify(resp.data.params));
$('#tree-resp').html(resp.data.tree);
$('#sparql-resp').html(''+resp.data.sparql_query);

loading = false;
$('#search').show();
$('#loader').hide();
},
'json'
).error(function(){
loading = false;
$('#search').show();
$('#loader').hide();
});
});
});
17 changes: 17 additions & 0 deletions nlquery-app/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>NLQuery</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
{% block header %}{% end %}
</head>
<body>
<div id="body">
<div id="content">{% block body %}{% end %}</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
{% block footer %}{% end %}
</body>
</html>
67 changes: 67 additions & 0 deletions nlquery-app/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{% extends "base.html" %}

{% block header %}
<link rel="stylesheet" href="{{ static_url("css/home.css") }}" type="text/css">
{% end %}

{% block body %}

<div class="jumbotron" style="margin-bottom: 0">
<div class="container">
<h1>NLQuery</h1>
<form class="form-group" id="query-form" autocomplete="off">
<div class="input-group">
<input id="query" type="text" class="input-lg form-control"/>
<span class="input-group-btn">
<input type="submit" id="search" class="btn btn-default btn-lg" type="button" value='Go!'/>
<div id="loader" style="display: none">
<img width="32px" height="32px" src="{{ static_url("img/ajax_loader.gif") }}">
</div>
</span>
</div>
</form>
<div class="panel panel-primary with-nav-tabs panel-default">
<div class="panel-heading">
<ul class="nav nav-tabs">
<li class="active"><a href="#tab1" data-toggle="tab">Result</a></li>
<li><a href="#tab2" data-toggle="tab">Tree</a></li>
<li><a href="#tab3" data-toggle="tab">Sparql</a></li>
</ul>
</div>
<div class="panel-body">
<div class="tab-content">
<div class="tab-pane fade in active" id="tab1">
<label>Question</label>
<div id="query-resp">
</div>
<hr>
<label>Params</label>
<div id="params-resp">
</div>
<hr>
<label>Answer</label>
<div id="ans-resp">
</div>
</div>
<div class="tab-pane fade" id="tab2">
<div id="tree-resp">
</div>
</div>
<div class="tab-pane fade" id="tab3">
<div id="sparql-resp">
</div>
</div>
<div class="tab-pane fade" id="tab4"></div>
<div class="tab-pane fade" id="tab5"></div>
</div>
</div>
</div>
</div>
</div>


{% end %}

{% block footer %}
<script src="/static/js/home.js"></script>
{% end %}
Empty file added nlquery/__init__.py
Empty file.
29 changes: 29 additions & 0 deletions nlquery/answer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from datetime import datetime

class Answer(object):
def __init__(self, query=None, data=None):
self.query = query
self.data = data
self.params = {}
self.tree = None

def to_plain(self):
if isinstance(self.data, list):
return ', '.join([Answer.to_string(d) for d in self.data])
else:
return Answer.to_string(self.data)

def to_dict(self):
return {
'plain': self.to_plain(),
'query': self.query,
'params': self.params,
'tree': self.tree,
}

@staticmethod
def to_string(value):
if isinstance(value, datetime):
return value.strftime('%B %d, %Y')
else:
return unicode(value)
Loading

0 comments on commit 49dc1cc

Please sign in to comment.