File size: 3,679 Bytes
3874369
 
18e68a0
 
 
 
3874369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18e68a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3874369
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import unittest

from subtitle_utils import (
    Cue, TimedWord, build_cues, chunks_to_words, timestamp_chunk_coverage,
    timed_words_to_text, to_json, to_srt, to_vtt, transcript_coverage,
)


class SubtitleUtilsTests(unittest.TestCase):
    def test_word_chunks_generate_readable_srt_vtt(self):
        chunks = [
            {"text": " Hello", "timestamp": (0.1, 0.4)},
            {"text": " there.", "timestamp": (0.45, 0.8)},
            {"text": " How", "timestamp": (1.1, 1.3)},
            {"text": " are", "timestamp": (1.3, 1.5)},
            {"text": " you?", "timestamp": (1.5, 1.9)},
        ]
        words = chunks_to_words(chunks, "Hello there. How are you?", 2.0, 42)
        cues = build_cues(words, max_chars=42)
        self.assertTrue(cues)
        self.assertIn("Hello there.", to_srt(cues))
        self.assertTrue(to_vtt(cues).startswith("WEBVTT"))
        self.assertIn('"text"', to_json(cues))

    def test_segment_chunk_is_split_for_caption_width(self):
        chunks = [{"text": " This is a longer segment that should wrap into readable subtitle lines.", "timestamp": (0.0, 4.0)}]
        words = chunks_to_words(chunks, "This is a longer segment that should wrap into readable subtitle lines.", 4.0, 28)
        cues = build_cues(words, max_chars=28)
        self.assertTrue(cues)
        for cue in cues:
            self.assertLessEqual(len(cue.text.splitlines()), 2)

    def test_missing_chunk_timestamps_falls_back_to_full_text(self):
        words = chunks_to_words([], "A small test transcript", 3.0, 42)
        self.assertTrue(words)
        cues = build_cues(words)
        self.assertTrue(cues)


    def test_very_long_token_is_not_dropped(self):
        long_token = "x" * 150
        chunks = [{"text": long_token, "timestamp": (0.0, 3.0)}]
        words = chunks_to_words(chunks, long_token, 3.0, 42)
        cues = build_cues(words, max_chars=42)
        recovered = "".join(cue.text.replace("\n", "") for cue in cues)
        self.assertEqual(recovered, long_token)

    def test_srt_timestamp_format(self):
        srt = to_srt([Cue(1.234, 62.5, "Timing")])
        self.assertIn("00:00:01,234 --> 00:01:02,500", srt)

    def test_missing_timestamp_chunks_are_not_dropped(self):
        chunks = [
            {"text": " Hello", "timestamp": (0.0, 0.4)},
            {"text": " missing", "timestamp": (None, None)},
            {"text": " timestamp", "timestamp": (None, None)},
            {"text": " preserved.", "timestamp": (1.1, 1.6)},
        ]
        words = chunks_to_words(chunks, "Hello missing timestamp preserved.", 2.0, 42)
        rebuilt = timed_words_to_text(words)
        self.assertEqual(transcript_coverage("Hello missing timestamp preserved.", rebuilt), 1.0)
        self.assertEqual(timestamp_chunk_coverage(chunks), 0.5)

    def test_partial_chunk_text_is_detectable_for_retry(self):
        chunks = [
            {"text": " This part is present", "timestamp": (0.0, 1.5)},
        ]
        words = chunks_to_words(chunks, "This part is present but this sentence is missing", 4.0, 42)
        rebuilt = timed_words_to_text(words)
        self.assertLess(transcript_coverage("This part is present but this sentence is missing", rebuilt), 0.985)

    def test_all_text_without_timestamps_is_preserved(self):
        chunks = [
            {"text": " One chunk", "timestamp": (None, None)},
            {"text": " another chunk.", "timestamp": (None, None)},
        ]
        words = chunks_to_words(chunks, "One chunk another chunk.", 3.0, 42)
        self.assertEqual(transcript_coverage("One chunk another chunk.", timed_words_to_text(words)), 1.0)


if __name__ == "__main__":
    unittest.main()