def test_compress_context_in_tokenlists(self): tokenized = [ tokens.Context(['123', 'Interpretations']), tokens.Paragraph(part='123', section='23', paragraph='a'), tokens.Verb(tokens.Verb.PUT, True), tokens.TokenList([ tokens.Verb(tokens.Verb.POST, True), tokens.Paragraph(part='123', section='23', paragraphs=['a', '1']), tokens.Paragraph(paragraphs=[None, None, 'i']), tokens.Paragraph(section='23', paragraph='b') ]) ] converted = amdparser.compress_context_in_tokenlists(tokenized) self.assertEqual(converted, [ tokens.Context(['123', 'Interpretations']), tokens.Paragraph(part='123', section='23', paragraph='a'), tokens.Verb(tokens.Verb.PUT, True), tokens.TokenList([ tokens.Verb(tokens.Verb.POST, True), tokens.Paragraph( part='123', section='23', paragraphs=['a', '1']), tokens.Paragraph( part='123', section='23', paragraphs=['a', '1', 'i']), tokens.Paragraph(part='123', section='23', paragraph='b') ]) ])
def test_resolve_confused_context(): tokenized = [tokens.Context([None, None, '12', 'a', '2', 'iii'])] converted = amdparser.resolve_confused_context(tokenized, ['123', 'Interpretations']) assert converted == [ tokens.Context([None, 'Interpretations', '12', '(a)(2)(iii)']) ]
def test_compress_context_simple(): tokenized = [ tokens.Verb(tokens.Verb.PUT, active=True), # part 9876, subpart A tokens.Context(['9876', 'Subpart:A']), # section 12 tokens.Context([None, None, '12']), # 12(f)(4) tokens.Paragraph.make(paragraphs=['f', '4']), # 12(f) tokens.Context([None, None, None, 'g']), # 12(g)(1) tokens.Paragraph.make(paragraphs=[None, '1']), ] converted, final_ctx = amdparser.compress_context(tokenized, []) assert converted == [ tokens.Verb(tokens.Verb.PUT, active=True), tokens.Paragraph.make(part='9876', subpart='A', section='12', paragraphs=['f', '4']), tokens.Paragraph.make(part='9876', subpart='A', section='12', paragraphs=['g', '1']), ] assert ['9876', 'Subpart:A', '12', 'g', '1'] == final_ctx
def test_switch_passive1(): tokenized = [ tokens.Context(['1']), tokens.Verb(tokens.Verb.PUT, active=True), tokens.Context(['2']) ] assert tokenized == amdparser.switch_passive(tokenized)
def test_resolve_confused_context(self): tokenized = [tokens.Context([None, None, '12', 'a', '2', 'iii'])] converted = resolve_confused_context(tokenized, ['123', 'Interpretations']) self.assertEqual( converted, [tokens.Context([None, 'Interpretations', '12', '(a)(2)(iii)'])])
def test_compress_context_in_tokenlists(): tokenized = [ tokens.Context(['123', 'Interpretations']), tokens.Paragraph.make(part='123', section='23', paragraph='a'), tokens.Verb(tokens.Verb.PUT, True), tokens.TokenList([ tokens.Verb(tokens.Verb.POST, True), tokens.Paragraph.make(part='123', section='23', paragraphs=['a', '1']), tokens.Paragraph.make(paragraphs=[None, None, 'i']), tokens.Paragraph.make(section='23', paragraph='b') ]) ] assert amdparser.compress_context_in_tokenlists(tokenized) == [ tokens.Context(['123', 'Interpretations']), tokens.Paragraph.make(part='123', section='23', paragraph='a'), tokens.Verb(tokens.Verb.PUT, True), tokens.TokenList([ tokens.Verb(tokens.Verb.POST, True), tokens.Paragraph.make(part='123', section='23', paragraphs=['a', '1']), tokens.Paragraph.make(part='123', section='23', paragraphs=['a', '1', 'i']), tokens.Paragraph.make(part='123', section='23', paragraph='b') ]) ]
def test_resolve_confused_context_appendix(self): tokenized = [tokens.Context([None, 'Appendix:A', '12'])] converted = resolve_confused_context(tokenized, ['123', 'Interpretations']) self.assertEqual( converted, [tokens.Context([None, 'Interpretations', 'A', '(12)'])])
def test_resolve_confused_context_appendix(): tokenized = [tokens.Context([None, 'Appendix:A', '12'])] converted = amdparser.resolve_confused_context(tokenized, ['123', 'Interpretations']) assert converted == [ tokens.Context([None, 'Interpretations', 'A', '(12)']) ]
def test_example_35(self): text = "5. Section 100.94 is added to subpart C to read as follows:" result = parse_text(text) self.assertEqual(result, [ tokens.Context(['100', None, '94'], certain=False), tokens.Verb(tokens.Verb.POST, active=False, and_prefix=False), tokens.Context([None, 'Subpart:C'], certain=True) ])
def test_contains_one_context(self): tokenized = self.list_of_tokens() context = tokens.Context(['200', '1']) tokenized.append(context) self.assertTrue(contains_one_context(tokenized)) designate_token = tokens.Verb(tokens.Verb.DESIGNATE, True) tokenized.append(designate_token) tokenized.append(tokens.Context(['200', '2'])) self.assertFalse(contains_one_context(tokenized))
def test_example12(self): text = "comment 31(b), amend paragraph 31(b)(2) by adding " text += "paragraphs 4 through 6;" result = parse_text(text) self.assertEqual(result, [ tokens.Context([None, 'Interpretations', '31', '(b)']), tokens.Context([None, 'Interpretations', '31', '(b)(2)']), tokens.Verb(tokens.Verb.POST, active=True), tokens.TokenList([ tokens.Paragraph([None, 'Interpretations', None, None, '4']), tokens.Paragraph([None, 'Interpretations', None, None, '5']), tokens.Paragraph([None, 'Interpretations', None, None, '6']) ]) ])
def test_example3(self): text = "6. Add subpart B to read as follows:" result = parse_text(text) self.assertEqual(result, [ tokens.Verb(tokens.Verb.POST, active=True), tokens.Context([None, 'Subpart:B'], certain=False) ])
def test_context_to_paragraph_exceptions2(): tokenized = [ tokens.Verb(tokens.Verb.PUT, active=True), tokens.Context(['2']), tokens.TokenList([tokens.Paragraph.make(part='3')]) ] assert tokenized == amdparser.context_to_paragraph(tokenized)
def test_switch_part_context(self): initial_context = ['105', '2'] tokenized = [ tokens.Paragraph(part='203', sub='2', section='x'), tokens.Verb(tokens.Verb.DESIGNATE, True) ] self.assertEqual( amdparser.switch_part_context(tokenized, initial_context), []) tokenized = [ tokens.Paragraph(part='105', sub='4', section='j', paragraph='iv'), tokens.Verb(tokens.Verb.DESIGNATE, True) ] self.assertEqual( amdparser.switch_part_context(tokenized, initial_context), initial_context) tokenized = [ tokens.Context(['', '4', 'j', 'iv']), tokens.Verb(tokens.Verb.DESIGNATE, True) ] self.assertEqual( amdparser.switch_part_context(tokenized, initial_context), initial_context)
def test_switch_context(self): initial_context = ['105', '2'] tokenized = [ tokens.Paragraph(['203', '2', 'x']), tokens.Verb(tokens.Verb.DESIGNATE, True) ] self.assertEqual(switch_context(tokenized, initial_context), []) tokenized = [ tokens.Paragraph(['105', '4', 'j', 'iv']), tokens.Verb(tokens.Verb.DESIGNATE, True) ] self.assertEqual(switch_context(tokenized, initial_context), initial_context) tokenized = [ tokens.Context(['', '4', 'j', 'iv']), tokens.Verb(tokens.Verb.DESIGNATE, True) ] self.assertEqual(switch_context(tokenized, initial_context), initial_context)
def test_context_to_paragraph_exceptions(self): tokenized = [ tokens.Verb(tokens.Verb.PUT, active=True), tokens.Context(['2']), tokens.Paragraph(['3']) ] converted = context_to_paragraph(tokenized) self.assertEqual(tokenized, converted) tokenized = [ tokens.Verb(tokens.Verb.PUT, active=True), tokens.Context(['2']), tokens.TokenList([tokens.Paragraph(['3'])]) ] converted = context_to_paragraph(tokenized) self.assertEqual(tokenized, converted)
def test_switch_part_context(): initial_context = ['105', '2'] tokenized = [ tokens.Paragraph.make(part='203', sub='2', section='x'), tokens.Verb(tokens.Verb.DESIGNATE, True) ] assert amdparser.switch_part_context(tokenized, initial_context) == [] tokenized = [ tokens.Paragraph.make(part='105', sub='4', section='j', paragraph='iv'), tokens.Verb(tokens.Verb.DESIGNATE, True) ] assert initial_context == amdparser.switch_part_context( tokenized, initial_context) tokenized = [ tokens.Context(['', '4', 'j', 'iv']), tokens.Verb(tokens.Verb.DESIGNATE, True) ] assert initial_context == amdparser.switch_part_context( tokenized, initial_context)
def test_example1(self): text = u"In § 9876.1, revise paragraph (b) to read as follows" result = parse_text(text) self.assertEqual(result, [ tokens.Context(['9876', None, '1'], certain=True), tokens.Verb(tokens.Verb.PUT, active=True), tokens.Paragraph([None, None, None, 'b']) ])
def test_separate_tokenlist(): tokenized = [ tokens.Context(['1']), tokens.TokenList([ tokens.Verb(tokens.Verb.MOVE, active=True), tokens.Context([None, '2']) ]), tokens.Paragraph.make(sub='3'), tokens.TokenList([tokens.Paragraph.make(section='b')]) ] assert amdparser.separate_tokenlist(tokenized) == [ tokens.Context(['1']), tokens.Verb(tokens.Verb.MOVE, active=True), tokens.Context([None, '2']), tokens.Paragraph.make(sub='3'), tokens.Paragraph.make(section='b') ]
def test_compress_context_interpretations(self): tokenized = [ tokens.Context(['123', 'Interpretations']), tokens.Paragraph([None, None, '12', 'a', '2', 'iii']), tokens.Paragraph([None, 'Interpretations', None, None, '3', 'v']), tokens.Context([None, 'Appendix:R']), tokens.Paragraph([None, 'Interpretations', None, None, '5']) ] converted, _ = compress_context(tokenized, []) self.assertEqual( converted, [ tokens.Paragraph( ['123', 'Interpretations', '12', '(a)(2)(iii)', '3', 'v']), # None because we are missing a layer tokens.Paragraph( ['123', 'Interpretations', 'Appendix:R', None, '5']) ])
def test_separate_tokenlist(self): tokenized = [ tokens.Context(['1']), tokens.TokenList([ tokens.Verb(tokens.Verb.MOVE, active=True), tokens.Context([None, '2']) ]), tokens.Paragraph([None, '3']), tokens.TokenList([tokens.Paragraph([None, None, 'b'])]) ] converted = separate_tokenlist(tokenized) self.assertEqual(converted, [ tokens.Context(['1']), tokens.Verb(tokens.Verb.MOVE, active=True), tokens.Context([None, '2']), tokens.Paragraph([None, '3']), tokens.Paragraph([None, None, 'b']) ])
def test_example17(self): text = "viii. Under comment 31(c)(4), paragraph 2.xi.is added." result = parse_text(text) self.assertEqual(result, [ tokens.Context([None, 'Interpretations', '31', '(c)(4)'], certain=True), tokens.Paragraph([None, 'Interpretations', None, None, '2', 'xi']), tokens.Verb(tokens.Verb.POST, active=False) ])
def test_example2(self): text = u"In § 7654.2, revise the introductory text to read as" text += " follows" result = parse_text(text) self.assertEqual(result, [ tokens.Context(['7654', None, '2'], certain=True), tokens.Verb(tokens.Verb.PUT, active=True), tokens.Paragraph([], field=tokens.Paragraph.TEXT_FIELD) ])
def test_example36(self): text = u'In Appendix A to Part 1002 revise [label:1002-A-p1-2-d] to read:' result = parse_text(text) self.assertEqual(result, [ tokens.Context(['1002', 'Appendix:A'], certain=True), tokens.Verb(tokens.Verb.PUT, active=True, and_prefix=False), tokens.Paragraph(['1002', 'Appendix:A', 'p1', '2', 'd'], field=None) ])
def test_example7(self): text = u'1. On page 1234, in the second column, in Subpart A, § ' text += '4444.3(a) is corrected to read as follows:' result = parse_text(text) self.assertEqual(result, [ tokens.Context([None, 'Subpart:A'], certain=True), tokens.Paragraph(['4444', None, '3', 'a']), tokens.Verb(tokens.Verb.PUT, active=False), ])
def test_example36(self): text = (u'In Appendix A to Part 1002 revise [label:1002-A-p1-2-d] to ' u'read:') result = parse_text(text) self.assertEqual(result, [ tokens.Context(['1002', 'Appendix:A'], certain=True), tokens.Verb(tokens.Verb.PUT, active=True, and_prefix=False), tokens.Paragraph(part='1002', appendix='A', section='p1', paragraphs=['2', 'd']) ])
def test_switch_level2_context(): """The presence of certain types of context should apply throughout the amendment""" initial = ['105', None, '2'] tokenized = [tokens.Paragraph(), tokens.Verb('verb', True)] transform = amdparser.switch_level2_context # shorthand assert transform(tokenized, initial) == initial context = tokens.Context(['105', 'Subpart:G'], certain=False) tokenized.append(context) assert transform(tokenized, initial) == initial tokenized[-1] = attr.assoc(context, certain=True) assert transform(tokenized, initial) == ['105', 'Subpart:G', '2'] # Don't try to proceed if multiple contexts are present tokenized.append(tokens.Context(['105', 'Appendix:Q'], certain=True)) assert transform(tokenized, initial) == initial
def test_compress_context_interpretations(): tokenized = [ tokens.Context(['123', 'Interpretations']), tokens.Paragraph.make(section='12', paragraphs=['a', '2', 'iii']), tokens.Paragraph.make(is_interp=True, paragraphs=[None, '3', 'v']), tokens.Context([None, 'Appendix:R']), tokens.Paragraph.make(is_interp=True, paragraphs=[None, '5']) ] converted, _ = amdparser.compress_context(tokenized, []) assert converted == [ tokens.Paragraph.make(part='123', is_interp=True, section='12', paragraphs=['(a)(2)(iii)', '3', 'v']), # None because we are missing a layer tokens.Paragraph.make(part='123', is_interp=True, section='Appendix:R', paragraphs=[None, '5']) ]
def test_compress_context_simple(self): tokenized = [ tokens.Verb(tokens.Verb.PUT, active=True), # part 9876, subpart A tokens.Context(['9876', 'Subpart:A']), # section 12 tokens.Context([None, None, '12']), # 12(f)(4) tokens.Paragraph([None, None, None, 'f', '4']), # 12(f) tokens.Context([None, None, None, 'g']), # 12(g)(1) tokens.Paragraph([None, None, None, None, '1']), ] converted, final_ctx = compress_context(tokenized, []) self.assertEqual(converted, [ tokens.Verb(tokens.Verb.PUT, active=True), tokens.Paragraph(['9876', 'Subpart:A', '12', 'f', '4']), tokens.Paragraph(['9876', 'Subpart:A', '12', 'g', '1']) ]) self.assertEqual(['9876', 'Subpart:A', '12', 'g', '1'], final_ctx)
def test_example13(self): text = "h. Under Section 6363.36, add comments 36(a), 36(b) and " text += "36(d)." result = parse_text(text) self.assertEqual(result, [ tokens.Context(['6363', None, '36'], certain=True), tokens.Verb(tokens.Verb.POST, active=True), # We assume that lists of comments are not context tokens.TokenList([ tokens.Paragraph(is_interp=True, section='36', paragraph=p) for p in ('(a)', '(b)', '(d)') ]) ])