def test_find(self): n1 = struct.Node('n1', label=['n1']) n2 = struct.Node('n2', label=['n2']) n3 = struct.Node('n3', label=['n3']) n5 = struct.Node('n5', label=['n1']) self.assertEqual(n1, struct.find(n1, 'n1')) self.assertEqual( n1, struct.find(struct.Node(children=[n1, n2, n3]), 'n1')) self.assertEqual( n1, struct.find(struct.Node(children=[n2, n1, n3]), 'n1')) self.assertEqual( n1, struct.find(struct.Node(children=[n2, n3, n1]), 'n1')) self.assertEqual( n5, struct.find(struct.Node(children=[n2, n5, n3, n1]), 'n1')) self.assertEqual(None, struct.find(n2, 'n1')) self.assertEqual(n2, struct.find(n2, 'n2'))
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 test_treeify_interp(self): n1 = struct.Node(label=['1', 'Interp']) n1b = struct.Node(label=['1', 'b', 'Interp']) n1b5 = struct.Node(label=['1', 'b', '5', 'Interp']) result = struct.treeify([n1, n1b, n1b5]) self.assertEqual(result, [ struct.Node(label=['1', 'Interp'], children=[ struct.Node(label=['1', 'b', 'Interp'], children=[ struct.Node(label=['1', 'b', '5', 'Interp']) ]) ]) ])
def test_treeify(self): n1 = struct.Node(label=['1']) n1b = struct.Node(label=['1', 'b']) n1b5 = struct.Node(label=['1', 'b', '5']) n2 = struct.Node(label=['2']) result = struct.treeify([n1, n1b5, n2, n1b]) self.assertEqual( sorted(result), sorted([ struct.Node( label=['1'], children=[ struct.Node( label=['1', 'b'], children=[struct.Node(label=['1', 'b', '5'])]) ]), struct.Node(label=['2']) ]))
def build_tree(self, text, p_level=0, exclude=[], label=[], title=''): """ Build a dict to represent the text hierarchy. """ subparagraphs = self.paragraphs(text, p_level, exclude) if subparagraphs: body_text = text[0:subparagraphs[0][0]] else: body_text = text children = [] for paragraph, (start, end) in enumerate(subparagraphs): new_text = text[start:end] new_excludes = [(e[0] - start, e[1] - start) for e in exclude] new_label = label + [p_levels[p_level][paragraph]] children.append( self.build_tree(new_text, p_level + 1, new_excludes, new_label)) return struct.Node(body_text, children, label, title, self.node_type)
def assert_depth(self, depth, label, node_type=struct.Node.REGTEXT): node = struct.Node("x", label=label, node_type=node_type) self.assertEqual(depth, node.depth())
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)