Skip to content

Commit 2e0a9d1

Browse files
feat: Caption enhancements - punctuation and testing (#97)
* Fix caption test - UserWarning for no utterances * Use punctuated_word for caption if it exists * WebVTT file starts with first line 'WEBVTT'
1 parent b3b221b commit 2e0a9d1

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

deepgram/extra.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@ def _to_caption(
5050
utterances = response["results"]["channels"][0]["alternatives"]
5151
captions = []
5252
line_counter = 1
53+
if format is Caption.WEBVTT:
54+
captions.append("WEBVTT")
5355
for utt_index, utt in enumerate(utterances):
5456
words = utterances[utt_index]["words"]
57+
word_text = "punctuated_word" if "punctuated_word" in words[0] else "word"
5558
for i in range(0, len(words), line_length):
5659
start_time = words[i]["start"]
5760
end_index = min(len(words) - 1, i + line_length - 1)
5861
end_time = words[end_index]["end"]
59-
text = " ".join([w["word"] for w in words[i:end_index + 1]])
62+
text = " ".join([w[word_text] for w in words[i:end_index + 1]])
6063
separator = "," if format is Caption.SRT else '.'
6164
prefix = "" if format is Caption.SRT else "- "
6265
caption = (

tests/test_extra.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
deepgram = Deepgram(api_key)
1313

1414
MOCK_SRT = "1\n00:00:05,519 --> 00:00:06,019\nYep.\n\n2\n00:00:07,094 --> 00:00:08,615\nI said it before, and I'll say it\n\n3\n00:00:08,615 --> 00:00:09,115\nagain.\n\n4\n00:00:09,974 --> 00:00:11,514\nLife moves pretty fast.\n\n5\n00:00:11,974 --> 00:00:13,654\nYou don't stop and look around once in\n\n6\n00:00:13,654 --> 00:00:15,701\na while. you could miss it."
15-
MOCK_WEBVTT = "1\n00:00:05.519 --> 00:00:06.019\n- Yep.\n\n2\n00:00:07.094 --> 00:00:08.615\n- I said it before, and I'll say it\n\n3\n00:00:08.615 --> 00:00:09.115\n- again.\n\n4\n00:00:09.974 --> 00:00:11.514\n- Life moves pretty fast.\n\n5\n00:00:11.974 --> 00:00:13.654\n- You don't stop and look around once in\n\n6\n00:00:13.654 --> 00:00:15.701\n- a while. you could miss it."
15+
MOCK_WEBVTT = "WEBVTT\n\n1\n00:00:05.519 --> 00:00:06.019\n- Yep.\n\n2\n00:00:07.094 --> 00:00:08.615\n- I said it before, and I'll say it\n\n3\n00:00:08.615 --> 00:00:09.115\n- again.\n\n4\n00:00:09.974 --> 00:00:11.514\n- Life moves pretty fast.\n\n5\n00:00:11.974 --> 00:00:13.654\n- You don't stop and look around once in\n\n6\n00:00:13.654 --> 00:00:15.701\n- a while. you could miss it."
1616

1717
"""
1818
Happy case of captions in SRT format.
@@ -32,5 +32,5 @@ def test_get_WebVTT():
3232
def test_get_SRT_no_utterances():
3333
response_no_utts = MOCK_RESPONSE
3434
del response_no_utts["results"]["utterances"]
35-
with pytest.raises(AssertionError):
35+
with pytest.warns(UserWarning):
3636
deepgram.extra.to_SRT(response_no_utts)

0 commit comments

Comments
 (0)