-
Notifications
You must be signed in to change notification settings - Fork 3
/
__main__.py
506 lines (466 loc) · 17.7 KB
/
__main__.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import argparse
import os
import re
from statics import (
ALL_YEARS,
ALL_YEARS_REG,
DE_CROSSREFERENCE_EDGELIST_PATH,
DE_CROSSREFERENCE_GRAPH_PATH,
DE_HIERARCHY_GRAPH_PATH,
DE_REFERENCE_PARSED_PATH,
DE_REG_AUTHORITY_EDGELIST_PATH,
DE_REG_CROSSREFERENCE_EDGELIST_PATH,
DE_REG_CROSSREFERENCE_GRAPH_PATH,
DE_REG_HIERARCHY_GRAPH_PATH,
DE_REG_REFERENCE_PARSED_PATH,
DE_REG_SNAPSHOT_MAPPING_EDGELIST_PATH,
DE_REG_SNAPSHOT_MAPPING_INDEX_PATH,
DE_SNAPSHOT_MAPPING_EDGELIST_PATH,
DE_SNAPSHOT_MAPPING_INDEX_PATH,
US_CROSSREFERENCE_EDGELIST_PATH,
US_CROSSREFERENCE_GRAPH_PATH,
US_HIERARCHY_GRAPH_PATH,
US_REFERENCE_PARSED_PATH,
US_REG_AUTHORITY_EDGELIST_PATH,
US_REG_CROSSREFERENCE_EDGELIST_PATH,
US_REG_CROSSREFERENCE_GRAPH_PATH,
US_REG_HIERARCHY_GRAPH_PATH,
US_REG_REFERENCE_PARSED_PATH,
US_REG_SNAPSHOT_MAPPING_EDGELIST_PATH,
US_REG_SNAPSHOT_MAPPING_INDEX_PATH,
US_SNAPSHOT_MAPPING_EDGELIST_PATH,
US_SNAPSHOT_MAPPING_INDEX_PATH,
)
from statutes_pipeline_steps.crossreference_graph import CrossreferenceGraphStep
from statutes_pipeline_steps.de_authority_edgelist import DeAuthorityEdgelist
from statutes_pipeline_steps.de_crossreference_edgelist import DeCrossreferenceEdgelist
from statutes_pipeline_steps.de_crossreference_lookup import DeCrossreferenceLookup
from statutes_pipeline_steps.de_law_names import DeLawNamesStep
from statutes_pipeline_steps.de_prepare_input import de_prepare_input
from statutes_pipeline_steps.de_reference_areas import DeReferenceAreasStep
from statutes_pipeline_steps.de_reference_parse import DeReferenceParseStep
from statutes_pipeline_steps.de_to_xml import DeToXmlStep, get_type_for_doknr_dict
from statutes_pipeline_steps.hierarchy_graph import HierarchyGraphStep
from statutes_pipeline_steps.snapshot_mapping_edgelist import (
SnapshotMappingEdgelistStep,
)
from statutes_pipeline_steps.snapshot_mapping_index import SnapshotMappingIndexStep
from statutes_pipeline_steps.us_authority_edgelist import UsAuthorityEdgelist
from statutes_pipeline_steps.us_crossreference_edgelist import UsCrossreferenceEdgelist
from statutes_pipeline_steps.us_crossreference_lookup import UsCrossreferenceLookup
from statutes_pipeline_steps.us_prepare_input import us_prepare_input
from statutes_pipeline_steps.us_reference_areas import UsReferenceAreasStep
from statutes_pipeline_steps.us_reference_parse import UsReferenceParseStep
from statutes_pipeline_steps.us_reg_prepare_input import us_reg_prepare_input
from statutes_pipeline_steps.us_reg_to_xml import UsRegsToXmlStep
from statutes_pipeline_steps.us_to_xml import UsToXmlStep
from utils.common import load_law_names, load_law_names_compiled, str_to_bool
def get_subseqitem_conf(subseqitems):
if subseqitems is None:
return False, True
elif subseqitems is True:
return (True,)
elif subseqitems is False:
return (False,)
ALL_STEPS = [
"prepare_input",
"xml",
"law_names", # DE only
"reference_areas",
"reference_parse",
"hierarchy_graph",
"crossreference_lookup",
"crossreference_edgelist",
"authority_edgelist",
"crossreference_graph",
# creates edgelist to map nodes between snapshots for DYNAMIC graph
"snapshot_mapping_index",
"snapshot_mapping_edgelist",
]
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("dataset", help="select a dataset: DE or US")
parser.add_argument("steps", nargs="+", help="select a step to perform by name")
parser.add_argument("--filter", nargs="*", help="filter for specific files")
parser.add_argument(
"--single-process",
dest="use_multiprocessing",
action="store_const",
const=False,
default=True,
help="prevent multiprocessing",
)
parser.add_argument(
"--overwrite",
dest="overwrite",
action="store_const",
const=True,
default=False,
help="overwrite files",
)
parser.add_argument(
"--subseqitems",
dest="subseqitems",
nargs="?",
const=True,
type=str_to_bool,
default=None,
help="include subseqitems in graphs",
)
parser.add_argument(
"--snapshots",
dest="snapshots",
nargs="*",
type=str,
default=["all"],
help=(
"snapshots for crossreferences. Eg. 2010-01-01 for de dataset or 2010 for "
"us dataset. To run on whole research window: all or all-new-years"
),
)
parser.add_argument(
"--interval",
dest="interval",
nargs="?",
type=int,
default=1,
help=(
"Only for snapshot_mapping_edgelist. Interval for mapped snapshots. "
"Default 1 (snapshot)"
),
)
parser.add_argument(
"-r",
"--regulations",
dest="regulations",
action="store_const",
const=True,
default=False,
help="Include regulations",
)
parser.add_argument(
"-dc",
"--detailed-crossreferences",
dest="detailed_crossreferences",
action="store_const",
const=True,
default=False,
help="Resolve cross references on the lowest possible level. "
"Default is to resolve on seqitem level (e.g. sections).",
)
args = parser.parse_args()
steps = [step.lower() for step in args.steps]
dataset = args.dataset.lower()
use_multiprocessing = args.use_multiprocessing
processes = None if args.use_multiprocessing else 1
overwrite = args.overwrite
snapshots = args.snapshots
interval = args.interval
selected_items = args.filter or []
regulations = args.regulations
detailed_crossreferences = args.detailed_crossreferences
if dataset not in ["de", "us"]:
raise Exception(f"{dataset} unsupported dataset. Options: us, de")
if "all" in snapshots or "all-new-years" in snapshots:
years = ALL_YEARS_REG if regulations else ALL_YEARS
if dataset == "us":
snapshots = [f"{year}" for year in years]
elif dataset == "de":
snapshots = [
f"{year}-12-31" if "all" in snapshots else f"{year}-01-01"
for year in years
]
if "all" in steps:
steps = ALL_STEPS
else:
unknown_steps = [s for s in steps if s not in ALL_STEPS]
assert not unknown_steps, unknown_steps
if (
"crossreference_lookup" in steps
or "crossreference_edgelist" in steps
or "crossreference_graph" in steps
):
if dataset == "de" or snapshots:
for snapshot in snapshots:
if not re.fullmatch(r"\d{4}(-\d{2}-\d{2})?", snapshot):
raise Exception(
"Add --snapshots as argument. "
"E.g. for de --snapshots 2012-01-31 2013-01-31 or for us "
"--snapshot 2001"
)
if detailed_crossreferences and regulations:
raise Exception(
"Combining detailed cross-references and regulations is not tested."
)
if "prepare_input" in steps:
if dataset == "us":
if regulations:
us_reg_prepare_input()
else:
us_prepare_input()
elif dataset == "de":
de_prepare_input(regulations)
print("Filter input: done")
if "xml" in steps:
if dataset == "us":
if regulations:
step = UsRegsToXmlStep(processes)
else:
step = UsToXmlStep(processes)
items = step.get_items(overwrite)
step.execute_filtered_items(items, selected_items)
elif dataset == "de":
dok_type_dict = get_type_for_doknr_dict()
step = DeToXmlStep(
regulations=regulations,
processes=processes,
dok_type_dict=dok_type_dict,
)
items = step.get_items(overwrite)
step.execute_filtered_items(items, selected_items)
print("Convert to xml: done")
if "law_names" in steps:
if dataset == "de":
step = DeLawNamesStep(regulations=regulations, processes=processes)
items = step.get_items()
step.execute_items(items)
print("Law names: done")
if "reference_areas" in steps:
if dataset == "us":
step = UsReferenceAreasStep(regulations=regulations, processes=processes)
items = step.get_items(overwrite)
step.execute_filtered_items(items)
elif dataset == "de":
law_names = load_law_names_compiled(regulations)
step = DeReferenceAreasStep(
law_names=law_names, regulations=regulations, processes=processes
)
items = step.get_items(overwrite)
step.execute_filtered_items(items)
print("Extract reference areas: done")
if "reference_parse" in steps:
if dataset == "us":
step = UsReferenceParseStep(regulations=regulations, processes=processes)
items = step.get_items(overwrite)
step.execute_filtered_items(items)
if dataset == "de":
law_names = load_law_names_compiled(regulations)
step = DeReferenceParseStep(
law_names=law_names, regulations=regulations, processes=processes
)
items = step.get_items(overwrite)
step.execute_filtered_items(items)
print("Parse references: done")
if "hierarchy_graph" in steps:
# for subseqitems_conf in get_subseqitem_conf(args.subseqitems):
for subseqitems_conf in [True]:
if dataset == "us":
source = (
US_REG_REFERENCE_PARSED_PATH
if regulations
else US_REFERENCE_PARSED_PATH
)
destination = os.path.join(
US_REG_HIERARCHY_GRAPH_PATH
if regulations
else US_HIERARCHY_GRAPH_PATH,
"subseqitems" if subseqitems_conf else "seqitems",
)
elif dataset == "de":
source = (
DE_REG_REFERENCE_PARSED_PATH
if regulations
else DE_REFERENCE_PARSED_PATH
)
destination = (
(
DE_REG_HIERARCHY_GRAPH_PATH
if regulations
else DE_HIERARCHY_GRAPH_PATH
)
+ "/"
+ ("subseqitems" if subseqitems_conf else "seqitems")
)
step = HierarchyGraphStep(
source=source,
destination=destination,
add_subseqitems=subseqitems_conf,
processes=processes,
)
items = step.get_items(overwrite)
step.execute_filtered_items(items)
print("Make hierarchy graphs: done")
if "crossreference_lookup" in steps:
if dataset == "us":
step = UsCrossreferenceLookup(
detailed_crossreferences=detailed_crossreferences,
regulations=regulations,
processes=processes,
)
items = step.get_items(overwrite, snapshots)
step.execute_items(items)
elif dataset == "de":
assert not detailed_crossreferences
step = DeCrossreferenceLookup(regulations=regulations, processes=processes)
items = step.get_items(snapshots)
step.execute_items(items)
print("Create crossreference lookup: done")
if "crossreference_edgelist" in steps:
if dataset == "us":
step = UsCrossreferenceEdgelist(
detailed_crossreferences=detailed_crossreferences,
regulations=regulations,
processes=processes,
)
items = step.get_items(overwrite, snapshots)
step.execute_items(items)
elif dataset == "de":
assert not detailed_crossreferences
law_names_data = load_law_names(regulations)
step = DeCrossreferenceEdgelist(
regulations=regulations,
law_names_data=law_names_data,
processes=processes,
)
items = step.get_items(overwrite, snapshots)
step.execute_items(items)
print("Create crossreference edgelist: done")
if "authority_edgelist" in steps:
if dataset == "de" and regulations:
law_names_data = load_law_names(regulations)
step = DeAuthorityEdgelist(
law_names_data=law_names_data, processes=processes
)
items = step.get_items(overwrite, snapshots)
step.execute_items(items)
elif dataset == "us" and regulations:
assert not detailed_crossreferences
step = UsAuthorityEdgelist(
detailed_crossreferences=detailed_crossreferences,
processes=processes,
regulations=regulations,
)
items = step.get_items(overwrite, snapshots)
step.execute_items(items)
print("Create authority edgelist: done")
if "crossreference_graph" in steps:
if dataset == "us":
source = US_HIERARCHY_GRAPH_PATH
source_regulation = US_REG_HIERARCHY_GRAPH_PATH
destination = (
US_REG_CROSSREFERENCE_GRAPH_PATH
if regulations
else US_CROSSREFERENCE_GRAPH_PATH
) + ("/detailed" if detailed_crossreferences else "")
edgelist_folder = (
US_REG_CROSSREFERENCE_EDGELIST_PATH
if regulations
else US_CROSSREFERENCE_EDGELIST_PATH
) + ("/detailed" if detailed_crossreferences else "")
authority_edgelist_folder = US_REG_AUTHORITY_EDGELIST_PATH
elif dataset == "de":
assert not detailed_crossreferences
source = (
DE_REG_HIERARCHY_GRAPH_PATH if regulations else DE_HIERARCHY_GRAPH_PATH
)
source_regulation = None
destination = (
DE_REG_CROSSREFERENCE_GRAPH_PATH
if regulations
else DE_CROSSREFERENCE_GRAPH_PATH
)
edgelist_folder = (
DE_REG_CROSSREFERENCE_EDGELIST_PATH
if regulations
else DE_CROSSREFERENCE_EDGELIST_PATH
)
authority_edgelist_folder = DE_REG_AUTHORITY_EDGELIST_PATH
step = CrossreferenceGraphStep(
regulations=regulations,
source=source,
source_regulation=source_regulation,
destination=destination,
edgelist_folder=edgelist_folder,
dataset=dataset,
authority_edgelist_folder=authority_edgelist_folder,
processes=processes,
)
items = step.get_items(overwrite, snapshots)
step.execute_items(items)
print("Make crossreference graph: done")
if "snapshot_mapping_index" in steps:
assert not detailed_crossreferences
if dataset == "us":
source_text = (
[US_REFERENCE_PARSED_PATH, US_REG_REFERENCE_PARSED_PATH]
if regulations
else US_REFERENCE_PARSED_PATH
)
destination = os.path.join(
US_REG_SNAPSHOT_MAPPING_INDEX_PATH
if regulations
else US_SNAPSHOT_MAPPING_INDEX_PATH,
"subseqitems",
)
law_names_data = None
elif dataset == "de":
source_text = (
DE_REG_REFERENCE_PARSED_PATH
if regulations
else DE_REFERENCE_PARSED_PATH
)
destination = os.path.join(
DE_REG_SNAPSHOT_MAPPING_INDEX_PATH
if regulations
else DE_SNAPSHOT_MAPPING_INDEX_PATH,
"subseqitems",
)
law_names_data = load_law_names(regulations)
step = SnapshotMappingIndexStep(
source_text,
destination,
dataset,
law_names_data,
processes=processes,
)
items = step.get_items(overwrite, snapshots)
step.execute_items(items)
print("Make snapshot mapping: done")
if "snapshot_mapping_edgelist" in steps:
assert not detailed_crossreferences
if dataset == "us":
source = os.path.join(
US_REG_SNAPSHOT_MAPPING_INDEX_PATH
if regulations
else US_SNAPSHOT_MAPPING_INDEX_PATH,
"subseqitems",
)
destination = os.path.join(
US_REG_SNAPSHOT_MAPPING_EDGELIST_PATH
if regulations
else US_SNAPSHOT_MAPPING_EDGELIST_PATH,
"subseqitems",
)
elif dataset == "de":
source = os.path.join(
DE_REG_SNAPSHOT_MAPPING_INDEX_PATH
if regulations
else DE_SNAPSHOT_MAPPING_INDEX_PATH,
"subseqitems",
)
destination = os.path.join(
DE_REG_SNAPSHOT_MAPPING_EDGELIST_PATH
if regulations
else DE_SNAPSHOT_MAPPING_EDGELIST_PATH,
"subseqitems",
)
step = SnapshotMappingEdgelistStep(
source,
destination,
interval,
dataset,
processes=processes,
)
items = step.get_items(overwrite, snapshots)
step.execute_items(items)
print("Make snapshot mapping: done")