@@ -31,9 +31,9 @@ def get_alignments(alignment_view):
31
31
annotations = alignment_view .annotations
32
32
# TODO: wanted to use "mmif.get_alignments(AnnotationTypes.TimeFrame, Uri.TOKEN)"
33
33
# but that gave errors so I gave up on it
34
- token_idx = {a .id :a for a in annotations if "Token" in str ( a .at_type ) }
35
- timeframe_idx = {a .id :a for a in annotations if "TimeFrame" in str ( a .at_type ) }
36
- alignments = [a for a in annotations if "Alignment" in str ( a .at_type ) ]
34
+ token_idx = {a .id :a for a in annotations if a .at_type . shortname == "Token" }
35
+ timeframe_idx = {a .id :a for a in annotations if a .at_type . shortname == "TimeFrame" }
36
+ alignments = [a for a in annotations if a .at_type . shortname == "Alignment" ]
37
37
vtt_start = None
38
38
texts = []
39
39
for alignment in alignments :
@@ -46,10 +46,10 @@ def get_alignments(alignment_view):
46
46
# Assuming here that start and end are in miliseconds
47
47
start , end , text = start_end_text
48
48
if not vtt_start :
49
- vtt_start = f'{ start // 60000 :02d} :{ start % 60000 // 1000 } .{ start % 1000 :03d} '
49
+ vtt_start = f'{ start // 60000 :02d} :{ start % 60000 // 1000 :02d } .{ start % 1000 :03d} '
50
50
texts .append (text )
51
51
if len (texts ) > 8 :
52
- vtt_end = f'{ end // 60000 :02d} :{ end % 60000 // 1000 } .{ end % 1000 :03d} '
52
+ vtt_end = f'{ end // 60000 :02d} :{ end % 60000 // 1000 :02d } .{ end % 1000 :03d} '
53
53
vtt_file .write (f'{ vtt_start } --> { vtt_end } \n { " " .join (texts )} \n \n ' )
54
54
vtt_start = None
55
55
texts = []
@@ -120,12 +120,9 @@ def get_boxes(mmif):
120
120
def get_document_type_short_form (document ):
121
121
"""Returns 'Video', 'Text', 'Audio' or 'Image' from the document type of
122
122
the document."""
123
- # Remove trailing "/v1" in newer @types
124
- at_type = re .sub (r"\/v\d*$" , "" , str (document .at_type ))
125
- document_type = os .path .split (at_type )[1 ]
123
+ document_type = document .at_type .shortname
126
124
return document_type [:- 8 ]
127
125
128
-
129
126
def prep_annotations (mmif ):
130
127
"""Prepare annotations from the views, and return a list of pairs of tabname
131
128
and tab content. The first tab is alway the full MMIF pretty print."""
@@ -170,7 +167,7 @@ def get_video_path(mmif):
170
167
def create_info (mmif ):
171
168
s = StringIO ('Howdy' )
172
169
for document in mmif .documents :
173
- at_type = str ( document .at_type ). rsplit ( '/' , 1 )[ - 1 ]
170
+ at_type = document .at_type . shortname
174
171
location = document .location
175
172
s .write ("%s %s\n " % (at_type , location ))
176
173
s .write ('\n ' )
@@ -180,7 +177,7 @@ def create_info(mmif):
180
177
s .write ('%s %s %s %d\n ' % (view .id , app , status , len (view .annotations )))
181
178
if len (view .annotations ) > 0 :
182
179
s .write ('\n ' )
183
- types = Counter ([str ( a .at_type ). rsplit ( '/' , 1 )[ - 1 ]
180
+ types = Counter ([a .at_type . shortname
184
181
for a in view .annotations ])
185
182
for attype , count in types .items ():
186
183
s .write (' %4d %s\n ' % (count , attype ))
@@ -200,7 +197,7 @@ def create_annotation_tables(mmif):
200
197
for annotation in view .annotations :
201
198
s .write (' <tr>\n ' )
202
199
s .write (' <td>%s</td>\n ' % annotation .id )
203
- s .write (' <td>%s</td>\n ' % str ( annotation .at_type ). split ( '/' )[ - 1 ] )
200
+ s .write (' <td>%s</td>\n ' % annotation .at_type . shortname )
204
201
s .write (' <td>%s</td>\n ' % limit_len (get_properties (annotation )))
205
202
s .write (' </tr>\n ' )
206
203
s .write ("</table>\n " )
@@ -212,7 +209,7 @@ def get_document_ids(view, annotation_type):
212
209
metadata = view .metadata .contains .get (annotation_type )
213
210
ids = set ([metadata ['document' ]]) if 'document' in metadata else set ()
214
211
for annotation in view .annotations :
215
- if str ( annotation_type ) in str (annotation . at_type ):
212
+ if annotation . at_type . shortname == str (annotation_type ):
216
213
try :
217
214
ids .add (annotation .properties ["document" ])
218
215
except KeyError :
@@ -298,20 +295,20 @@ def get_aligned_views(mmif):
298
295
"""Return list of properly aligned views (for tree display)"""
299
296
aligned_views = []
300
297
for view in mmif .views :
301
- if any (["Alignment" in str ( at_type ) for at_type in view .metadata .contains ]):
298
+ if any ([at_type . shortname == "Token" for at_type in view .metadata .contains ]):
302
299
if check_view_alignment (view .annotations ) == True :
303
300
aligned_views .append (view .id )
304
301
return aligned_views
305
302
306
303
def check_view_alignment (annotations ):
307
304
anno_stack = []
308
305
for annotation in annotations :
309
- if "Alignment" in str ( annotation .at_type ) :
306
+ if annotation .at_type . shortname == "Alignment" :
310
307
anno_stack .insert (0 , annotation .properties )
311
308
else :
312
309
anno_stack .append (annotation .id )
313
310
if len (anno_stack ) == 3 :
314
- if not (anno_stack [0 ]["source" ] in anno_stack and anno_stack [0 ]["target" ] in anno_stack ):
311
+ if type ( anno_stack [ 0 ] == str ) or not (anno_stack [0 ]["source" ] in anno_stack and anno_stack [0 ]["target" ] in anno_stack ):
315
312
return False
316
313
anno_stack = []
317
314
return True
@@ -357,17 +354,17 @@ def prepare_ocr_visualization(mmif, view):
357
354
frames , text_docs , alignments = {}, {}, {}
358
355
for anno in view .annotations :
359
356
try :
360
- if "BoundingBox" in str ( anno .at_type ) :
357
+ if anno .at_type . shortname == "BoundingBox" :
361
358
frames = add_bounding_box (anno , frames )
362
359
363
- elif "TextDocument" in str ( anno .at_type ) :
360
+ elif anno .at_type . shortname == "TextDocument" :
364
361
t = anno .properties ["text_value" ]
365
362
if t :
366
363
text_id = anno .properties ["id" ]
367
364
# Format string so it is JSON-readable
368
365
text_docs [text_id ] = re .sub (r'([\\\/\|\"\'])' , r'\1 ' , t )
369
366
370
- elif "Alignment" in str ( anno .at_type ) :
367
+ elif anno .at_type . shortname == "Alignment" :
371
368
source = anno .properties ["source" ]
372
369
target = anno .properties ["target" ]
373
370
alignments [source ] = target
0 commit comments