-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax_tree.py
77 lines (66 loc) · 2.23 KB
/
syntax_tree.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
import matplotlib
matplotlib.use('Agg')
from nltk.parse.corenlp import CoreNLPParser
from nltk.draw.tree import TreeView
from nltk.draw import TreeWidget
from nltk import Tree
from nltk.draw.util import CanvasFrame
from nltk.parse.corenlp import CoreNLPDependencyParser
import os
import sys
from flask import Flask, request, jsonify
import logging
from flask_cors import CORS
import requests
from matplotlib import pyplot as plt
import time
app = Flask(__name__)
CORS(app)
def draw_syntax_tree(text="Have you practised so long to learn to read?"):
app.logger.info("aca adentro")
parser = CoreNLPParser()
cf = CanvasFrame()
#text = sys.argv[1]
#text = "Have you practised so long to learn to read?"
parse = next(parser.raw_parse(text))
#print(dir(TreeView(parse)._cframe))
tc = TreeWidget(cf.canvas(),parse)
tc['node_font'] = 'helvetica 14 '
tc['leaf_font'] = 'helvetica 14 bold'
tc['node_color'] = '#FFFFFF'
tc['leaf_color'] = '#3F8F57'
tc['line_color'] = '#175252'
cf.add_widget(tc,10,10) # (10,10) offsets
cf.print_to_file('output_test.ps')
cf.destroy('all')
convert_to_png = "gs -dSAFER -dEPSCrop -r600 -sDEVICE=pngalpha -o output_test.svg output_test.ps"
os.system(convert_to_png)
convert_to_svg = "convert output_test.svg output_test.svg"
os.system(convert_to_svg)
app.logger.info("drawn")
def draw_dep_tree():
parser = CoreNLPDependencyParser()
cf = CanvasFrame()
text = sys.argv[1] #"Have you practised so long to learn to read?"
parse = next(parser.raw_parse(text))
tc = TreeWidget(cf.canvas(),parse)
print(tc, dir(tc))
tc['node_font'] = 'arial 14 bold'
tc['leaf_font'] = 'arial 14'
tc['node_color'] = '#005990'
tc['leaf_color'] = '#3F8F57'
tc['line_color'] = '#175252'
cf.add_widget(tc,10,10) # (10,10) offsets
cf.print_to_file('tree3.ps')
cf.destroy()
@app.route('/exec', methods=['GET', 'POST','DELETE', 'PATCH'])
def exec():
app.logger.info(request.json["element"])
draw_syntax_tree(text=request.json["element"])
return ''
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5005,debug=True)
#if __name__ == "__main__":
# text=sys.argv[1]
# print("python",text)
# draw_syntax_tree(text)