|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import argparse |
| 4 | + |
| 5 | +import webstruct.loaders |
| 6 | +import webstruct.webannotator |
| 7 | + |
| 8 | +DEFAULT_ENTITIES = [ |
| 9 | + 'ORG', 'TEL', 'FAX', 'HOURS', |
| 10 | + 'STREET', 'CITY', 'STATE', 'ZIPCODE', 'COUNTRY', |
| 11 | + 'EMAIL', 'PER', 'FUNC', 'SUBJ' |
| 12 | +] |
| 13 | + |
| 14 | + |
| 15 | +def nodes_difference(l, r): |
| 16 | + if l.tag != r.tag: |
| 17 | + return {'tag': '"{0}" != "{1}"'.format(l.tag, r.tag)} |
| 18 | + |
| 19 | + l_attrib = [(k, l.attrib[k]) for k in l.attrib] |
| 20 | + l_attrib.sort(key=lambda x: x[0]) |
| 21 | + r_attrib = [(k, r.attrib[k]) for k in r.attrib] |
| 22 | + r_attrib.sort(key=lambda x: x[0]) |
| 23 | + |
| 24 | + idx = 0 |
| 25 | + while idx < len(l_attrib) and idx < len(r_attrib): |
| 26 | + l_attr = l_attrib[idx] |
| 27 | + r_attr = r_attrib[idx] |
| 28 | + idx = idx + 1 |
| 29 | + |
| 30 | + if l_attr != r_attr: |
| 31 | + return {'attributes': '"{0}" != "{1}"'.format(l_attr, r_attr)} |
| 32 | + |
| 33 | + if idx < len(l_attrib): |
| 34 | + return {'attributes': "{0} != None".format(l_attrib[idx])} |
| 35 | + |
| 36 | + if idx < len(r_attrib): |
| 37 | + return {'attributes': "None != {0}".format(r_attrib[idx])} |
| 38 | + |
| 39 | + l_text = '' |
| 40 | + if l.text: |
| 41 | + l.text = l.text.strip() |
| 42 | + |
| 43 | + r_text = '' |
| 44 | + if r.text: |
| 45 | + r.text = r.text.strip() |
| 46 | + |
| 47 | + if l_text != r_text: |
| 48 | + return {'text': "{0} != {1}".format(l_text, r_text)} |
| 49 | + |
| 50 | + l_tail = '' |
| 51 | + if l.tail: |
| 52 | + l.tail = l.tail.strip() |
| 53 | + |
| 54 | + r_tail = '' |
| 55 | + if r.tail: |
| 56 | + r.tail = r.tail.strip() |
| 57 | + |
| 58 | + if l_tail != r_tail: |
| 59 | + return {'tail': "{0} != {1}".format(l_tail, r_tail)} |
| 60 | + |
| 61 | + if len(l) != len(r): |
| 62 | + return {'children count': "{0} != {1}".format(len(l), len(r))} |
| 63 | + |
| 64 | + return None |
| 65 | + |
| 66 | + |
| 67 | +def node_path(node): |
| 68 | + ret = '' |
| 69 | + current = node |
| 70 | + while current is not None: |
| 71 | + parent = current.getparent() |
| 72 | + idx = 0 |
| 73 | + if parent: |
| 74 | + idx = parent.index(current) |
| 75 | + step = '{0}:{1}'.format(idx, current.tag) |
| 76 | + ret = step + '/' + ret |
| 77 | + current = parent |
| 78 | + |
| 79 | + return ret |
| 80 | + |
| 81 | + |
| 82 | +def tree_difference(l, r): |
| 83 | + stack = [(l, r)] |
| 84 | + while stack: |
| 85 | + l_node, r_node = stack.pop(0) |
| 86 | + diff = nodes_difference(l_node, r_node) |
| 87 | + |
| 88 | + if diff: |
| 89 | + return {"l": node_path(l_node), |
| 90 | + "r": node_path(r_node), |
| 91 | + "diff": diff} |
| 92 | + |
| 93 | + for idx, l_child in enumerate(l_node): |
| 94 | + stack.append((l_child, r_node[idx])) |
| 95 | + |
| 96 | + return None |
| 97 | + |
| 98 | + |
| 99 | +def main(): |
| 100 | + cmdline = argparse.ArgumentParser(description=('utility to verify ' |
| 101 | + 'annotation conversion ' |
| 102 | + 'from GATE format ' |
| 103 | + 'to WebAnnotator format')) |
| 104 | + cmdline.add_argument('--GATE', |
| 105 | + help='path to file annotated in GATE format', |
| 106 | + type=str, |
| 107 | + required=True) |
| 108 | + cmdline.add_argument('--WebAnnotator', |
| 109 | + help='path to file annotated in WebAnnotator format', |
| 110 | + type=str, |
| 111 | + required=True) |
| 112 | + cmdline.add_argument('--entity', |
| 113 | + help='enitity type to verify against', |
| 114 | + type=str, |
| 115 | + action='append', |
| 116 | + required=False) |
| 117 | + cmdline.add_argument('--loglevel', |
| 118 | + help='logging level', |
| 119 | + type=str, |
| 120 | + default='INFO') |
| 121 | + args = cmdline.parse_args() |
| 122 | + |
| 123 | + logging.basicConfig(level=getattr(logging, args.loglevel.upper()), |
| 124 | + format=('%(asctime)s [%(levelname)s] ' |
| 125 | + '%(pathname)s:%(lineno)d %(message)s')) |
| 126 | + |
| 127 | + if args.entity: |
| 128 | + entities = args.entity |
| 129 | + else: |
| 130 | + entities = DEFAULT_ENTITIES |
| 131 | + |
| 132 | + logging.debug('Known entities %s', entities) |
| 133 | + |
| 134 | + gate = webstruct.loaders.GateLoader(known_entities=entities) |
| 135 | + wa = webstruct.loaders.WebAnnotatorLoader(known_entities=entities) |
| 136 | + |
| 137 | + tokenizer = webstruct.HtmlTokenizer(tagset=entities) |
| 138 | + with open(args.GATE, 'rb') as reader: |
| 139 | + data = reader.read() |
| 140 | + gate_tree = gate.loadbytes(data) |
| 141 | + gate_tokens, gate_annotations = tokenizer.tokenize_single(gate_tree) |
| 142 | + |
| 143 | + with open(args.WebAnnotator, 'rb') as reader: |
| 144 | + data = reader.read() |
| 145 | + wa_tree = wa.loadbytes(data) |
| 146 | + wa_tokens, wa_annotations = tokenizer.tokenize_single(wa_tree) |
| 147 | + |
| 148 | + is_diff = False |
| 149 | + tree_diff = tree_difference(gate_tree, wa_tree) |
| 150 | + if tree_diff: |
| 151 | + logging.error('tree differs %s', json.dumps(tree_diff)) |
| 152 | + is_diff = True |
| 153 | + |
| 154 | + annot_diff = list() |
| 155 | + for idx, (gate_a, wa_a) in enumerate(zip(gate_annotations, |
| 156 | + wa_annotations)): |
| 157 | + if gate_a == wa_a: |
| 158 | + continue |
| 159 | + |
| 160 | + annot_diff.append({'idx': idx, |
| 161 | + 'gate_a': gate_a, |
| 162 | + 'wa_a': wa_a}) |
| 163 | + |
| 164 | + if annot_diff: |
| 165 | + logging.error('annotation differs %s', json.dumps(annot_diff)) |
| 166 | + is_diff = True |
| 167 | + |
| 168 | + return is_diff is False |
| 169 | + |
| 170 | +if __name__ == "__main__": |
| 171 | + main() |
0 commit comments