Files
hpr-intro-stripper/hpr_stripper/markers.py
T
Lee Hanken 9917320640 hpr-intro-stripper: layered intro/outro cut-point detection and trimming
Fetches HPR's own metadata, audio, and official transcripts live; falls
back to local speech recognition (tiny model, matching HPR's own
hpr-get-and-transcribe.bash tooling) only when needed, and to
correlation against vendored reference clips or sibling episodes
otherwise. License-aware, resumable, and never guesses -- flags
anything it isn't confident about for manual review rather than
producing a plausible-looking wrong cut.
2026-11-22 10:36:07 +00:00

99 lines
3.9 KiB
Python

"""
Fuzzy phrase matching against the boilerplate marker library (markers.json).
Two real false-positive bugs were found and fixed while building the
original prototype this is ported from -- both preserved here as the
reasons behind the design, not just comments:
1. A fuzzy fallback with too loose a threshold matched a host's own
sign-off ("...Hacker Public Radio signing off...") against the short
marker "hacker public radio does" purely on word overlap, nearly
truncating the host's actual last sentence. Fixed by requiring markers
to have at least MIN_WORDS_FOR_FUZZY words before the fuzzy fallback
applies at all, and by using longer, more distinctive literal phrases
for the outro markers specifically.
2. A host's own organic cold open (hpr0500: "...this is Klaatu and this is
Hacker Public Radio...") was briefly misread as the start of the
auto-generated TTS announcement, because the generic "welcome to HPR"
greeting line is not actually proof of station boilerplate -- hosts say
it unprompted too. Fixed by splitting markers into "strong" (exact
substring only, no fuzzy fallback at all -- specific enough that a host
could not plausibly say them by coincidence) vs. "greeting" (can extend
an already-established boilerplate run, but can never be the sole
evidence that one exists).
"""
import json
import os
import re
from difflib import SequenceMatcher
_MARKERS_PATH = os.path.join(os.path.dirname(__file__), "markers.json")
def load_markers():
with open(_MARKERS_PATH) as f:
return json.load(f)
def normalize(text):
text = text.lower()
text = text.replace("'", "") # "today's" -> "todays", not "today s"
text = re.sub(r"[^a-z0-9 ]+", " ", text)
return re.sub(r"\s+", " ", text).strip()
def fuzzy_contains(markers, text, ratio_threshold=0.82, min_words_for_fuzzy=5):
"""Substring containment first; the fuzzy ratio fallback only kicks in
for markers with enough words that a short, generic overlap can't score
high by accident (see module docstring, bug #1)."""
norm = normalize(text)
for marker in markers:
if marker in norm:
return True
mwords = marker.split()
if len(mwords) < min_words_for_fuzzy:
continue
words = norm.split()
n = len(mwords)
if len(words) < n:
continue
for i in range(len(words) - n + 1):
window = " ".join(words[i:i + n])
if SequenceMatcher(None, window, marker).ratio() >= ratio_threshold:
return True
return False
def find_intro_end(segments, markers=None):
"""segments: list of (text, start, end) from srt.parse(). Returns the
end-time of the last boilerplate segment, or None if no STRONG marker
was ever matched (see module docstring, bug #2) -- a run made only of
greeting-marker matches is never trusted on its own."""
markers = markers or load_markers()
strong = markers["strong_intro_markers"]
greeting = markers["greeting_intro_markers"]
last_end = None
saw_strong = False
for text, _start, end in segments:
is_strong = fuzzy_contains(strong, text, min_words_for_fuzzy=100) # exact-substring only
is_greeting = fuzzy_contains(greeting, text, min_words_for_fuzzy=3)
if is_strong or is_greeting:
last_end = end
saw_strong = saw_strong or is_strong
else:
break
return last_end if saw_strong else None
def find_outro_start(segments, clip_offset, markers=None):
"""segments: from the tail clip, with timestamps relative to that
clip's own start. clip_offset is that clip's start time within the
full episode, added back to give an absolute timestamp."""
markers = markers or load_markers()
for text, start, _end in segments:
if fuzzy_contains(markers["outro_markers"], text):
return clip_offset + start
return None