-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_graph.py
97 lines (85 loc) · 2.9 KB
/
word_graph.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
import matplotlib
matplotlib.use('Agg')
from nltk.corpus import wordnet as wn
from nltk.corpus import knbc, jeita
import networkx as nx
import matplotlib.pyplot as plt
import sys
import os
from googletrans import Translator, constants
from random import random
#
from flask import Flask, request, jsonify
import logging
from flask_cors import CORS
import requests
import time
#
app = Flask(__name__)
CORS(app)
plt.style.use('dark_background')
params = {"ytick.color" : "white",
"xtick.color" : "white",
"axes.labelcolor" : "white",
"axes.edgecolor" : "white",
"text.color":"white"}
plt.rcParams.update(params)
def graph(word):
def closure_graph(synset, fn):
seen = set()
graph = nx.DiGraph()
def recurse(s):
if not s in seen:
seen.add(s)
graph.add_node(s.name())
for s1 in fn(s):
rnd_nm = random()
if rnd_nm > .51:
name=s1.name()
else:
only_name= s1.name().split(".")[0]
only_name = only_name.replace("_", " ")
spanish_name = Translator().translate(only_name, dest="es").text
name = spanish_name.replace(" ","_") + "." + ".".join(s1.name().split(".")[1:])
#name = s1.name()
print(">>> ",name)
graph.add_node(name)
graph.add_edge(s.name(), name)
recurse(s1)
res_amount = recurse(synset)
return graph,len(seen)
#word = sys.argv[1]
dog = wn.synsets(word)[0]
G_hypo, length_hypo = closure_graph(dog,
lambda s: s.hyponyms())
G_hyper, length_hyper = closure_graph(dog,
lambda s: s.hypernyms())
res = [{"name":"hypo", "obj":G_hypo, "len":length_hypo}, {"name":"hyper", "obj":G_hyper, "len":length_hyper }]
print("hiponimos> ", length_hypo)
print("hiperónimos> ", length_hyper)
G = max(res, key=lambda x:x["len"])["obj"]
index = nx.betweenness_centrality(G)
plt.rc('figure', figsize=(20, 10))
node_size = [index[n]*5000 for n in G]
pos = nx.spring_layout(G)
nx.draw_networkx(G, pos, node_size=node_size, edge_color='r', alpha=.6, linewidths=.2, font_color="whitesmoke")
plt.show()
#fig, ax = plt.subplots()
plt.box(False)
plt.savefig('output_word.png',bbox_inches='tight', transparent=True)
plt.close()
convert_to_png = "convert output_word.png output_word.svg"
os.system(convert_to_png)
print("dibujado")
return
@app.route('/exec', methods=['GET', 'POST','DELETE', 'PATCH'])
def exec():
words = request.json["element"]
app.logger.info(words)
try:
graph(words)
except:
pass
return ''
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000,debug=True)