Ejemplo n.º 1
0
    def test_encode(self):
        n1 = struct.Node('texttext', [struct.Node(node_type='t')],
                         ['1', '2', '3'])
        n2 = struct.Node(node_type='someType', title='Some Title')

        enc = struct.NodeEncoder(sort_keys=True)
        self.assertEqual(
            enc.encode(n1),
            enc.encode({
                'node_type':
                struct.Node.REGTEXT,
                'text':
                'texttext',
                'children': [{
                    'node_type': 't',
                    'text': '',
                    'children': [],
                    'label': []
                }],
                'label': ['1', '2', '3']
            }))
        self.assertEqual(
            enc.encode(n2),
            enc.encode({
                'node_type': 'someType',
                'text': '',
                'children': [],
                'label': [],
                'title': 'Some Title'
            }))
Ejemplo n.º 2
0
            # include the period
            next_char = node_text[first_p + 1:first_p + 2]
            if next_char in (')', u'”'):
                first_sentence = node_text[:first_p + 2]
            else:
                first_sentence = node_text[:first_p + 1]
        else:
            first_sentence = node_text

        # Key terms can't be the entire text of a leaf node
        if first_sentence == node_text and not node.children:
            return

        words = first_sentence.split()
        if (not words[-1] == part_end and not first_sentence.startswith('![')):
            num_words = len(words)

            # key terms are short
            if num_words <= 15:
                layer_element = {"key_term": first_sentence, "locations": [0]}
                layer[label_id] = [layer_element]


if __name__ == "__main__":
    # Use the plain text based JSON for the regulation.
    tree = api_stub.get_regulation_as_json(
        '/vagrant/data/stub-server/regulation/1005/2013-10604-eregs')
    struct.walk(tree, generate_keyterm)

    print struct.NodeEncoder().encode(layer)
Ejemplo n.º 3
0
 def as_json(self):
     """ Write out the changes. """
     return struct.NodeEncoder().encode(self.changes)