-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.py
More file actions
306 lines (257 loc) · 9.82 KB
/
visualizer.py
File metadata and controls
306 lines (257 loc) · 9.82 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
'''
CS141 Legal Access For All
visualizer.py
'''
import json
import csv
import re
import sys
csv.field_size_limit(sys.maxsize)
################################################################################
# CONSTANTS
################################################################################
DATA_FOLDER = './data/'
################################################################################
# PROCESS STATUTES
################################################################################
def process_sqldump_data():
pass
def process_scraper_data():
'''
Extract the information needed to visualize the statutes in a network.
Put it into a standardized format.
Here's the standardized format:
"data": [
{"id": "CA COML 4104",
"titles": ["California Commercial Code - COM",
"Division 4. Bank Deposits and Collections",
"Chapter 1. General Provisions and Definitions"]},
"text": ["(a)In this division unless the context otherwise requires:",
"(1)Account means any deposit or credit account with a bank,
including a demand, time, savings, passbook, share draft, or
like account, other than an account evidenced by a
certificate of deposit."]},
{"id": ... }
]
'''
statutes_file = 'CA_statutes.csv'
BEGIN_COM = 16641
END_COM = 17262
data = []
with open(DATA_FOLDER + statutes_file, 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
raw_data = [d for d in spamreader]
for line in raw_data[BEGIN_COM: END_COM]:
# Find the column with section number
section_num_index = 0
for i in range(len(line)):
m = re.match("^([A-Z\s&]+)(App.)?\s+([0-9.]+)$", line[i])
if m:
section_num_index = i
if section_num_index == 0:
print "The section number was the first one! Something's fishy"
sys.exit()
break
# Couldn't find section number. Who knows what happened.
if not m:
print 'Could not find section title and num. Build a better regex!'
# print line
continue
statute_title = m.group(1).strip()
if m.group(3) != None:
statute_num = m.group(3)
else:
statute_num = m.group(2)
statute_fullname = statute_title + ' ' + statute_num
datum = {}
datum['id'] = statute_fullname
datum['titles'] = line[:section_num_index]
datum['text'] = line[section_num_index + 1:]
datum['url'] = helper_convert_statute_to_link(datum)
data.append(datum)
return data
def data_to_force_graph(data):
'''
Convert standardized data format to force graph layout data structure.
Example:
"nodes": [
{"id": "Myriel", "group": 1},
{"id": "Mme.Hucheloup", "group": 8}
],
"links": [
{"source": "Napoleon", "target": "Myriel", "value": 1},
{"source": "Mlle.Baptistine", "target": "Myriel", "value": 8},
{"source": "Cravatte", "target": "Myriel", "value": 1}
]
'''
nodes, links = {}, []
divisions = ["None"]
for d in data:
node = {}
node['id'] = d['id']
# Add the group equal to the division
if len(d['titles']) >= 2 and d['titles'][1] in divisions:
node['group'] = divisions.index(d['titles'][1])
elif len(d['titles']) >= 2 and d['titles'][1] not in divisions:
divisions.append(d['titles'][1])
node['group'] = divisions.index(d['titles'][1])
else:
print "Warning: No Division found"
node['group'] = 0
statute = ' '.join(d['text'])
node['statute'] = statute
node['url'] = d['url']
node['d'] = 0
nodes[d['id']] = node
# nodes.append(node)
# Add the links to sections which are mentioned in this statute
pattern = re.compile(r"Section (\d+)", re.IGNORECASE)
seen = set() # Keep track of sections we've seen, so we don't add twice
statute_title = re.match("^([A-Z\s&]+)\s([0-9.]+)$", d['id']).group(1)
for m in re.finditer(pattern, statute):
if m.group(1) not in seen:
link = {}
link['source'] = d['id']
link['target'] = statute_title + ' ' + m.group(1)
link['value'] = 1
links.append(link)
# Some nodes are unfortunately not caught/parsed but still show up in the
# statutes. Add the nodes.
for l in links:
if all(l['source'] != n['id'] for n in nodes.values()):
node = {}
node['id'] = l['source']
node['group'] = 0
node['d'] = 0
nodes[node['id']] = node
# nodes.append(node)
if all(l['target'] != n['id'] for n in nodes.values()):
node = {}
node['id'] = l['target']
node['group'] = 0
node['d'] = 0
nodes[node['id']] = node
# nodes.append(node)
for link in links:
nodes[link['target']]['d'] += 1
nodes = nodes.values()
network = {}
network['nodes'] = nodes
network['links'] = links
with open('network.json', 'w') as f:
json.dump(network, f)
def data_to_collapsible_graph(data):
'''
Converts the standardized format to hybrid collapsible force layout data
structure.
Example:
{
"name": "flare",
"collapsible": true
"children": [
{"name": "AgglomerativeCluster", "group": 1,
"refers": ["MergeEdge", "BetweennessCentrality"]
},
{"name": "CommunityStructure", "group": 1},
{"name": "HierarchicalCluster", "group": 1},
{"name": "MergeEdge", "group": 1},
{"name": "graph", "group": 2,
"children": [
{"name": "BetweennessCentrality", "group": 2},
{"name": "LinkDistance", "group": 2},
]
}
]
}
'''
network = {}
divisions = ["None"]
for d in data:
# Set the group id equal to the division
if len(d['titles']) >= 2 and d['titles'][1] in divisions:
groupid = divisions.index(d['titles'][1])
elif len(d['titles']) >= 2 and d['titles'][1] not in divisions:
divisions.append(d['titles'][1])
groupid = divisions.index(d['titles'][1])
else:
print "Warning: No Division found"
groupid = 0
cur = network
for t in d['titles']:
# Iterate to the node where d['titles'][i] is supposed to be.
# If the current node doesn't exist in the network, then add it.
if 'children' not in cur:
cur['children'] = []
if all(t != c['name'] for c in cur['children']):
cur['children'].append({"name": t, "group": groupid})
cur = next(x for x in cur['children'] if x['name'] == t)
if 'children' not in cur:
cur['children'] = []
node = {"name": d['id'], "group": groupid, "url": d['url']}
# Add the links to sections which are mentioned in this statute
statute = ' '.join(d['text'])
pattern = re.compile(r"Section (\d+)", re.IGNORECASE)
seen = set() # Keep track of sections we've seen, so we don't add twice
statute_title = re.match("^([A-Z\s&]+)\s([0-9.]+)$", d['id']).group(1)
for m in re.finditer(pattern, statute):
if m.group(1) not in seen:
if "refers" not in node:
node['refers'] = []
node['refers'].append(statute_title + ' ' + m.group(1))
cur['children'].append(node)
# HACKFIX: Accidentally puts root in a children list. Cut the root. I guess
# this isn't a bug if there are more than one code.
network = network['children'][0]
# Remove group from the commercial code.
network['group'] = 0
# Mark the divisions (first layer) as collapsible
for c in network['children']:
c['collapsible'] = True
# Sanity check the references to ensure there are corresponding names.
# Remove references if they don't exist.
seen = set()
helper_collect_names(network, seen)
helper_enforce_refers_exist(network, seen)
with open('collapsible.json', 'w') as f:
json.dump(network, f)
def helper_collect_names(cur, seen):
'''
Recursive helper function for data_to_collapsible_graph. Meant to collect
up the ids (names) of the statutes.
'''
if "children" not in cur:
seen.add(cur['name'])
else:
for c in cur['children']:
helper_collect_names(c, seen)
def helper_enforce_refers_exist(cur, seen):
'''
Recursively helper function for data_to_collapsible_graph. Make sure
the references within each statute actually exist.
'''
if "children" not in cur:
if "refers" in cur:
cur['refers'] = list(seen.intersection(set(cur['refers'])))
else:
for c in cur['children']:
helper_enforce_refers_exist(c, seen)
def helper_convert_statute_to_link(statute):
"""
Converts an input of a statute <type 'dict'> and returns a string
corresponding to the URL of that statute.
"""
code = statute['titles'][0]
code_labels = [label.lower().strip('california').strip() \
for label in code.split('-')]
sect = statute['id'].split(' ')[-1]
# Construct URL
url = 'http://codes.findlaw.com/ca/'
url += code_labels[0].replace(' ', '-') + '/'
url += code_labels[1] + '-sect-' + sect + '.html'
return url
def main():
data = process_scraper_data()
data_to_force_graph(data)
# data_to_collapsible_graph(data)
if __name__ == "__main__":
main()