Skip to content

Commit

Permalink
fixed petrarch 2 output format, added default petrarch 2 dictionaries…
Browse files Browse the repository at this point in the history
…, moved petrarch 2 parse format fixer into hypnos app.py
  • Loading branch information
benradford committed Jul 9, 2019
1 parent 9ca2bd3 commit caf97c5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
22 changes: 16 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ def bad_request(error):
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):
Expand All @@ -39,9 +50,8 @@ def get(self):
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
Expand All @@ -62,7 +72,7 @@ def send_to_petr(event_dict):
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 = process_results(events_r.json())
event_updated = events_r.json()

return event_updated

Expand All @@ -73,10 +83,10 @@ def process_corenlp(output, date, STORYID):
event_dict[STORYID]['meta'] = {}
event_dict[STORYID]['meta']['date'] = date
for i, _ in enumerate(output['sentences']):
sents = output['sentences']
sent = output['sentences'][i]
event_dict[STORYID]['sents'][str(i)] = {}
event_dict[STORYID]['sents'][str(i)]['content'] = ' '.join(sents[i]['tokens'])
event_dict[STORYID]['sents'][str(i)]['parsed'] = sents[i]['parse'].upper().replace(')', ' )')
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

Expand Down
30 changes: 25 additions & 5 deletions petrarch/petrarch_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

cwd = os.path.abspath(os.path.dirname(__file__))

config = petrarch2.utilities._get_data('data/config/','PETR_config.ini')
petrarch2.PETRreader.parse_Config(config)
petrarch2.read_dictionaries()
#print(config)
#print(getattr(petrarch2,"VerbDict"))

@app.errorhandler(400)
def bad_request(error):
Expand Down Expand Up @@ -39,18 +44,33 @@ def post(self):
except Exception as e:
sys.stderr.write("An error occurred with PETR. {}\n".format(e))
event_dict_updated = event_dict

for key in event_dict_updated:
event_dict_updated[key]['meta']['verbs']=[]
for sent in event_dict_updated[key]['sents']:
try:
temp_meta = event_dict_updated[key]['sents'][sent]['meta']
event_dict_updated[key]['sents'][sent]['meta']={'actortext':list(temp_meta['actortext'].values()),
'eventtext':list(temp_meta['eventtext'].values()),
'nouns':temp_meta['nouns'],
'actorroot':list(temp_meta['actorroot'].values())}
except:
event_dict_updated[key]['sents'][sent]['meta']={'actortext':[[]],
'eventtext':[[]],
'nouns':[],
'actorroot':[[]]}

return event_dict_updated


api.add_resource(CodeAPI, '/petrarch/code')

if __name__ == '__main__':
config = petrarch2.utilities._get_data('data/config/', 'PETR_config.ini')
print("reading config")
petrarch2.PETRreader.parse_Config(config)
print("reading dicts")
petrarch2.read_dictionaries()
#config = petrarch2.utilities._get_data('data/config/', 'PETR_config.ini')
#print("reading config")
#petrarch2.PETRreader.parse_Config(config)
#print("reading dicts")
#petrarch2.read_dictionaries()

http_server = HTTPServer(WSGIContainer(app))
http_server.listen(5001)
Expand Down

0 comments on commit caf97c5

Please sign in to comment.