package transcribe import ( "regexp" "sort" "strings" ) // PauseThreshold is the silence, in seconds, that counts as a pause: long // enough to be noticed by an interviewer, short enough to catch hesitation. const PauseThreshold = 2.0 // Pause is a silence starting At seconds in. type Pause struct { At, Length float64 } // Filler is one filler word or phrase and how often it was said. type Filler struct { Word string Count int } // Metrics are the delivery numbers Claude cannot get from reading the // transcript: it never hears the recording, so pace and silence are // measured here from the timestamps. type Metrics struct { Duration float64 Words int WPM float64 Pauses []Pause // chronological Fillers []Filler // FillerCount is the sum of Fillers. FillerCount int // ComplexityAt is when time or memory complexity was first mentioned, // or -1 if it never was. ComplexityAt float64 } // Longest is the longest pause, or the zero Pause. func (m Metrics) Longest() Pause { var p Pause for _, q := range m.Pauses { if q.Length > p.Length { p = q } } return p } type token struct { text string start, end float64 } var notWord = regexp.MustCompile(`[^a-z0-9'()]+`) // tokens are the transcript's words, lowercased and stripped of punctuation. // A server that gives no word timings gets each word its segment's times, // which is still enough to place things to within a sentence. func (t *Transcript) tokens() []token { var out []token add := func(raw string, start, end float64) { for _, f := range strings.Fields(notWord.ReplaceAllString(strings.ToLower(raw), " ")) { out = append(out, token{f, start, end}) } } if len(t.Words) > 0 { for _, w := range t.Words { add(w.Word, w.Start, w.End) } return out } for _, s := range t.Segments { add(s.Text, s.Start, s.End) } if len(out) == 0 { add(t.Text, 0, t.Duration) } return out } // Whisper spells drawn-out fillers with repeated letters ("ummm", "uhh"). var fillerShapes = []struct { re *regexp.Regexp word string }{ {regexp.MustCompile(`^u+h*m+$`), "um"}, {regexp.MustCompile(`^u+h+$`), "uh"}, {regexp.MustCompile(`^e+r+m*$`), "er"}, {regexp.MustCompile(`^h+m+$`), "hmm"}, } // Single words that are usually filler in spoken explanation. "So" and // "right" are left out: they start too many real sentences to count. var fillerWords = map[string]bool{"like": true, "basically": true, "actually": true, "literally": true} var fillerPhrases = map[string]bool{"you know": true, "kind of": true, "sort of": true, "i mean": true} // Metrics measures t. func (t *Transcript) Metrics() Metrics { toks := t.tokens() m := Metrics{Duration: t.Duration, Words: len(toks), ComplexityAt: -1} if m.Duration > 0 { m.WPM = float64(m.Words) / (m.Duration / 60) } if len(t.Words) > 0 { for i := 1; i < len(t.Words); i++ { if gap := t.Words[i].Start - t.Words[i-1].End; gap >= PauseThreshold { m.Pauses = append(m.Pauses, Pause{t.Words[i-1].End, gap}) } } } else { for i := 1; i < len(t.Segments); i++ { if gap := t.Segments[i].Start - t.Segments[i-1].End; gap >= PauseThreshold { m.Pauses = append(m.Pauses, Pause{t.Segments[i-1].End, gap}) } } } counts := map[string]int{} for i, tk := range toks { next := "" if i+1 < len(toks) { next = toks[i+1].text } for _, f := range fillerShapes { if f.re.MatchString(tk.text) { counts[f.word]++ } } if fillerWords[tk.text] { counts[tk.text]++ } if pair := tk.text + " " + next; fillerPhrases[pair] { counts[pair]++ } if m.ComplexityAt < 0 && (tk.text == "complexity" || strings.HasPrefix(tk.text, "o(") || ((tk.text == "big" || tk.text == "o") && (next == "o" || next == "of"))) { m.ComplexityAt = tk.start } } for w, n := range counts { m.Fillers = append(m.Fillers, Filler{w, n}) m.FillerCount += n } sort.Slice(m.Fillers, func(i, j int) bool { a, b := m.Fillers[i], m.Fillers[j] return a.Count > b.Count || (a.Count == b.Count && a.Word < b.Word) }) return m }