-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkeygraph.py
More file actions
388 lines (320 loc) · 12.7 KB
/
keygraph.py
File metadata and controls
388 lines (320 loc) · 12.7 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import codecs
import pprint
import time
import math
import networkx as nx
from networkx.algorithms.community.centrality import girvan_newman
from networkx.algorithms.community.quality import modularity
from pyvis.network import Network
from document import Document
# sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
# sys.stdin = codecs.getreader('utf_8')(sys.stdin)
class Util:
@staticmethod
# Pretty-print a Python object
def pp(obj):
pp = pprint.PrettyPrinter(indent=4, width=160)
s = pp.pformat(obj)
return s
@staticmethod
# Read file name from the console
def get_file_name():
if (len(sys.argv) != 2):
print("Usage: #python %s file-name" % sys.argv[0])
sys.exit()
return sys.argv[1]
class KeyGraph:
def __init__(self, document, M=30, K=12):
self.document = document
self.base = self.compute_base(M)
self.G_C = self.compute_hubs(K)
# Compute base of frequently co-occurring words
def compute_base(self, M):
mtime0 = time.time()
print("Compute base: top %d words and edges" % M)
# Sort words by their frequency (in ascending order)
freq_count = self.document.freq_count()
words_freq = sorted(freq_count.items(), key=lambda x: x[1])
# Compute unique words
self.words = [w for w, f in words_freq]
print("Unique words:", len(self.words))
# Calculate word frequency in sentences
self.wfs = self.calculate_wfs()
# Determine high frequency words
# Include all words with the higher or same frequency than the Mth word
wf_min = words_freq[-M][1] if len(words_freq) > M else 0
hf = [w for w, f in words_freq if f >= wf_min]
# Adjust M to the number of high frequency words
M = len(hf)
print("Adjust M to", M)
print("High frequency words:", len(hf))
# Calculate co-occurrence degree of high-frequency words
co = self.calculate_co_occurrence(hf)
# Keep only the tightest links
# Include all links with the higher of same co-occurrence degree as the Mth link
c_min = co[-M][2] if len(co) > M else 0
co = [[i, j] for i, j, c in co if c >= c_min]
print(Util.pp(co))
mtime1 = time.time()
print("Execution time of compute base before find clusters: %.4f seconds" % (mtime1 - mtime0))
# Compute the clusters (which are the basis for islands)
self.find_clusters(co)
mtime2 = time.time()
print("Execution time for find clusters: %4f" % (mtime2 - mtime1))
return co
# Calculate word frequency in sentences
def calculate_wfs(self):
wfs = {}
for w in self.words:
for s_idx, s in enumerate(self.document.sentences):
if w not in wfs:
wfs[w] = {}
wfs[w][s_idx] = s.count(w)
return wfs
# Calculate co-occurrence degree of high-frequency words
def calculate_co_occurrence(self, hf):
co = {}
for hf1 in hf:
co[hf1] = {}
for hf2 in hf[hf.index(hf1)+1:]:
co[hf1][hf2] = 0
for s in self.document.sentences:
# Why sum products, not min, as in Ohsawa (1998)?
# co[hf1][hf2] += s.count(hf1) * s.count(hf2)
co[hf1][hf2] += min(s.count(hf1), s.count(hf2))
co_list = []
for x in co.keys():
for y in co[x].keys():
co_list.append([x, y, co[x][y]])
co_list.sort(key=lambda a: a[2])
return co_list
# Detect communities in the base
# The base is a list of pairs of words that are co-occurring in the document
# Clusters will be used to define islands of connected words, however, the edges
# between the clusters do not be removed to do that
def find_clusters(self, base):
G = nx.Graph()
for i, j in base:
G.add_edge(i, j)
communities = girvan_newman(G)
communities_by_quality = [(c, modularity(G, c)) for c in communities]
c_best = sorted([(c, m) for c, m in communities_by_quality], key=lambda x: x[1], reverse=True)
c_best = c_best[0][0]
# print(Util.pp(communities_by_quality))
print("Clusters:", modularity(G, c_best), c_best)
# only include clusters of more than one node (for now)
# self.clusters = [c for c in c_best if len(c) > 1]
# Include all clusters (do not remove edges between clusters)
self.clusters = c_best
# for cluster in c_best:
# print(G.subgraph(cluster).edges())
self.new_base = [edge for cluster in c_best for edge in G.subgraph(cluster).edges()]
# return new_base
# Links between clusters could be shown in a different color or dotted line
# Compute hubs that connect words in the base
def compute_hubs(self, K):
print("Compute hubs: top %d key terms and bridges" % K)
# Extract nodes in the base
G_base = set([x for pair in self.base for x in pair])
# Remove high frequency words from G_base, leaving non-high frequency words
self.words = [w for w in self.words if w not in G_base]
print("Non-high frequency words:", len(self.words))
# Compute key terms that connect clusters
key = self.key(self.words)
print("Key terms:", Util.pp(key))
# Sort terms in D by keys
# Include all words with the higher or same frequency than the Kth word
high_key = sorted(key.items(), key=lambda x: x[1])
k_min = high_key[-K][1] if len(high_key) > K else 0
high_key = [w for w, k in high_key if k >= k_min]
# Adjust K to the number of high key words
K = len(high_key)
print("Adjusted K:", K)
print(Util.pp(high_key))
# Calculate columns
C = self.columns(high_key, G_base)
print(Util.pp(C))
# Compute the top links between key terms (red nodes) and columns
# Include all links with the higher of same co-occurrence degree as the Kth link
c_min = C[-K][2] if len(C) > K else 0
G_C = [[i, j] for i, j, c in C if c >= c_min]
# Compute adjacency list
self.base_adj = self.adjacency_list(self.base, G_C)
return G_C
# Compute key terms that connect clusters
def key(self, words):
# optimization: compute the neighbors of all clusters ahead of time
neighbors = {}
for g_idx, g in enumerate(self.clusters):
neighbors[g_idx] = self.neighbors(g)
# key is a dictionary of the form key = {w: key value}
key = {}
for w in words:
# print("keyword: {}".format(w))
product = 1.0
for g_idx, g in enumerate(self.clusters):
# print("g", g_)
# print("neighbors", neighbors)
based = self.based(w, g)
# print("based", based)
product *= 1 - based/neighbors[g_idx]
key[w] = 1.0 - product
return key
# Count of words in sentences including words in cluster g
def neighbors(self, g):
neighbors = 0
for s, sentence in enumerate(self.document.sentences):
g_s = 0
for t in g:
g_s += self.wfs[t][s]
# print("g_s", g_s)
for w in sentence:
# print(s, w)
w_s = self.wfs[w][s]
if w in g:
# print("w in g")
neighbors += + w_s * (g_s - w_s)
else:
# print("w not in g")
neighbors += w_s * g_s
return neighbors
# Count how many times w appeared in D based on concept represented by cluster g
def based(self, w, g):
based = 0
for s, sentence in enumerate(self.document.sentences):
# print(s, w)
g_s = 0
for t in g:
g_s += self.wfs[t][s]
# print("g_s", g_s)
w_s = self.wfs[w][s]
if w in g:
# print("w in g")
based += w_s * (g_s - w_s)
else:
# print("w not in g")
based += w_s * g_s
return based
# Calculate columns c(wi,wj)
def columns(self, hk, base):
c = {}
for k in hk:
c[k] = {}
for b in base:
c[k][b] = 0
for s in self.document.sentences:
c[k][b] += min(s.count(k), s.count(b))
n_clusters = self.clusters_touching(c)
print(Util.pp(n_clusters))
c_list = []
for k in c.keys():
for b in c[k].keys():
if n_clusters[k] > 1 and c[k][b] > 0:
c_list.append([k, b, c[k][b]])
c_list.sort(key=lambda a: a[2])
return c_list
# How many clusters does each column touch
def clusters_touching(self, c):
n_clusters = {}
for k in c.keys():
# print("k", k)
n_clusters[k] = 0
for g in self.clusters:
# print("g", g)
in_cluster = 0
for b in c[k].keys():
# print("b", b)
if c[k][b] > 0 and b in g:
# print("b in g")
in_cluster = 1
n_clusters[k] += in_cluster
return n_clusters
# Create an adjacency list
def adjacency_list(self, base, G_C):
a_list = {}
for i, j in base:
if i in a_list:
a_list[i].append([j,'base'])
else:
a_list[i] = [[j,'base']]
if j in a_list:
a_list[j].append([i,'base'])
else:
a_list[j] = [[i,'base']]
for i, j in G_C:
if i in a_list:
a_list[i].append([j,'key'])
else:
a_list[i] = [[j,'key']]
if j in a_list:
a_list[j].append([i,'key'])
else:
a_list[j] = [[i,'key']]
return a_list
def save_adjacency_list(self, fname):
fout = codecs.open("./adjacency_list/" + fname + ".txt", "w", "utf-8")
fout.write(Util.pp(self.base_adj))
fout.close()
def draw(self, fname):
G = nx.Graph()
# Add all nodes in clusters
for cluster in self.clusters:
G.add_nodes_from(cluster, color='black')
# Add edges for nodes in clusters
for i, j in self.base:
if (i, j) in self.new_base:
G.add_edge(i, j)
# Add edges for nodes in key terms
for i, j in self.G_C:
G.add_node(i, color='red')
G.add_edge(i, j, color='red')
# Remove isolated nodes
G.remove_nodes_from(list(nx.isolates(G)))
network = Network('600px', '600px')
network.from_nx(G)
network.show("./graphs/{}.html".format(fname))
# Draw keygraph in dot format
def draw_dot(self, fname):
fout = codecs.open("./dot/" + fname + ".dot","w","utf-8")
fout.write('graph keygraph {\n')
fout.write('graph [size="10,10", overlap="scale"]\n')
g = []
for i, j in self.base:
g.append(i)
g.append(j)
for i in set(g):
fout.write(self.quote(i) + ' [color="black"]\n')
k = []
for i, j in self.G_C:
k.append(i)
for i in set(k):
fout.write(self.quote(i) + ' [color="red"]\n')
for i, j in self.base:
fout.write(self.quote(i) + '--' + self.quote(j) +'\n')
for i, j in self.G_C:
fout.write(self.quote(i) + '--' + self.quote(j) + ' [color="red", style="dotted"]\n')
fout.write('}')
fout.close()
# Add optional quotes around a name
def quote(self, name):
if 1 in [c in name for c in ['-', '/', '.', '\'']] or name in ["graph"]:
return "\"{}\"".format(name)
return name
#-----------Main----------------
if __name__ == "__main__":
stime = time.time()
# Create a document
fname = Util.get_file_name()
doc = Document(file_name = 'txt_files/' + fname + '.txt')
# Create a keygraph
kg = KeyGraph(doc, M=20, K=8) # default: M=30, K=12
print("clusters", kg.clusters)
kg.save_adjacency_list(fname)
mtime = time.time()
kg.draw(fname)
print("Time to draw keygraph: %.4f", (mtime - stime))
etime = time.time()
print("Execution time: %.4f seconds" % (etime - stime))