-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
326 lines (269 loc) · 12.3 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
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
from cdlib import algorithms, viz
import networkx as nx
from gesetz import Gesetz
from networkx.algorithms.hierarchy import flow_hierarchy
from networkx.algorithms.components import strongly_connected_components, weakly_connected_components
from networkx.algorithms.dag import lexicographical_topological_sort
import datetime
import json
import heapq
import unicodedata
class Graph:
bigDiMul = None
pagerank = None
louvain = None
topic = None
hand_picked = {
"Erfurt":{
"relevant":["ÄArbVtrG", "ArbGG","ArbZG","AÜG","BetrVG","BUrlG","DrittelbG","EntgTranspG","FPfZG","KSchG","MiLoG","MitbestG","MontanMitbestG","NachwG","PflegeZG","SprAuG","TVG","EFZG","WZVG"],
"auszuege":["GG","AEntG","AGG","AktG","ArbPlSchG","ArbSchG","BBiG","BDSG","BEEG","BetrAVG","BGB","GenDG","GewO","HAG","HGB","InsO","JArbSchG","MuSchG","TzBfG","UmwG","ATG"]
},
"Beck":{
"relevant":["AEntG", "AGG","ArbZG","AÜG","BUrlG","EntgTranspG","FPfZG","KSchG","MiLoG","PflegeZG","TVG","TzBfG","ATG","EFZG"],
"auszuege":["ArbGG","BBiG","BEEG","BetrAVG","BetrVG","BGB","BGBEG","GewO","GG","HGB","InsO","MuSchG","VO","MiArbG"]
},
"Umwelt":{
"relevant":["BGBEG", "KrWG","BImSchG","BBodSchG","UBAG","USchadG","UmwRG","WHG","BNatSchG","TEHG","UVPG","OstSUmwSchÜbkG","LMeerSchÜbkG"]
},
"StrafR nomos":{
"relevant":["StGB", "EGStGB","SubvG","UWG","UrhG","VStGB","WpHG","StVG","AufenthG","AsylG","BtMG","WStG","WaffG","JuSchG","VersammlG","G 10","OWiG","NetzDG","GVG","EGGVG","StPO","StPOEG","JGG","IStGHG","IRG","StVollzG","BZRG","StrEG","OEG","JVEG","RVG","GG"]
},
"UmwR beck":{
"relevant":["UBAG", "DBUStiftG","UStatG","UVPG","UIG","UAG","USchadG","BfNatSchG","BNatSchG","TierSchG","BBodSchG","WHG","AbwAG","WRMG","KrWG","ElektroG","BattG","VerpackG","BImSchG","BzBlG","TEHG","SchlärmschG","KSG","BEHG","G BAStrlSchG","BfkEG","AtG","EnEG","StromStG","EVPG","EEWärmeG","EmoG","CsgG","ChemG","GenTG","UmweltHG","UmwRG"]
},
"GesR beck":{
"relevant":[u"AktG", u"AktGEG",u"GmbHG",u"EGGmbHG",u"GenG",u"HGBEG",u"PublG",u"PartGG",u"SEAG",u"EWIVAG",u"WpHG",u"WpÜG",u"UmwG",u"MitbestG",u"MontanMitbestG",u"MontanMitbestGErgG",u"DrittelbG",u"MgVG",u"SEBG",u"SpruchG",u"COVInsAG",u"GesRuaCOVBekG"]
},
"WaffR Beck":{
"relevant":["WaffG", "NWRG","BeschG","SprengG"]
},
"BankR beck":{
"relevant":["BBankG", "FinDAG","KWG","FKAG","ZAG","AnlEntG","EinSiG","ZKG","GwG","PfandBG","BauSparkG","FMStFG","FMStBG","KredReorgG","RStruktFG","SAG","ScheckG","WG"]
},
"ArbText beck":{
"relevant":["AGG", "AEntG","ArbnErfG","AÜG","ArbGG","AnlEntG","ArbSchG","ArbZG","AsylbLG","AAG","ÄArbVtrG","BBiG","BetrVG","BEEG","BUrlG","DrittelbG","EntgTranspG","EBRG", "FPfZG", "HAG", "KSchG", "LadSchlG", "MiLoG", "MitbestG", "MontanMitbestGErgG", "MontanMitbestG", "MuSchG", "NachwG", "PflegeZG", "SprAuG", "TVG", "TzBfG", "WissZeitVG", "ZPO"]
}
}
@staticmethod
def get_subgraph(nodes):
return Graph.bigDiMul.subgraph(nodes).copy()
def __init__(self,hand_picked,hand_picked_auszuege=[]):
not_found = []
found_rel = []
found_aus = []
for pick_raw in hand_picked:
pick = unicodedata.normalize("NFKC", pick_raw)
found = False
for node in Gesetz.collected_laws.keys():
if pick == node:
found = True
found_rel.append(node)
if not found:
not_found.append(node)
for pick_raw in hand_picked_auszuege:
pick = unicodedata.normalize("NFKC", pick_raw)
found = False
for node in Gesetz.collected_laws.keys():
if node==pick:
found = True
found_aus.append(node)
if not found:
not_found.append(node)
print("not found: " + str(not_found))
self.G_all = Graph.get_subgraph(found_rel+found_aus)
self.G_rel = Graph.get_subgraph(found_rel)
self.build_layers()
self.core = None
for component in strongly_connected_components(self.G_rel):
if self.core == None or len(self.core)<len(component):
self.core = component
def get_auszug(self,node):
return 0 if node not in self.G_rel else 1
def get_core(self,node):
return 1 if node in self.core else 0
def get_topic(self,node):
return Graph.topic[node]
def get_louvain(self,node):
return Graph.louvain[node]
def get_pagerank(self,node):
return Graph.pagerank[node]
def get_flow(self):
return nx.flow_hierarchy(self.G_rel)
def get_closure(self):
inner = 0
total = 0
for node in self.G_rel:
for nbr in self.bigDiMul.neighbors(node):
total = total +1
if nbr in self.G_rel:
inner = inner +1
return inner/total
def get_layer(self,node):
return self.layers[node]
def get_picked(self,node):
for name in Graph.hand_picked:
if node in Graph.hand_picked[name]["relevant"]:
return name
return "None"
def build_layers(self):
def build_metagraph(components,graph):
G = nx.MultiDiGraph()
for key in set(components.values()):
G.add_node(key)
for (s,t,n) in graph.edges:
#if n==0:
#print(s+"->"+t)
if components[s] != components[t]:
G.add_edge(components[s],components[t])
return G
def get_topo_sort(graph,sort_key):
comp = strongly_connected_components(graph)
components = {}
i = 0
keys = {}
for component in strongly_connected_components(graph):
i = i+1
for node in component:
components[node] = i
if i not in keys or keys[i]<sort_key(node):
keys[i]=sort_key(node)
meta_graph = build_metagraph(components,graph)
to_return = []
def max_key(compared_meta_node):
to_return = keys[compared_meta_node]
#print("returned: "+str(to_return))
return to_return
#print(keys)
connected_components = list(reversed(list(lexicographical_topological_sort(meta_graph,max_key))))
for index in range(len(connected_components)):
meta_node = connected_components[index]
to_return.append([])
for node in components:
if components[node] == meta_node:
to_return[-1].append(node)
return to_return
def sort_digraph(graph,sort_key):
to_sort = []
for comp in weakly_connected_components(graph):
to_sort.append((len(comp),comp))
to_sort.sort(reverse=True)
to_return = []
for comp in to_sort:
#print(comp)
sub_graph = Graph.get_subgraph(comp[1])
to_return.append(get_topo_sort(sub_graph,sort_key))
return to_return
def sort_key(node):
return datetime.datetime.today() - Gesetz.collected_laws[node].date
sorted_di = sort_digraph(self.G_rel,sort_key)
self.layers = {}
for weak_component in sorted_di:
for index in range(len(weak_component)):
for node in weak_component[-1-index]:
self.layers[node] = index+3
for node in self.G_all:
if node not in self.G_rel:
self.layers[node] = 0
def add_to_network(self,network,theshhold=0):
def addnode(node,nodes):
nodes[node]={}
nodes[node]["Topic"] = self.get_topic(node)
nodes[node]["Auszug"] = self.get_auszug(node)
nodes[node]["Core"] = self.get_core(node)
nodes[node]["TopoSort"]= self.get_layer(node)
nodes[node]["Louvain"]= self.get_louvain(node)
nodes[node]["PageRank"]= self.get_pagerank(node)
nodes[node]["Rechtsgebiet"] = self.get_picked(node)
nodes[node]["ist_in_sammlung"] = True if self.get_picked(node) != None else False
nodes = {}
if theshhold == 0:
for node in self.G_all:
addnode(node,nodes)
edges = []
for (s,t,n) in self.G_all.edges:
if(n>=theshhold):
if s not in nodes:
addnode(s,nodes)
if t not in nodes:
addnode(t,nodes)
edges.append([s,t])
network.add_layer(
nodes=nodes,
adjacency=edges
)
@staticmethod
def set_big(collected_laws):
Graph.bigDiMul = nx.MultiDiGraph()
for law in Gesetz.collected_laws.values():
Graph.bigDiMul.add_node(law.name_short)
for law in Gesetz.collected_laws.values():
for link in law.links:
if link.target in Graph.bigDiMul and law.name_short in Graph.bigDiMul:
if link.target == None:
print("Found null node and link!")
else:
Graph.bigDiMul.add_edge(law.name_short,link.target)
Graph.pagerank = nx.pagerank(nx.DiGraph(Graph.bigDiMul))
center = None
for component in weakly_connected_components(Graph.bigDiMul):
if center == None or len(center)<len(component):
center = component
bigMul = nx.MultiGraph()
for (s,t) in Graph.bigDiMul.edges():
if s in center and t in center:
bigMul.add_edge(s,t)
coms1 = algorithms.louvain(bigMul,resolution=0.9)
Graph.louvain = {}
for node in Graph.bigDiMul.nodes:
Graph.louvain[node]=0
index = 1
for com in coms1.communities:
index = index+1
for node in com:
Graph.louvain[node] = index
Graph.topic = {}
for node in Graph.bigDiMul:
Graph.topic[node] = collected_laws[node].get_topic()
print(set(Graph.louvain.values()))
@staticmethod
def build_handpicked(name):
if "auszuege" in Graph.hand_picked[name]:
return Graph(Graph.hand_picked[name]["relevant"],Graph.hand_picked[name]["auszuege"])
else:
return Graph(Graph.hand_picked[name]["relevant"])
@staticmethod
def get_handpicked_laws(name):
return [unicodedata.normalize("NFKC", law) for law in Graph.hand_picked[name]["relevant"] if unicodedata.normalize("NFKC", law) in Graph.bigDiMul]
@staticmethod
def get_topic_laws(name):
return [law for law in Graph.bigDiMul if Graph.topic[law]==name]
@staticmethod
def build_topics(topics):
relevant = []
for law in Graph.bigDiMul:
if Graph.topic[law] in topics:
relevant.append(law)
return Graph(relevant)
@staticmethod
def build_louvain(array):
relevant = []
for law in Graph.bigDiMul:
if Graph.louvain[law] in array:
relevant.append(law)
#to_return = nx.Graph()
#for node in relevant:
# to_return.append(node)
#for (s,t) in Graph.bigDiMul.edges():
# if Graph.louvain[s] == Graph.louvain[t]:
# to_return.add_edge(s,t)
return Graph(relevant)
@staticmethod
def build_all():
return Graph([node for node in Graph.bigDiMul.node])
def filter_oldest(self,amount):
nodes = []
for node in self.G_all:
heapq.heappush(nodes,(datetime.datetime.today() - Gesetz.collected_laws[node].date,node))
if len(nodes) > amount:
heapq.heappop(nodes)
return Graph([node[1] for node in nodes])