-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
92 lines (83 loc) · 2.74 KB
/
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
import re
import nltk
from nltk.tokenize import sent_tokenize
import spacy
from extraction import findSVOs
import networkx as nx
import matplotlib.pyplot as plt
import time
import os
import en_core_web_lg
nlp = spacy.load('en_core_web_lg')
def create_graph(final_nodes):
source = []
target = []
edge = []
indexes = []
for i in (range(len(final_nodes))):
ent1 = final_nodes[i][0]
ent2 = final_nodes[i][2]
rel = final_nodes[i][1]
source.append(ent1.lower().strip())
target.append(ent2.lower().strip())
edge.append("".join(rel).strip())
indexes.append(i)
if(len(edge) == 0 or len(final_nodes) == 0):
return None
else:
G = nx.DiGraph(directed=True)
for i in (range(len(edge))):
G.add_weighted_edges_from([(source[i], target[i], i)])
size=20
if len(edge)/2 > 20:
size = len(edge)/2
plt.figure(figsize = (size, size))
edge_labels = dict([((u, v, ), edge[d['weight']]) for u, v, d in G.edges(data = True)])
pos = nx.spring_layout(G, k = 0.8)
nx.draw(G, with_labels = True, node_color = 'lightblue', node_size=5000, edge_color='r', edge_cmap = plt.cm.Blues, pos=pos, font_size=20)
nx.draw_networkx_edge_labels(G, pos, edge_labels = edge_labels, font_size = 15)
return G, edge_labels
def load_text(file_name):
with open(file_name, 'r', encoding='utf-8') as file:
text = file.read()
sentences = sent_tokenize(text)
return sentences
# for i in nodes:
# for j in i:
# if(len(j) == 3):
# final_nodes.append(j)
# g = create_graph(final_nodes)
# plt.savefig('graph.png')
# def make_graph_sum(summary):
# nodes = []
# final_nodes = []
# sentences = sent_tokenize(summary)
# for sentence in sentences:
# tokens = nlp(sentence)
# svos = findSVOs(tokens)
# nodes.append(svos)
# for i in nodes:
# for j in i:
# if(len(j) == 3):
# final_nodes.append(j)
# g = create_graph(final_nodes)
# plt.savefig('static/sgraph.png')
def make_graph(text):
nodes = []
final_nodes = []
sentences = sent_tokenize(text)
for sentence in sentences:
tokens = nlp(sentence)
svos = findSVOs(tokens)
nodes.append(svos)
for i in nodes:
for j in i:
if(len(j) == 3):
final_nodes.append(j)
g = create_graph(final_nodes)
new_graph_name = "graph" + str(time.time()) + ".png"
for filename in os.listdir('static/graphs/'):
if filename.startswith('graph_'): # not to remove other images
os.remove('static/graphs/' + filename)
plt.savefig('static/graphs/' + new_graph_name)
return new_graph_name