-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAnalyses.py
373 lines (295 loc) · 12.9 KB
/
Analyses.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
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
import networkx as nx
import copy
import nltk
import subprocess
import sys
def single_cluster_modularityOV(graph, Clusters, f, nCluster):#, resultPosition):
E_in = 0
E_out = 0
E = 0
for e in graph.edges():
E += graph[e[0]][e[1]]['weight']
for v in Clusters[nCluster]:
for e in graph[v]:
for c in Clusters:
if e in c:
if c == Clusters[nCluster]:
E_in += 1/f[v] * 1/f[e] * graph[v][e]['weight'] / 2
else:
E_out += 1/f[v] * 1/f[e] * graph[v][e]['weight']
#thread_result[resultPosition] = (E_in / E) - ((((2*E_in) + E_out)/(2*E))**2)
return (E_in / E) - ((((2*E_in) + E_out)/(2*E))**2)
def calc_f(graph, Clusters):
f = {}
for i in graph.nodes():
count = 0
for c in Clusters:
if i in c:
count += 1
if count < 1:
count = 1
f[i] = count
return f
def merge_Clusters(Clusters, i, j, f):
for n in Clusters[j]:
if n not in Clusters[i]:
Clusters[i].append(n)
else:
if f[n] > 1:
f[n] -= 1
Clusters.remove(Clusters[j])
return Clusters, f
###################------------ MERGE SMALL CLUSTERS INTO LARGER ONES -----------------######################3
def reduceClusters(g, Clusters, nFinalClusters):
f = calc_f(g, Clusters)
for j in range(len(Clusters)-1, nFinalClusters-1, -1):
better_i_value = 1000
better_i = -1
#print('Merging cluster', j)
index_max = nFinalClusters
if j < nFinalClusters:
index_max = j-1
for c,i in zip(Clusters[:index_max], range(index_max)):
mod_i = single_cluster_modularityOV(g, Clusters, f, i)
mod_j = single_cluster_modularityOV(g, Clusters, f, j)
mod_iUj = single_cluster_modularityOV(g, *merge_Clusters(copy.deepcopy(Clusters), i, j, copy.deepcopy(f)), i)
if mod_i + mod_j - mod_iUj < better_i_value:
better_i = i
better_i_value = mod_i + mod_j - mod_iUj
#print('Analyzing option', i)
#print(better_i_value)
merge_Clusters(Clusters, better_i, j, f)
return Clusters
def clusterRelationGraph(g, Clusters):
f = calc_f(g, Clusters)
connections = []
#init connections
for c in Clusters:
connection = []
for c2 in Clusters:
connection.append(0)
connections.append(connection)
#calc connections
for e in g.edges():
for c in range(len(Clusters)):
if e[0] in Clusters[c]:
for c2 in range(len(Clusters)):
if e[1] in Clusters[c2]:
connections[c][c2] += (1/f[e[0]]) * (1/f[e[1]]) * g[e[0]][e[1]]['weight']
print('\n\n', c, c2, connections[c][c2], f[e[0]], f[e[1]], g[e[0]][e[1]]['weight'])
#for c in range(len(Clusters)):
# for c2 in range(len(Clusters)):
cluster_relation_graph = nx.Graph()
id_Cluster = 0
for n in connections:
cluster_relation_graph.add_node(id_Cluster, peso=len(Clusters[id_Cluster]))
id_Cluster += 1
for c1 in range(len(connections)):
for c2 in range(len(connections)):
if cluster_relation_graph.has_edge(c1,c2):
cluster_relation_graph[c1][c2]['weight'] += connections[c1][c2]
else:
cluster_relation_graph.add_edge(c1,c2, weight=connections[c1][c2])
maxWeight = 0
minWeight = sys.maxsize
for e in cluster_relation_graph.edges():
if cluster_relation_graph[e[0]][e[1]]['weight'] > maxWeight:
maxWeight = cluster_relation_graph[e[0]][e[1]]['weight']
if cluster_relation_graph[e[0]][e[1]]['weight'] < minWeight:
minWeight = cluster_relation_graph[e[0]][e[1]]['weight']
return cluster_relation_graph, maxWeight, minWeight
################################################################### KEYPHRASE EXTRACTION ########################################################
def nodeRank(g):
r = nx.degree_centrality(g)
rank = {}
for p in r:
rank[p] = r[str(p)]
rank = sorted(rank.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)
return rank
def takeSecond(elem):
return elem[1]
def keyPhrasesCompilation(keyWords, g, g2, dictionaryCode,lenght,totalWords):
keyphrases_dict = {code : value for code, value in keyWords}
coumpound_keyphrases = []
#identify compound keyphrases
for k in keyWords[:lenght]:
for k2 in keyWords[:lenght]:
if g2.has_edge(k[0], k2[0]) and g2[k[0]][k2[0]]['weight'] >= int(totalWords / 1000) + 2: #verify occurrences
if g2.has_edge(k2[0], k[0]) == False or g2[k2[0]][k[0]]['weight'] < g2[k[0]][k2[0]]['weight']:
weight = g.out_degree(k[0], weight='weight') + g.in_degree(k2[0], weight='weight') #normalization factor | w(Out(i)) + w(In(j))
phrase = [k[0] + ',' + k2[0], g[k[0]][k2[0]]['weight'] / weight] #weight compound keyphrase | NIE_{i,j}
if phrase not in coumpound_keyphrases:
coumpound_keyphrases.append(phrase)
coumpound_keyphrases = sorted(coumpound_keyphrases, key=lambda kv: (kv[1], kv[0]), reverse=True)
keyphrases_weight = [t[1] for t in coumpound_keyphrases]
keyphrases_weight_norm = [float(i)/sum(keyphrases_weight) for i in keyphrases_weight] #normalize NIE | NIE_{i,j} / (sum_all(NIE))
keyphrases = [t for t in coumpound_keyphrases]
for kp, n in zip(keyphrases, keyphrases_weight_norm):
codes = kp[0].split(',')
#rank keyphrases
kp[1] = ((keyphrases_dict[codes[0]] + keyphrases_dict[codes[1]])) * n # CC_{i,j}
kp[0] = dictionaryCode[codes[0]] + ' ' + dictionaryCode[codes[1]]
soma = sum([v[1] for v in keyphrases])
keyphrases = [[k[0], k[1]/soma] for k in keyphrases] #NCC_{i,j}
keywords = [[dictionaryCode[k[0]], k[1]] for k in keyWords]
merged = keyphrases[:6] + keywords #FWC U NCC_{1:6}
merged.sort(key=takeSecond, reverse=True)
return merged
def extract_keyphrases(g, dictionaryCode):
g = nx.Graph(g)
phrases = []
words = []
#First Rank
keyphrases = nodeRank(g)
#Exclude words different from nouns, verbs and adjectives
new_keyphrases = []
for k in keyphrases:
words = dictionaryCode[k[0]]
tokens = nltk.word_tokenize(words)
notDesiredTags = False
for w in nltk.pos_tag(tokens):
if w[1][0] != 'N' and w[1][0] != 'J' and w[1][0] != 'V':
notDesiredTags = True
if notDesiredTags:
bla = [k[0], 0]
new_keyphrases.append(bla)
else:
new_keyphrases.append(k)
keyphrases = new_keyphrases
keywords = sorted(keyphrases, key=lambda kv: (kv[1], kv[0]), reverse=True)
#excludes last 87% keyphrases
lenght = int(.13*len(keywords))
summation = sum([v[1] for v in keywords[:lenght]])
keywords = [[k[0], k[1]/summation] for k in keywords[:lenght]]
#re-weight mult-term keyphrases
keywords = [[k[0], (k[1]**(1/(len(dictionaryCode[k[0]].split(' ')))))] for k in keywords[:lenght]]
keywords = sorted(keywords, key=lambda kv: (kv[1], kv[0]), reverse=True)
nodesToRemove = []
for n in g:
inKeywords = False
for k in keywords:
if k[0] == n:
inKeywords = True
if inKeywords == False:
nodesToRemove.append(n)
for n in nodesToRemove:
g.remove_node(n)
totalWords = 0
for n in g.nodes():
totalWords += g.nodes()[n]['peso']
keyphrases = keyPhrasesCompilation(keywords,g,g,dictionaryCode,lenght,totalWords)
return keyphrases
########################### COMPARE COVERS ############################################
def parseAnswer(answer):
parsedAnswer = []
for line in answer:
line = line.split('\t')
if len(line) >= 2:
parsedAnswer.append(line)
for l in parsedAnswer:
l[0] = l[0].replace(':', '').replace(' ', '')
return parsedAnswer
def compareFullCovers(cover1, cover2, folderNMI, NMI_type='NMI<Max>'):
command = folderNMI + ' ' + cover1 + ' ' + cover2
p = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
answer = p.stdout.decode('ascii').split('\n')
parsedAnswer = parseAnswer(answer)
if NMI_type == 'NMI<Max>':
return parsedAnswer[0][1]
elif NMI_type == 'lfkNMI':
return parsedAnswer[1][1]
elif NMI_type == 'NMI<Sum>':
return parsedAnswer[2][1]
else:
print('Wrong NMI_type!\n')
return parsedAnswer
def calcDegreeCentrality(g):
grau = {}
for v in g:
grau[v] = len(g[v])
#sorted_grau = sorted(grau.items(), key=operator.itemgetter(1), reverse=True)
return grau
def calcNodesCentrality(g1,g2):
grau = {}
for v in g1:
grau[v] = len(g1[v])
for v2 in g2:
if v2 in grau:
grau[v2] = (len(g1[v2]) + len(g2[v2])) / 2
else:
grau[v2] = len(g2[v2])
return grau
def clusterCentrality(cluster, g, nodeCentrality):
total = 0
for n in cluster:
total += nodeCentrality[n]
return total
def calcClustersCentralities(cover, g, nodeCentrality):
centralities = {}
for c,i in zip(cover, range(len(cover))):
centralities[i] = clusterCentrality(c, g, nodeCentrality)
return centralities
def comunitySimilarity(c1,c2, n1,n2,nodeCentrality, clustersCentralities1, clustersCentralities2):
similarity = 0
for n in c1:
if n in c2:
#similarity += 1
similarity += nodeCentrality[n]
#return similarity / max(len(c1), len(c2))
return [similarity / max(clustersCentralities1[n1], clustersCentralities2[n2]), similarity / min(clustersCentralities1[n1], clustersCentralities2[n2])]
def bestComunitySimilarity(comunity, cover1, nodeCentrality, clustersCentralities1, clustersCentralities2):
higherSimilarity = -1
nCluster = -1
for c, i in zip(cover1, range(len(cover1))):
similarity = comunitySimilarity(c, comunity, nodeCentrality, clustersCentralities1, clustersCentralities2)
if similarity > higherSimilarity:
higherSimilarity = similarity
nCluster = i
return higherSimilarity, nCluster
def coverSimilarities(cover1, cover2, nodeCentrality, clustersCentralities1, clustersCentralities2, sizeThreshold=10):
all_similarities = []
for c1, n1 in zip(cover1, range(len(cover1))):
if len(c1) >= sizeThreshold:
local_similarities = []
for c2, n2 in zip(cover2, range(len(cover2))):
if len(c2) >= sizeThreshold:
local_similarities.append(comunitySimilarity(c1,c2,n1,n2, nodeCentrality, clustersCentralities1, clustersCentralities2))
else:
local_similarities.append([0,0])
all_similarities.append(local_similarities)
else:
local_similarities = []
for c2 in cover2:
local_similarities.append([0,0])
all_similarities.append(local_similarities)
return all_similarities
def compareCovers(all_similarities, threshold):
similar_clusters = []
for c1 in range(len(all_similarities)):
for c2 in range(len(all_similarities[c1])):
if all_similarities[c1][c2][0] >= threshold:
#if [c2,c1,all_similarities[c1][c2]] not in similar_clusters:
similar_clusters.append([c1,c2,all_similarities[c1][c2][0]])
return similar_clusters
############################## EVOLUTION ################################################
def evolution(c1, c2, sKGraph1, sKGraph2):
new_graph = nx.Graph()
for n in c1:
if n in c2:
new_graph.add_node(n, peso=sKGraph1.sciKGraph.nodes()[n]['peso']+sKGraph2.sciKGraph.nodes()[n]['peso'], clusters=3, dicionario=sKGraph1.dictionaryCodeMerged[n])
else:
new_graph.add_node(n, peso=sKGraph1.sciKGraph.nodes()[n]['peso'], clusters=1, dicionario=sKGraph1.dictionaryCodeMerged[n])
for n in c2:
if n not in c1:
new_graph.add_node(n, peso=sKGraph2.sciKGraph.nodes()[n]['peso'], clusters=2, dicionario=sKGraph2.dictionaryCodeMerged[n])
for e in sKGraph1.sciKGraph.edges():
if new_graph.has_node(e[0]) and new_graph.has_node(e[1]):
if e in sKGraph2.sciKGraph.edges():
new_graph.add_edge(e[0], e[1], weight= sKGraph1.sciKGraph[e[0]][e[1]]['weight'] + sKGraph2.sciKGraph[e[0]][e[1]]['weight'])
else:
new_graph.add_edge(e[0], e[1], weight= sKGraph1.sciKGraph[e[0]][e[1]]['weight'])
for e in sKGraph2.sciKGraph.edges():
if new_graph.has_node(e[0]) and new_graph.has_node(e[1]):
if e not in sKGraph1.sciKGraph.edges():
new_graph.add_edge(e[0], e[1], weight= sKGraph2.sciKGraph[e[0]][e[1]]['weight'])
return new_graph