forked from eregs/regulations-parser
/
reg_text.py
256 lines (211 loc) · 8.44 KB
/
reg_text.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
# vim: set encoding=utf-8
import re
import string
from regparser import utils
from regparser.citations import internal_citations, Label
from regparser.grammar.unified import marker_subpart_title
from regparser.search import find_offsets, find_start, segments
from regparser.tree import struct
from regparser.tree.appendix.carving import find_appendix_start
from regparser.tree.paragraph import ParagraphParser
from regparser.tree.supplement import find_supplement_start
def build_subparts_tree(text, part, subpart_builder):
""" Build a tree of a subpart and its child sections.
subpart_builder can be a builder that builds a subpart or an
emptypart. """
subpart = subpart_builder(part)
sects = sections(text, part)
if sects:
children = []
for s, e in sects:
section_text = text[s:e]
children.append(build_section_tree(section_text, part))
subpart.children = children
return (subpart, text[:sects[0][0]])
return (subpart, text)
def build_reg_text_tree(text, part):
"""Build up the whole tree from the plain text of a single regulation. This
only builds the regulation text part, and does not include appendices or
the supplement. """
title, body = utils.title_body(text)
label = [str(part)]
subparts_list = []
subpart_locations = subparts(body)
if subpart_locations:
pre_subpart = body[:subpart_locations[0][0]]
first_emptypart, children_text = build_subparts_tree(
pre_subpart, part, build_empty_part)
if pre_subpart.strip() and first_emptypart.children:
subparts_list.append(first_emptypart)
else:
children_text = pre_subpart
for start, end in subpart_locations:
subpart_body = body[start:end]
subpart, _ = build_subparts_tree(
subpart_body, part, lambda p: build_subpart(subpart_body, p))
subparts_list.append(subpart)
else:
emptypart, children_text = build_subparts_tree(
body, part, build_empty_part)
if emptypart.children:
subparts_list.append(emptypart)
else:
return struct.Node(
text, [build_empty_part(part)], label, title)
return struct.Node(children_text, subparts_list, label, title)
regParser = ParagraphParser(r"\(%s\)", struct.Node.REGTEXT)
def build_empty_part(part):
""" When a regulation doesn't have a subpart, we give it an emptypart (a
dummy subpart) so that the regulation tree is consistent. """
label = [str(part), 'Subpart']
return struct.Node(
'', [], label, '', node_type=struct.Node.EMPTYPART)
def build_subpart(text, part):
results = marker_subpart_title.parseString(text)
subpart_letter = results.subpart
subpart_title = results.subpart_title
label = [str(part), 'Subpart', subpart_letter]
return struct.Node(
"", [], label, subpart_title, node_type=struct.Node.SUBPART)
def subjgrp_label(starting_title, letter_list):
words = starting_title.split()
candidate_title = ""
suffixes = [""] + list(string.lowercase)
if len(words) == 1:
"""
E.g. if the word is "Penalties" the progression is:
Pe
Pe.
Pen
Pen.
Pena
Pena.
<etc.>
Penalties.
Penalties-b.
Penalties-c.
<etc.>
"""
word = words[0]
terminator = ""
suffix_pos = 0
pos = min([2, len(word)])
while candidate_title == "" or candidate_title in letter_list:
suffix = '-%s' % suffixes[suffix_pos] if suffix_pos else ''
candidate_title = '%s%s%s' % (word[:pos], terminator, suffix)
if terminator:
terminator = ""
if pos < len(word):
pos = pos + 1
else:
suffix_pos = suffix_pos + 1
else:
terminator = "."
return candidate_title
else:
"""
E.g. if the title is "Change of Ownership" the progression is:
CoO
C.o.O.
C_o_O
ChofOw
Ch.of.Ow.
<etc.>
ChangeofOwnership-a
"""
separators = ("", ".", "_")
separator_pos, suffix_pos = 0, 0
num_letters = 1
longest = max([len(word) for word in words])
while candidate_title == "" or candidate_title in letter_list:
sep = separators[separator_pos]
suffix = suffixes[suffix_pos]
suffix = "-%s" % suffix if suffix else ""
suffix = "%s%s" % (sep, suffix) if sep == "." else suffix
candidate_title = "%s%s" % (sep.join(
word[:num_letters] for word in words), suffix)
if separator_pos + 1 < len(separators):
separator_pos = separator_pos + 1
elif num_letters == longest:
separator_pos = 0
suffix_pos = suffix_pos + 1
else:
separator_pos = 0
num_letters = num_letters + 1
return candidate_title
def build_subjgrp(title, part, letter_list):
"""
We're constructing a fake "letter" here by taking the first letter of each
word in the subjgrp's title, or using the first two letters of the first
word if there's just one—we're avoiding single letters to make sure we
don't duplicate an existing subpart, and we're hoping that the initialisms
created by this method are unique for this regulation.
We can make this more robust by accepting a list of existing initialisms
and returning both that list and the Node, and checking against the list
as we construct them.
"""
letter_title = subjgrp_label(title, letter_list)
letter_list.append(letter_title)
label = [str(part), 'Subjgrp', letter_title]
return (letter_list, struct.Node(label=label, title=title,
node_type=struct.Node.SUBPART))
def find_next_subpart_start(text):
""" Find the start of the next Subpart (e.g. Subpart B)"""
return find_start(text, u'Subpart', ur'[A-Z]—')
def find_next_section_start(text, part):
"""Find the start of the next section (e.g. 205.14)"""
return find_start(text, u"§", str(part) + r"\.\d+")
def next_section_offsets(text, part):
"""Find the start/end of the next section"""
offsets = find_offsets(text, lambda t: find_next_section_start(t, part))
if offsets is None:
return None
start, end = offsets
subpart_start = find_next_subpart_start(text)
appendix_start = find_appendix_start(text)
supplement_start = find_supplement_start(text)
if subpart_start is not None \
and subpart_start > start and subpart_start < end:
end = subpart_start
elif appendix_start is not None and appendix_start < end:
end = appendix_start
elif supplement_start is not None and supplement_start < end:
end = supplement_start
if end >= start:
return (start, end)
def next_subpart_offsets(text):
"""Find the start,end of the next subpart"""
offsets = find_offsets(text, find_next_subpart_start)
if offsets is None:
return None
start, end = offsets
appendix_start = find_appendix_start(text)
supplement_start = find_supplement_start(text)
if appendix_start is not None and appendix_start < end:
end = appendix_start
elif supplement_start is not None and supplement_start < end:
end = supplement_start
if end >= start:
return (start, end)
def sections(text, part):
"""Return a list of section offsets. Does not include appendices."""
def offsets_fn(remaining_text, idx, excludes):
return next_section_offsets(remaining_text, part)
return segments(text, offsets_fn)
def subparts(text):
""" Return a list of subpart offset. Does not include appendices,
supplements. """
def offsets_fn(remaining_text, idx, excludes):
return next_subpart_offsets(remaining_text)
return segments(text, offsets_fn)
def build_section_tree(text, part):
"""Construct the tree for a whole section. Assumes the section starts
with an identifier"""
title, text = utils.title_body(text)
exclude = [(pc.full_start, pc.full_end) for pc in
internal_citations(text, Label(part=part))]
section = re.search(r'%d\.(\d+)\b' % part, title).group(1)
label = [str(part), section]
p_tree = regParser.build_tree(
text, exclude=exclude, label=label, title=title)
return p_tree