Files
usaco/site/internal/transcribe/metrics.go
T
adamfandClaude Opus 5.5 63608f130c
Test and publish the site / test (push) Successful in 2m4s
Test and publish the site / publish (push) Successful in 1m44s
Review recorded explanations of a solution on the site
The admin can upload a recording of themselves explaining a problem's
solution out loud (mp3, m4a, wav, webm, ogg or flac, up to 25 MB) and
get interview-style feedback, committed as reviews/<id>.md so every
attempt is kept.

Claude can't take audio, so the recording is transcribed first by an
OpenAI-compatible endpoint (whisper-1 by default; TRANSCRIBE_API_KEY,
with TRANSCRIBE_URL for a self-hosted server). Pace, pauses, filler
words and when complexity first comes up are measured from the word
timestamps and given to Claude with the timestamped transcript, the
statement and optionally the code. The reply covers the verdict, scores
in five areas, the approach and its complexity, missed edge cases,
delivery and what to work on.

Recordings are kept for playback in an S3 bucket (Garage) when the
S3_* settings are given, and are thrown away otherwise. Reviews are
admin-only. The job runs in the background like Explain, and the
statement fetch and save-error handling are now shared between the two.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-20 01:56:55 +01:00

154 lines
4.0 KiB
Go

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
}