Skip to content

Commit 8000d53

Browse files
Merge pull request #38 from clamsproject/37-fix-timeunit-vtt
more robust `timedelta` initiation for VTT generation
2 parents ed43dfa + d561af5 commit 8000d53

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

utils.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
from flask import Flask, url_for
66
from lapps.discriminators import Uri
7-
from mmif import DocumentTypes
87
from mmif.serialize.annotation import Text, Document
9-
from mmif.vocabulary import AnnotationTypes
8+
from mmif.utils.timeunit_helper import UNIT_NORMALIZATION
109

1110
import displacy
1211
import iiif_utils
@@ -19,6 +18,13 @@
1918
app.secret_key = 'your_secret_key_here'
2019

2120

21+
def normalize_timeunit(tu_str):
22+
if tu_str in UNIT_NORMALIZATION:
23+
return UNIT_NORMALIZATION[tu_str]
24+
else:
25+
return tu_str
26+
27+
2228
def asr_alignments_to_vtt(alignment_view, viz_id):
2329
vtt_filename = cache.get_cache_root() / viz_id / f"{alignment_view.id.replace(':', '-')}.vtt"
2430
if vtt_filename.exists():
@@ -27,7 +33,10 @@ def asr_alignments_to_vtt(alignment_view, viz_id):
2733
vtt_file.write("WEBVTT\n\n")
2834
annotations = alignment_view.annotations
2935
timeframe_at_type = [at_type for at_type in alignment_view.metadata.contains if at_type.shortname == "TimeFrame"][0]
30-
timeunit = alignment_view.metadata.contains[timeframe_at_type]["timeUnit"]
36+
timeunit = normalize_timeunit(alignment_view.metadata.contains[timeframe_at_type]["timeUnit"])
37+
# make plural so that this key can be used in timedelta init
38+
if timeunit[-1] != 's':
39+
timeunit += 's'
3140
# TODO: wanted to use "mmif.get_alignments(AnnotationTypes.TimeFrame, Uri.TOKEN)"
3241
# but that gave errors so I gave up on it
3342
token_idx = {a.id: a for a in annotations if a.at_type.shortname == "Token"}

0 commit comments

Comments
 (0)