Module: Cohere::Transcribe::Output::Rendering

Defined in:
lib/cohere/transcribe/output/rendering.rb

Constant Summary collapse

SENTENCE_ENDINGS =
[".", "!", "?", "؟", "،", "؛", ""].freeze

Class Method Summary collapse

Class Method Details

.build_cues(words, max_chars:, max_duration:, max_gap:, min_cue_duration: 0.30, media_duration: nil) ⇒ Object



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
# File 'lib/cohere/transcribe/output/rendering.rb', line 16

def build_cues(words, max_chars:, max_duration:, max_gap:, min_cue_duration: 0.30, media_duration: nil)
  cue_words = []
  current = []
  words.each do |word|
    if current.any?
      candidate = (current.map { |item| field(item, :text) } + [field(word, :text)]).join(" ")
      gap = field(word, :start) - field(current.last, :end)
      duration = field(word, :end) - field(current.first, :start)
      if candidate.length > max_chars || duration > max_duration || gap > max_gap
        cue_words << current
        current = []
      end
    end
    current << word
    if SENTENCE_ENDINGS.any? { |ending| field(word, :text).end_with?(ending) }
      cue_words << current
      current = []
    end
  end
  cue_words << current if current.any?

  cues = cue_words.map do |items|
    SubtitleCue.new(
      start: [0.0, field(items.first, :start)].max,
      end: [field(items.first, :start), field(items.last, :end)].max,
      text: PythonText.strip(items.map { |item| field(item, :text) }.join(" "))
    )
  end
  cues.each_with_index.map do |cue, index|
    start_time = media_duration ? [cue.start, media_duration].min : cue.start
    end_time = media_duration ? [cue.end, media_duration].min.clamp(start_time, media_duration) : cue.end
    next_start = index + 1 < cues.length ? cues[index + 1].start : Float::INFINITY
    upper_bound = [next_start, media_duration || Float::INFINITY].min
    desired_end = [end_time, start_time + min_cue_duration].max
    cue.with(start: start_time, end: [[desired_end, upper_bound].min, start_time].max)
  end.freeze
end

.json(payload) ⇒ Object



90
91
92
# File 'lib/cohere/transcribe/output/rendering.rb', line 90

def json(payload)
  "#{JSON.pretty_generate(payload)}\n"
end

.plain_text(lines) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/cohere/transcribe/output/rendering.rb', line 68

def plain_text(lines)
  normalized = lines.filter_map do |line|
    text = PythonText.strip(line.to_s)
    text unless text.empty?
  end
  "#{normalized.join("\n")}\n"
end

.srt(cues) ⇒ Object



76
77
78
79
80
81
# File 'lib/cohere/transcribe/output/rendering.rb', line 76

def srt(cues)
  cues.each_with_index.map do |cue, index|
    "#{index + 1}\n#{timestamp(field(cue, :start), include_hours: true, marker: ",")} --> " \
      "#{timestamp(field(cue, :end), include_hours: true, marker: ",")}\n#{field(cue, :text)}\n\n"
  end.join
end

.timestamp(seconds, include_hours: false, marker: ".") ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cohere/transcribe/output/rendering.rb', line 54

def timestamp(seconds, include_hours: false, marker: ".")
  milliseconds = ([0.0, seconds].max * 1000).round(half: :even)
  hours, milliseconds = milliseconds.divmod(3_600_000)
  minutes, milliseconds = milliseconds.divmod(60_000)
  whole_seconds, milliseconds = milliseconds.divmod(1000)
  if include_hours || hours.positive?
    format("%<hours>02d:%<minutes>02d:%<seconds>02d%<marker>s%<milliseconds>03d",
           hours: hours, minutes: minutes, seconds: whole_seconds, marker: marker, milliseconds: milliseconds)
  else
    format("%<minutes>02d:%<seconds>02d%<marker>s%<milliseconds>03d",
           minutes: minutes, seconds: whole_seconds, marker: marker, milliseconds: milliseconds)
  end
end

.vtt(cues) ⇒ Object



83
84
85
86
87
88
# File 'lib/cohere/transcribe/output/rendering.rb', line 83

def vtt(cues)
  body = cues.map do |cue|
    "#{timestamp(field(cue, :start))} --> #{timestamp(field(cue, :end))}\n#{field(cue, :text)}\n\n"
  end.join
  "WEBVTT\n\n#{body}"
end