/
create_wiki_graph_wiki.py
202 lines (166 loc) · 6.41 KB
/
create_wiki_graph_wiki.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
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 20 17:30:35 2015
@author: mrinmoymaity
"""
import networkx as nx
import os
def path_extraction(file, level):
dir_level_1 = os.path.dirname(file)
dir_level_2 = os.path.dirname(dir_level_1)
dir_level_3 = os.path.dirname(dir_level_2)
if (level == 1):
return dir_level_1
elif level == 2:
return dir_level_2
elif dir_level_3:
return dir_level_3
## Test path_extraction
#def test_path_extraction():
# file = "C:\WikiProject\Controversial Single Pages Simple Wiki\Anonymous Inclusion Without Details\User Graphs With Anonymous"
# result = path_extraction(file,3)
# print file
# print result
#
#test_path_extraction()
def filename_extraction(file):
filename = os.path.basename(file)
parts = filename.split(".")
return parts[0]
## Test filename extractor
#def test_filename_extraction():
# file = "C:\WikiProject\Controversial Single Pages Simple Wiki\Anonymous Inclusion Without Details\User Graphs With Anonymous\\abc.txt"
# result = filename_extraction(file)
# print file
# print result
#test_filename_extraction()
def initialize_undirected_wiki_graph():
G = nx.Graph()
return G
def initialize_directed_wiki_graph():
G = nx.DiGraph()
return G
#Add Nodes/Users if not already present
def update_users(G,user):
if G.has_node(user) != None:
G.add_node(user)
return
#Update weights of the edges or add one if not already present
def update_user_links(G,user1,user2,weight):
if G.has_edge(user1,user2):
G[user1][user2]['weight']+=weight
else:
G.add_edge(user1,user2,weight=weight)
return
#Only add edges at least one of whose end is anonymous
def update_user_links_only_anonymous(G,user1,user2,weight):
if G.has_edge(user1,user2):
G[user1][user2]['weight']+=weight
else:
if is_anonymous(user1) or is_anonymous(user2):
update_users(G, user1)
update_users(G, user2)
G.add_edge(user1,user2,weight=weight)
return
#Update user network
def include_in_network(G,reverter,revertedTo,reverted):
update_users(G,reverter)
update_users(G,reverted)
update_users(G,revertedTo)
update_user_links(G,reverter,reverted,-1)
update_user_links(G,reverter,revertedTo,1)
# update_user_links(G,reverted,revertedTo,-1)
return
#Update user network only anonymous
def include_in_network_only_anonymous(G,reverter,revertedTo,reverted):
update_user_links_only_anonymous(G,reverter,reverted,-1)
update_user_links_only_anonymous(G,reverter,revertedTo,1)
return
def graph_properties(G):
print("No. of triangles:", nx.triangles(G))
print("Clustering: ", nx.clustering(G))
structural_balance(G)
print("Radius:", nx.radius(G))
print("Diameter:", nx.diameter(G))
print("Density:", nx.density(G))
print("Periphery:", nx.periphery(G))
def structural_balance(G):
node_list = []
errors = 0
balanced_triplets = 0
unbalanced_triplets = 0
unbalanced_triplets_all_negatives = 0
unbalanced_triplets_one_negative = 0
for node in G.nodes():
node_list.append(node)
for i in range(len(node_list)):
for j in range(len(node_list)):
for k in range(len(node_list)):
node_A = node_list[i]
node_B = node_list[j]
node_C = node_list[k]
if (i!=j) and (j!=k) and (i!=k):
if G.has_edge(node_A,node_B) and G.has_edge(node_B, node_C) and G.has_edge(node_C, node_A):
edge_AB = (G[node_A][node_B]['weight']>=0)
edge_BC = (G[node_B][node_C]['weight']>=0)
edge_CA = (G[node_C][node_A]['weight']>=0)
if (edge_AB and edge_BC and edge_CA) or (edge_AB + edge_BC + edge_CA == 1):
balanced_triplets += 1
elif (edge_AB + edge_BC + edge_CA == 0):
unbalanced_triplets += 1
unbalanced_triplets_all_negatives += 1
elif (edge_AB + edge_BC + edge_CA == 2):
unbalanced_triplets += 1
unbalanced_triplets_one_negative += 1
else:
errors += 1
print("Total number of Nodes:", len(node_list))
print("Total number of Edges:", G.number_of_edges())
print("Maximum possible Edges:", ((len(node_list))*(len(node_list)-1)*(len(node_list)-2)/6))
print("Edge Density:", (G.number_of_edges()*6.0/((len(node_list))*(len(node_list)-1)*(len(node_list)-2))))
print("balanced_triplets:",balanced_triplets)
print("unbalanced_triplets:",unbalanced_triplets)
print("unbalanced_triplets_all_negatives:",unbalanced_triplets_all_negatives)
print("unbalanced_triplets_one_negative:",unbalanced_triplets_one_negative)
print("errors:",errors)
# Test if the input is a real user_id or anonymous
def is_anonymous(num):
try:
int(num)
return False
except ValueError:
return True
## Test is_anonymous
#def test_is_anonymous():
# result = is_anonymous(45)
# print result
#test_is_anonymous()
# Create a new graph where there will be a edge only between reverter and reverted (thereby omitting revertedTo)
def create_undirected_reverter_reverted_network(inputFile):
op_path = "~/WikiAnalysis/Wikidumps/Output_Logs/reverts_network.gml"
G = initialize_undirected_wiki_graph()
for lines in inputFile:
if lines[0] != '#':
words = lines.split(",")
reverter = words[0].strip()
reverted = words[2].strip()
include_in_network_only_reverter_reverted(G,reverter,reverted)
else:
#New pageID
print(lines)
continue
nx.write_gml(G,op_path)
graph_properties(G)
# Called from create_Directed_reverter_reverted_network()
def include_in_network_only_reverter_reverted(G,reverter,reverted):
update_users(G,reverter)
update_users(G,reverted)
update_user_links(G,reverter,reverted,1)
return
def main():
primary_ip_dir = "~/WikiAnalysis/Wikidumps/Output_Logs/"
inputpath = primary_ip_dir+"reverts_.log"
inputFile = open(inputpath, "rb")
create_undirected_reverter_reverted_network(inputFile)
inputFile.close()
main()