-
Notifications
You must be signed in to change notification settings - Fork 59
Move annotations from GATE to WebAnnotator format #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
210f81a
more annotations
whalebot-helmsman e4fac51
more annotations
whalebot-helmsman 2df7c24
more annotations
whalebot-helmsman b3956cc
correct ids
whalebot-helmsman 51ef652
correct ids
whalebot-helmsman f1e002c
does not copy wa-title attributes
whalebot-helmsman 807f3a6
verify conversion
whalebot-helmsman 57bc016
convert annotation
whalebot-helmsman 02aad41
write as html
whalebot-helmsman b7e1e17
move gate annotations to webannotator
whalebot-helmsman eb97aa5
tests for html tools
whalebot-helmsman e541806
pep8 style
whalebot-helmsman d42dcea
add program description
whalebot-helmsman 37b8728
pep8 style
whalebot-helmsman 80a6fb8
pep8 style
whalebot-helmsman dca5dd3
add program description
whalebot-helmsman 9e480d4
pep8 style
whalebot-helmsman c1a1175
ability to pass entities list to verify
whalebot-helmsman 5feb78a
look for annotations in WebAnnotator folder
whalebot-helmsman 6d59b83
pep8
whalebot-helmsman 2a7b013
test attribute removal for wa-title
whalebot-helmsman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import logging | ||
import argparse | ||
|
||
import webstruct.loaders | ||
from webstruct.webannotator import EntityColors, to_webannotator | ||
|
||
|
||
def main(): | ||
cmdline = argparse.ArgumentParser(description=('utility ' | ||
'to convert annotations ' | ||
'from GATE format to ' | ||
'WebAnnotator format')) | ||
cmdline.add_argument('--GATE', | ||
help='path to file annotated in GATE format', | ||
type=str, | ||
required=True) | ||
cmdline.add_argument('--sample', | ||
help=('path to file annotated in WebAnnotator format ' | ||
'for colors and entities transfer'), | ||
type=str, | ||
required=True) | ||
cmdline.add_argument('--WebAnnotator', | ||
help='path to result file in WebAnnotator format', | ||
type=str, | ||
required=True) | ||
cmdline.add_argument('--loglevel', | ||
help='logging level', | ||
type=str, | ||
default='INFO') | ||
args = cmdline.parse_args() | ||
|
||
logging.basicConfig(level=getattr(logging, args.loglevel.upper()), | ||
format=('%(asctime)s [%(levelname)s]' | ||
'%(pathname)s:%(lineno)d %(message)s')) | ||
with open(args.sample, 'rb') as sample_reader: | ||
colors = EntityColors.from_htmlbytes(sample_reader.read()) | ||
entities = [typ for typ in colors] | ||
|
||
logging.debug('Current entities %s', entities) | ||
logging.debug('Current colors %s', colors) | ||
|
||
gate = webstruct.loaders.GateLoader(known_entities=entities) | ||
tokenizer = webstruct.HtmlTokenizer(tagset=entities) | ||
with open(args.GATE, 'rb') as reader: | ||
data = reader.read() | ||
tree = gate.loadbytes(data) | ||
tokens, annotations = tokenizer.tokenize_single(tree) | ||
tree = to_webannotator(tree, entity_colors=colors) | ||
with open(args.WebAnnotator, 'wb') as writer: | ||
tree.write(writer, method='html', pretty_print=True) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import json | ||
import logging | ||
import argparse | ||
|
||
import webstruct.loaders | ||
import webstruct.webannotator | ||
|
||
DEFAULT_ENTITIES = [ | ||
'ORG', 'TEL', 'FAX', 'HOURS', | ||
'STREET', 'CITY', 'STATE', 'ZIPCODE', 'COUNTRY', | ||
'EMAIL', 'PER', 'FUNC', 'SUBJ' | ||
] | ||
|
||
|
||
def nodes_difference(l, r): | ||
if l.tag != r.tag: | ||
return {'tag': '"{0}" != "{1}"'.format(l.tag, r.tag)} | ||
|
||
l_attrib = [(k, l.attrib[k]) for k in l.attrib] | ||
l_attrib.sort(key=lambda x: x[0]) | ||
r_attrib = [(k, r.attrib[k]) for k in r.attrib] | ||
r_attrib.sort(key=lambda x: x[0]) | ||
|
||
idx = 0 | ||
while idx < len(l_attrib) and idx < len(r_attrib): | ||
l_attr = l_attrib[idx] | ||
r_attr = r_attrib[idx] | ||
idx = idx + 1 | ||
|
||
if l_attr != r_attr: | ||
return {'attributes': '"{0}" != "{1}"'.format(l_attr, r_attr)} | ||
|
||
if idx < len(l_attrib): | ||
return {'attributes': "{0} != None".format(l_attrib[idx])} | ||
|
||
if idx < len(r_attrib): | ||
return {'attributes': "None != {0}".format(r_attrib[idx])} | ||
|
||
l_text = '' | ||
if l.text: | ||
l.text = l.text.strip() | ||
|
||
r_text = '' | ||
if r.text: | ||
r.text = r.text.strip() | ||
|
||
if l_text != r_text: | ||
return {'text': "{0} != {1}".format(l_text, r_text)} | ||
|
||
l_tail = '' | ||
if l.tail: | ||
l.tail = l.tail.strip() | ||
|
||
r_tail = '' | ||
if r.tail: | ||
r.tail = r.tail.strip() | ||
|
||
if l_tail != r_tail: | ||
return {'tail': "{0} != {1}".format(l_tail, r_tail)} | ||
|
||
if len(l) != len(r): | ||
return {'children count': "{0} != {1}".format(len(l), len(r))} | ||
|
||
return None | ||
|
||
|
||
def node_path(node): | ||
ret = '' | ||
current = node | ||
while current is not None: | ||
parent = current.getparent() | ||
idx = 0 | ||
if parent: | ||
idx = parent.index(current) | ||
step = '{0}:{1}'.format(idx, current.tag) | ||
ret = step + '/' + ret | ||
current = parent | ||
|
||
return ret | ||
|
||
|
||
def tree_difference(l, r): | ||
stack = [(l, r)] | ||
while stack: | ||
l_node, r_node = stack.pop(0) | ||
diff = nodes_difference(l_node, r_node) | ||
|
||
if diff: | ||
return {"l": node_path(l_node), | ||
"r": node_path(r_node), | ||
"diff": diff} | ||
|
||
for idx, l_child in enumerate(l_node): | ||
stack.append((l_child, r_node[idx])) | ||
|
||
return None | ||
|
||
|
||
def main(): | ||
cmdline = argparse.ArgumentParser(description=('utility to verify ' | ||
'annotation conversion ' | ||
'from GATE format ' | ||
'to WebAnnotator format')) | ||
cmdline.add_argument('--GATE', | ||
help='path to file annotated in GATE format', | ||
type=str, | ||
required=True) | ||
cmdline.add_argument('--WebAnnotator', | ||
help='path to file annotated in WebAnnotator format', | ||
type=str, | ||
required=True) | ||
cmdline.add_argument('--entity', | ||
help='enitity type to verify against', | ||
type=str, | ||
action='append', | ||
required=False) | ||
cmdline.add_argument('--loglevel', | ||
help='logging level', | ||
type=str, | ||
default='INFO') | ||
args = cmdline.parse_args() | ||
|
||
logging.basicConfig(level=getattr(logging, args.loglevel.upper()), | ||
format=('%(asctime)s [%(levelname)s] ' | ||
'%(pathname)s:%(lineno)d %(message)s')) | ||
|
||
if args.entity: | ||
entities = args.entity | ||
else: | ||
entities = DEFAULT_ENTITIES | ||
|
||
logging.debug('Known entities %s', entities) | ||
|
||
gate = webstruct.loaders.GateLoader(known_entities=entities) | ||
wa = webstruct.loaders.WebAnnotatorLoader(known_entities=entities) | ||
|
||
tokenizer = webstruct.HtmlTokenizer(tagset=entities) | ||
with open(args.GATE, 'rb') as reader: | ||
data = reader.read() | ||
gate_tree = gate.loadbytes(data) | ||
gate_tokens, gate_annotations = tokenizer.tokenize_single(gate_tree) | ||
|
||
with open(args.WebAnnotator, 'rb') as reader: | ||
data = reader.read() | ||
wa_tree = wa.loadbytes(data) | ||
wa_tokens, wa_annotations = tokenizer.tokenize_single(wa_tree) | ||
|
||
is_diff = False | ||
tree_diff = tree_difference(gate_tree, wa_tree) | ||
if tree_diff: | ||
logging.error('tree differs %s', json.dumps(tree_diff)) | ||
is_diff = True | ||
|
||
annot_diff = list() | ||
for idx, (gate_a, wa_a) in enumerate(zip(gate_annotations, | ||
wa_annotations)): | ||
if gate_a == wa_a: | ||
continue | ||
|
||
annot_diff.append({'idx': idx, | ||
'gate_a': gate_a, | ||
'wa_a': wa_a}) | ||
|
||
if annot_diff: | ||
logging.error('annotation differs %s', json.dumps(annot_diff)) | ||
is_diff = True | ||
|
||
return is_diff is False | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import unittest | ||
|
||
import lxml | ||
|
||
import webstruct.annotation_verifier | ||
|
||
html_1 = '<span style="abc" class="span">aa</span>' | ||
html_2 = '<span style="abd" class="span">aa</span>' | ||
html_3 = '<span style="abd" class="span">aa<p>s</p></span>' | ||
html_3 = '<span style="abd" class="span">aa<p>s</p><p>ss</p></span>' | ||
|
||
|
||
class SomethingTest(unittest.TestCase): | ||
|
||
def test_is_node_equal_to_self(self): | ||
tree_1 = lxml.etree.fromstring(html_1) | ||
diff = webstruct.annotation_verifier.nodes_difference(tree_1, tree_1) | ||
self.assertIsNone(diff) | ||
|
||
def test_is_different_nodes_are_diffirent(self): | ||
tree_1 = lxml.etree.fromstring(html_1) | ||
tree_2 = lxml.etree.fromstring(html_2) | ||
diff = webstruct.annotation_verifier.nodes_difference(tree_1, tree_2) | ||
self.assertIsNotNone(diff) | ||
|
||
def test_is_tree_equal_to_self(self): | ||
tree_1 = lxml.etree.fromstring(html_3) | ||
diff = webstruct.annotation_verifier.tree_difference(tree_1, tree_1) | ||
self.assertIsNone(diff) | ||
|
||
def test_is_different_trees_are_diffirent(self): | ||
tree_1 = lxml.etree.fromstring(html_2) | ||
tree_2 = lxml.etree.fromstring(html_3) | ||
diff = webstruct.annotation_verifier.tree_difference(tree_1, tree_2) | ||
self.assertIsNotNone(diff) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docstring is no longer valid, and there is no need to create gate_loader.