-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
110 lines (84 loc) · 3.44 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
import json
import requests
from tornado.ioloop import IOLoop
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from flask import Flask, jsonify, make_response
from flask.ext.restful import Api, Resource, reqparse
app = Flask(__name__)
api = Api(app)
cwd = os.path.abspath(os.path.dirname(__file__))
@app.errorhandler(400)
def bad_request(error):
return make_response(jsonify({'error': 'Bad request'}), 400)
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
def format_parsed_str(parsed_str):
if parsed_str.strip().startswith("(ROOT") and parsed_str.strip().endswith(")"):
parsed_str = parsed_str.strip()[5:-1].strip()
elif parsed_str.strip()[1:].strip().startswith("("):
parsed_str = parsed_str.strip()[1:-1]
parsed = parsed_str.split('\n')
parsed = [line.strip() + ' ' for line in [line1.strip() for line1 in
parsed if line1] if line]
parsed = [line.replace(')', ' ) ').upper() for line in parsed]
treestr = ''.join(parsed)
return treestr
class ExtractAPI(Resource):
def __init__(self):
self.reqparse = reqparse.RequestParser()
self.reqparse.add_argument('text', type=unicode, location='json')
self.reqparse.add_argument('id', type=unicode, location='json')
self.reqparse.add_argument('date', type=unicode, location='json')
super(ExtractAPI, self).__init__()
def get(self):
args = self.reqparse.parse_args()
text = args['text']
text = text.encode('utf-8')
storyid = args['id']
date = args['date']
out = send_to_ccnlp(text)
event_dict = process_corenlp(out, date, storyid)
event_updated = send_to_petr(event_dict)
return event_updated
def send_to_ccnlp(text):
headers = {'Content-Type': 'application/json'}
core_data = json.dumps({'text': text})
ccnlp_url = 'http://ccnlp:5000/process'
r = requests.post(ccnlp_url, data=core_data, headers=headers)
out = r.json()
return out
def send_to_petr(event_dict):
headers = {'Content-Type': 'application/json'}
events_data = json.dumps({'events': event_dict})
petr_url = 'http://petrarch:5001/petrarch/code'
events_r = requests.post(petr_url, data=events_data, headers=headers)
event_updated = events_r.json()
return event_updated
def process_corenlp(output, date, STORYID):
event_dict = {STORYID: {}}
event_dict[STORYID]['sents'] = {}
event_dict[STORYID]['meta'] = {}
event_dict[STORYID]['meta']['date'] = date
for i, _ in enumerate(output['sentences']):
sent = output['sentences'][i]
event_dict[STORYID]['sents'][str(i)] = {}
event_dict[STORYID]['sents'][str(i)]['content'] = ' '.join(sent['tokens'])
event_dict[STORYID]['sents'][str(i)]['parsed'] = format_parsed_str(sent['parse'])
return event_dict
def process_results(event_dict):
for s_id in event_dict:
sents = event_dict[s_id]['sents']
for sent in sents:
if 'issues' not in sents[sent].keys():
sents[sent]['issues'] = []
if 'events' not in sents[sent].keys():
sents[sent]['events'] = []
return event_dict
api.add_resource(ExtractAPI, '/hypnos/extract')
if __name__ == '__main__':
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(5002)
IOLoop.instance().start()