-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTNA.py
274 lines (245 loc) · 10.9 KB
/
TNA.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
import pandas
from pylab import *
import networkx as nx
import pandas as pd
from operator import itemgetter
def timeit(func):
"""
Decorator for measuring function's running time.
"""
def measure_time(*args, **kw):
start_time = time.time()
result = func(*args, **kw)
print("Processing time of %s(): %.2f seconds."
% (func.__qualname__, time.time() - start_time))
return result
return measure_time
@timeit
def TNAfunc(path, names):
# Open input csv file, and create output files
dataset = pd.read_csv('frequencyTest.csv', delimiter=',')
appNamesFile = pd.read_csv('apps_names.csv', delimiter=',')
topologyStatisticsFile = open("topologyStatistics.txt", 'w', encoding='utf8')
graphDictionaryFile = open("graphDictionary.txt", 'w', encoding='utf8')
appList = []
appRawList = []
listWithAllAppsHash = []
print("getting apps...")
for i in range(dataset['App'].count()):
splitVal = dataset['App'][i].split(';')
for j in range(len(splitVal)):
appList.append(splitVal[j].strip())
for i in range(appNamesFile['App'].count()+1):
appRawList.append(appNamesFile['App'][i])
# Remove redundant entries, and put into a dictionary with a base comment score of 0
appList = set(appList)
# check if name of app is valid
for e in appList:
if e in appRawList:
listWithAllAppsHash.append(e)
listWithAllAppsHash = list(listWithAllAppsHash)
listWithAllAppsHash = {i: 0 for i in listWithAllAppsHash}
authListHash = {}
# Create dictionary for author values. Including name, apps commented on, and number of apps commented on
print("getting comments...")
count=0
for i in range(dataset['Author'].count()):
aVal1 = dataset['Author'][i]
aVal2 = dataset['App'][i]
aVal3 = dataset['App ID'][i]
aVal4 = dataset['Total'][i]
parts = aVal2.split(";")
listAux = []
for p in parts:
listAux.append(p)
listAux = set(listAux)
if len(parts) != len(listAux):
count+=1
else:
authListHash[aVal1] = aVal2, aVal3, aVal4
print("Repeated names of authors: "+str(count))
print("getting sentiment...")
authListHash2 = {}
# Here we add the sentiment besides the app name with the separator "!?$"
for index, item in enumerate(names):
print("processing index (out of 500): "+str(index))
try:
filePath = pandas.read_csv(str(path+"/"+item))
except:
print("couldn't read file")
appSingleName = item[31:len(item)-4]
for i in range(len(filePath)):
if authListHash.get(filePath['Author'][i]) is not None:
try:
splitId = authListHash2[filePath['Author'][i]][1].split(";")
splitApp = authListHash2[filePath['Author'][i]][0]
idValue = authListHash2[filePath['Author'][i]][1]
idFrequency = authListHash2[filePath['Author'][i]][2]
for j in range(len(splitId)):
if splitId[j] == appSingleName:
sentiment = 'k'
if filePath['sentiment'][i] == 'negative':
sentiment = 'r'
elif filePath['sentiment'][i] == 'positive':
sentiment = 'b'
splitApp[j] = splitApp[j] + "!?$" + sentiment
authListHash2[filePath['Author'][i]] = splitApp[0:len(splitApp)], idValue, idFrequency
except:
splitId = authListHash[filePath['Author'][i]][1].split(";")
splitApp = authListHash[filePath['Author'][i]][0].split(";")
idValue = authListHash[filePath['Author'][i]][1]
idFrequency = authListHash[filePath['Author'][i]][2]
for j in range(len(splitId)):
if splitId[j] == appSingleName:
sentiment = 'k'
if filePath['sentiment'][i]=='negative':
sentiment = 'r'
elif filePath['sentiment'][i]=='positive':
sentiment = 'b'
splitApp[j] = splitApp[j]+"!?$"+sentiment+str(filePath['compound'][i])
authListHash2[filePath['Author'][i]] = splitApp[0:len(splitApp)], idValue, idFrequency
print("number of authors: "+str(len(authListHash2)))
# Create and name graph
G = nx.Graph()
G.name = "Topological Network Analysis"
G_APP = nx.Graph()
G_APP.name = "Topological Network Analysis APP"
# Add author list and apps list
G.add_nodes_from(listWithAllAppsHash.keys(), type="app")
G_APP.add_nodes_from(listWithAllAppsHash.keys(), type="app")
G.add_nodes_from(authListHash2.keys(), type="author")
# Add edge between every author and every app they commented on
print("building network (9 min)...")
for key, value in authListHash2.items():
for k, v in listWithAllAppsHash.items():
if str(k) in str(value[0]):
color = 'k'
compound = '0'
for n in range(len(value[0])):
if '!?$' in value[0][n] and str(k) in value[0][n]:
try:
partsWeigh = value[0][n].split("!?$")
color = partsWeigh[1][0:1]
compound = partsWeigh[1][1:len(partsWeigh[1])]
if compound == '':
color = 'k'
compound = '0'
except:
color = 'k'
G.add_edge(key, k, color=color, weight=int(float(compound)*10000))
listWithAllAppsHash[k] += 1
# graph with apps only
# for each author
for key_author, value in authListHash2.items():
for app1, valueApp2 in G[key_author].items():
for app2, valueApp2 in G[key_author].items():
factorW = 0
factorC = 1
weight1 = G[key_author][app1]["weight"]
weight2 = G[key_author][app2]["weight"]
if G_APP.has_edge(app1, app2):
factorW = G_APP[app1][app2]["weight"]
factorC = G_APP[app1][app2]["counter"]
averageTemp = (weight1 + weight2) / 2
average = (factorW*factorC + averageTemp)/(factorC+1)
G_APP[app1][app2]["weight"] = int(average)
G_APP[app1][app2]["counter"] = 1+factorC
else:
average = int((weight1+weight2)/2)
G_APP.add_edge(app1, app2, weight=average, counter=1)
print("Extracting degree...")
degreeOfApp = {}
degreeOfAuthors = {}
degree_dict = dict(G.degree(G.nodes()))
for k, v in degree_dict.items():
if k in listWithAllAppsHash:
degreeOfApp[k] = v
else:
if v > 1:
degreeOfAuthors[k] = v
else:
print("removing node: "+str(k)+" value: "+str(v))
G.remove_node(k)
authListHash2.pop(k)
degreeOfApp = sorted(degreeOfApp.items(), key=lambda x: x[1], reverse=True)
degreeOfAuthors = sorted(degreeOfAuthors.items(), key=lambda x: x[1], reverse=True)
# Print statistical information
print(nx.info(G))
topologyStatisticsFile.write(str(nx.info(G)) + "\n")
density = nx.density(G)
print("Network density:", density)
topologyStatisticsFile.write("Network density: " + str(density))
degree_dict = dict(G.degree(G.nodes()))
# remove invalid authors with zero reviews
appHighestDegree = str(list(degreeOfApp[0])[0]) + " - " + str(list(degreeOfApp[0])[1])
authorHighestDegree = str(list(degreeOfAuthors[0])[0]) + " - " + str(list(degreeOfAuthors[0])[1])
topologyStatisticsFile.write(appHighestDegree)
topologyStatisticsFile.write(authorHighestDegree)
print("average degree...")
count = 0
for v in degreeOfApp:
count += v[1]
topologyStatisticsFile.write("\nNode degree av. APP: "+str(count/len(degreeOfApp)))
count = 0
for v in degreeOfAuthors:
count += v[1]
topologyStatisticsFile.write("\nNode degree av. Author: "+str(count/len(degreeOfAuthors)))
topologyStatisticsFile.write("\nNumber of apps:"+str(len(listWithAllAppsHash)))
topologyStatisticsFile.write("\nNumber of Authors:"+str(len(authListHash2)))
topologyStatisticsFile.write("\nTop 10 Authors:\n")
for v in range(0, 10):
topologyStatisticsFile.write(str(degreeOfAuthors[v])+"\n")
print("\n"+str(degreeOfAuthors[v]))
print(str(authListHash2[str(degreeOfAuthors[v][0])])+"\n")
nx.set_node_attributes(G, degree_dict, 'degree')
sorted_degree = sorted(degree_dict.items(), key=itemgetter(1), reverse=True)
print("Top 5 nodes by degree:")
topologyStatisticsFile.write("\nTop 10 Apps:\n")
for d in sorted_degree[:10]:
print(d)
topologyStatisticsFile.write(str(d)+"\n")
# Determine node size
authWeight = []
appWeight = [float(l)*50 for x,l in listWithAllAppsHash.items()]
for i in authListHash2.items():
authWeight.append(float(i[1][2])*50)
print("Weights assigned")
colors = nx.get_edge_attributes(G, 'color').values()
print("Graph completed!")
print("Saving graph...")
#save graph in disk
nx.write_gpickle(G, "G_COMPLETE.gpickle")
nx.write_gpickle(G_APP, "G_APP.gpickle")
#read graph from disk
G_from_dick = nx.read_gpickle("G_APP.gpickle")
print(G_from_dick)
# print("Map to layout (takes time)...")
# # Map to layout
# pos = nx.spring_layout(G, k=0.25, iterations=70, scale=10)
# print("size of graph: "+str(G.size()))
# plt.figure(figsize=(20, 20))
#
# print("Draw network..")
# # Draw network
# nx.draw_networkx_nodes(G, pos=pos, node_color="red", nodelist=listWithAllAppsHash.keys(), node_size=appWeight)
# nx.draw_networkx_nodes(G, pos=pos, node_color="blue", nodelist=authListHash2.keys(), node_size=authWeight)
# nx.draw_networkx_edges(G, pos=pos,edge_color=colors)
#
# print("Add legend")
# # Add legend
# p = mlines.Line2D([],[],label="Author",color="blue")
# a = mlines.Line2D([],[],label="Application",color="red")
# plt.legend(handles=[p,a])
#
# try:
# print("Print graph")
# # Print graph list to output, and output image
# graphDictionaryFile.write(str(G.edges))
# plt.savefig("TopologicalAnalysis.jpg", dpi=200, pil_kwargs={'quality': 50})
# except:
# print("Reached exception when printing JPG")
# Return original graph
return G
# Return graph for the APPs with compound score as 'weights'
# and n of reviews as 'counter'
#return G_APP