Skip to content

Commit 9019c15

Browse files
committed
replaced brittle annotation getter with SDK implementation
1 parent c1edb9b commit 9019c15

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

ocr.py

+7-17
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,22 @@ def update(self, anno, mmif):
5151

5252
elif anno.at_type.shortname == "Paragraph":
5353
view = mmif.get_view_by_id(anno.parent)
54-
text_anno = view.get_annotation_by_id(anno.properties.get("document"))
54+
text_anno = mmif[anno.properties.get("document")]
5555
self.add_text_document(text_anno)
5656

5757
def add_bounding_box(self, anno, mmif: Mmif):
5858
timepoint_anno = None
5959
if "timePoint" in anno.properties:
60-
timepoint_anno = find_annotation(anno.get("timePoint"), mmif)
60+
timepoint_anno = mmif[anno.get("timePoint")]
6161

6262
else:
6363
for alignment_anns in mmif.get_alignments(AnnotationTypes.BoundingBox, AnnotationTypes.TimePoint).values():
6464
for alignment_ann in alignment_anns:
6565
if alignment_ann.get('source') == anno.id:
66-
timepoint_anno = find_annotation(alignment_ann.get('target'), mmif)
66+
timepoint_anno = mmif[alignment_ann.get('target')]
6767
break
6868
elif alignment_ann.get('target') == anno.id:
69-
timepoint_anno = find_annotation(alignment_ann.get('source'), mmif)
69+
timepoint_anno = mmif[alignment_ann.get('source')]
7070
break
7171
if timepoint_anno:
7272
self.add_timepoint(timepoint_anno, mmif, skip_if_view_has_frames=False)
@@ -90,7 +90,7 @@ def add_timeframe(self, anno, mmif):
9090
if "targets" in anno.properties:
9191
start_id, end_id = anno.properties.get("targets")[0], anno.properties.get("targets")[-1]
9292
anno_parent = mmif.get_view_by_id(anno.parent)
93-
start_anno, end_anno = anno_parent.get_annotation_by_id(start_id), anno_parent.get_annotation_by_id(end_id)
93+
start_anno, end_anno = mmif[start_id], mmif[end_id]
9494
start = convert_timepoint(mmif, start_anno, "frames")
9595
end = convert_timepoint(mmif, end_anno, "frames")
9696
start_secs = convert_timepoint(mmif, start_anno, "seconds")
@@ -126,16 +126,6 @@ def add_text_document(self, anno):
126126
text_val = re.sub(r'([\\\/\|\"\'])', r'\1 ', t)
127127
self.text = self.text + [text_val] if text_val not in self.text else self.text
128128

129-
def find_annotation(anno_id, mmif):
130-
if mmif.id_delimiter in anno_id:
131-
view_id, anno_id = anno_id.split(mmif.id_delimiter)
132-
view = mmif.get_view_by_id(view_id)
133-
for view in mmif.views:
134-
try:
135-
return view.get_annotation_by_id(anno_id)
136-
except KeyError:
137-
continue
138-
139129

140130
def get_ocr_frames(view, mmif):
141131
frames = {}
@@ -144,8 +134,8 @@ def get_ocr_frames(view, mmif):
144134
# If view contains alignments
145135
if full_alignment_type:
146136
for alignment in view.get_annotations(full_alignment_type[0]):
147-
source = find_annotation(alignment.get("source"), mmif)
148-
target = find_annotation(alignment.get("target"), mmif)
137+
source = mmif[alignment.get("source")]
138+
target = mmif[alignment.get("target")]
149139

150140
# Account for alignment in either direction
151141
frame = OCRFrame(source, mmif)

0 commit comments

Comments
 (0)