Files
hpr-intro-stripper/tests/test_markers.py
T
Lee Hanken 17dec27955 Replace fixed-phrase marker matching with synopsis-alignment as the primary intro method
markers.find_intro_end_via_synopsis sequence-aligns the ASR transcript
against metadata.synopsis_text()'s deterministic reconstruction of the
expected per-episode announcement text (the say.php reproduction from
earlier), rather than matching against a fixed phrase list. This was a
direct response to the pattern across the last several fixes: each one
patched a single new mis-hearing variant, with no ceiling on how many
exist. Sequence alignment tolerates scattered word-level ASR errors
throughout the transcript, not just at phrase boundaries someone thought
to special-case.

Applied to both the official-SRT and ASR-anchor paths (_resolve_intro_end
in detect.py), since wording drift and ASR mis-hearings affect both.
Falls back to the existing fixed-phrase matching when there's no
expected text to align against, and still defers to it entirely for the
sponsor-ad-read continuation, which has no deterministic per-episode
text of its own.

Live-verified: hpr2157 moved from 15.1s (old marker match) to 30.6s;
hpr3237 to 34.7s; hpr3009 (Linux Inlaws) extended further but stayed
correctly flagged rather than silently trusted; hpr4775 now resolves via
official_srt+synopsis_alignment+reference_xcorr_floor, composing all
three recent fixes. hpr1892 correctly still returns None -- its garbling
is concentrated in the first ~17 words, with no confident anchor near
the start, which the leading-anchor check is specifically designed to
catch rather than trust a coincidental later match.

Also fixes a real bug found while validating this: the sponsor-ad-read
continuation was filtering segments by end time instead of start time,
so a segment that merely ended near the anchor but started before it
broke the extension chain immediately, discarding a real extension
(caught on hpr2157's own sponsor read).

New test file test_synopsis_alignment.py (8 tests), plus two tests
locking in the earlier narrowly-scoped fuzzy-threshold exception for the
station-name marker (validated against both the real mis-hearing and the
original hpr0500 regression case before being enabled).
2026-11-22 12:24:56 +00:00

203 lines
12 KiB
Python

"""
Regression tests for the two real false-positive bugs found while building
the original prototype (see hpr_stripper/markers.py docstring). These use
real transcript text observed during development, not synthetic examples,
specifically so a future change can't silently reintroduce either bug.
"""
import unittest
from hpr_stripper import markers
class TestIntroDetection(unittest.TestCase):
def test_modern_episode_stops_after_tts_block(self):
# hpr3700, real Whisper segmentation.
segments = [
("This is Hacker Public Radio Episode 3,700 for Friday the 7th of October 2022.", 0.0, 13.24),
("Today's show is entitled Introduction to Batch Files.", 13.24, 17.88),
("It is part of the series DOS.", 17.88, 20.36),
("It is hosted by AOKA, and is about 15 minutes long.", 20.36, 25.0),
("It carries a clean flag.", 25.0, 27.12),
("This summary is More on DOS.", 27.12, 30.44),
("This time is Introduction to Batch Files.", 30.44, 41.24),
("Hello, this is AOKA, welcoming you to Hacker Public Radio.", 41.24, 44.92),
("And another exciting episode in our ongoing DOS series.", 44.92, 49.76),
]
self.assertEqual(markers.find_intro_end(segments), 44.92)
def test_2026_wording_variant_is_recognized(self):
# hpr4775, real official SRT -- a wording variant found by visually
# inspecting a waveform/spectrogram of the clip (see docs/DESIGN.md):
# "the host is X and the duration is Y" / "the flag is Y, and the
# license is Z" replaced the older "is hosted by" / "it carries a
# clean flag" phrasing. The old marker set stopped matching after
# segment 2 (16.4s) and silently under-trimmed; it must now reach
# the real boundary at 27.32s.
segments = [
("This is Hacker Public Radio Episode 4775, for 2026-11-20", 0.0, 13.2),
('Today\'s show is entitled, "BruCON interview 4: northstar: robots and AI"', 13.2, 16.4),
("The host is Kristoff and the duration is 00:24:05", 16.4, 20.04),
("The flag is Clean, and the license is CC-BY-SA", 20.04, 22.08),
('The summary is "Kristoff talks to Achraf of Northstar about how AI is makes robotics accessable for everybody"', 22.08, 27.32),
("I, everybody, welcome to exciting new episode of Hacker Public Radio, my name is Christof.", 40.0, 46.52),
]
self.assertEqual(markers.find_intro_end(segments), 27.32)
def test_2015_2020_template_with_bare_hpr_abbreviation(self):
# hpr2157, real Whisper tiny transcription. This era's template
# apparently says the bare "HPR episode" rather than "Hacker
# Public Radio episode"; Whisper also mis-heard "is part" as "in
# part" here specifically. Found via a waveform inspection after a
# metadata_arithmetic disagreement flag (84.2s vs 3.2s) pointed at
# this episode; the old marker set matched nothing in segment 0
# and returned None immediately, falling through to a much worse
# sibling-correlation guess (3.25s) instead of the real ~37.6s
# boundary.
segments = [
("This in HPR episode 2,157 entitled Markham Manchester Part 3, and in part of the series, Interviews.", 0.0, 8.1),
("It is posted by Tony H1,212, and in about 8 minutes long.", 8.1, 14.0),
("The summary is.", 14.0, 15.1),
("This in an Interview with Alononovo, which I did at Markham Manchester.", 15.1, 19.8),
("This episode of HPR is brought to you by An Honest Host at Come.", 19.8, 24.3),
("Get 15% discount on all shared hosting with the offer code HPR-15.", 24.3, 30.6),
("That's HPR-15.", 30.6, 33.1),
("Better web hosting that's Honest and Fair at An Honest Host.com.", 33.1, 37.6),
]
self.assertIsNotNone(markers.find_intro_end(segments))
def test_todays_misheard_as_to_its(self):
# hpr3356: Whisper tiny heard "Today's show is entitled" as "To
# its show is entitled", breaking the marker on segment 1 even
# though segment 0 matched fine.
segments = [
("This is Hacker Public Radio episode, 3,356 Formundi, 14 June 2021.", 0.0, 8.0),
("To its show is entitled, HPR 2020, 2021 New Year's Eve, Show Episode 5.", 8.0, 15.5),
]
self.assertEqual(markers.find_intro_end(segments), 15.5)
def test_sponsor_ad_read_is_recognized_when_reached(self):
# hpr2263, real Whisper tiny transcription: a fixed, reused
# anhonesthost.com ad script immediately follows the TTS block.
# This specific episode's free-text summary happens to end with
# "with the letter after." -- genuinely unmatched filler that is
# part of the host-written summary, not station boilerplate -- so
# the chain correctly stops at 14.0s (end of the "minutes long"
# segment) rather than reaching the ad read at all. That's the
# intended conservative behaviour (never bridge unmatched text),
# not a bug; the sponsor markers below are exercised directly to
# confirm they'd be recognized once reached.
segments = [
("This image, the Repisode 2000-2063 entitled Freak.ge, it is posted by first-time host FTH and in about", 0.0, 8.3),
("24 minutes long, and Karima Klienflag, the summary in, a rift through a variety of topics", 8.3, 14.0),
("with the letter after.", 14.0, 15.8),
("This episode of HBR is brought to you by An Honest Host.com.", 15.8, 20.2),
("Get 15% discount on all shared hosting, with the offer code HBR15, that's HBR15.", 20.2, 29.0),
("Get your Rep hosting that's Honest and Fair at An Honest Host.com.", 29.0, 32.9),
]
self.assertEqual(markers.find_intro_end(segments), 14.0)
for text, _start, _end in segments[3:]:
self.assertTrue(markers.fuzzy_contains(markers.load_markers()["strong_intro_markers"], text, min_words_for_fuzzy=100))
def test_archive_org_sponsor_ad_read_is_recognized(self):
# hpr3419, real Whisper tiny transcription: the other of HPR's two
# observed sponsor ad scripts (Internet Archive, not
# anhonesthost.com). The chain correctly reaches 38.2s (through
# "brought to you by archive.org" and "support universal access to
# all knowledge") -- real improvement over the old marker set,
# which stopped at 27.2s. The final segment, "forward slash
# doughnays." (Whisper's mangling of ".../donate"), doesn't match
# on its own since Whisper split "archive.org forward slash" across
# a segment boundary; a bare "forward slash" marker would close
# that gap but is too generic to add safely (a host could
# plausibly say it while reading out an unrelated URL in their own
# content), so this small, conservative undershoot is accepted
# rather than chased.
segments = [
("This is hacker public radio episode, 3,419-430, the 9th of September 2021.", 0.0, 8.0),
("The summary is, all you ever wanted to hear, and more about micro kernels, and other operating", 21.6, 27.2),
("stories. This episode of HPR is brought to you by archive.org.", 27.7, 33.0),
("Support Universal Access to all knowledge by heading over to archive.org", 33.0, 38.2),
("forward slash doughnays.", 38.2, 48.2),
]
self.assertEqual(markers.find_intro_end(segments), 38.2)
def test_fuzzy_match_catches_a_real_mishearing_of_the_station_name(self):
# hpr3237: Whisper tiny heard "Hacker Public Radio" as "Haka
# Public Radio" -- close enough (ratio 0.923 against the canonical
# marker) that fuzzy matching, enabled for markers of >=4 words at
# a 0.88 threshold, correctly recognizes it.
segments = [
("This is Haka Public Radio episode 3,237-149 of December 2020.", 0.0, 7.6),
]
self.assertEqual(markers.find_intro_end(segments), 7.6)
def test_fuzzy_threshold_still_rejects_the_bug_2_regression_case(self):
# The exact hpr0500 case from bug #2 (see module docstring) scores
# 0.824 against "hacker public radio episode" -- below the 0.88
# threshold, so this must still return None. This is the specific
# regression this threshold choice must never reopen.
segments = [
("Hi everyone, this is Klaatu and this is Hacker Public Radio, year review number two,", 0.0, 23.0),
]
self.assertIsNone(markers.find_intro_end(segments))
def test_organic_cold_open_is_not_mistaken_for_tts_boilerplate(self):
# hpr0500: the host's own greeting happens to say "this is Hacker
# Public Radio", but there is no actual TTS metadata announcement.
# Bug: a loose fuzzy match on the "welcome to HPR" greeting line
# alone used to trigger a false intro_end here.
segments = [
("Hi everyone, this is Klaatu and this is Hacker Public Radio, year review number two,", 0.0, 23.0),
("where we could call it around table, so I have it done around table in like three months.", 23.0, 27.0),
("On the call is Enigma.", 27.0, 29.0),
]
self.assertIsNone(markers.find_intro_end(segments))
def test_greeting_alone_never_starts_a_run(self):
# A "welcome to HPR" line with nothing stronger around it must not
# be trusted on its own, even in isolation. (Deliberately avoids
# the literal substring "hacker public radio episode", which is a
# *strong* marker on its own -- this text must only be able to
# match the "greeting" markers.)
segments = [("Hello, this is Dan, welcoming you to Hacker Public Radio.", 0.0, 4.0)]
self.assertIsNone(markers.find_intro_end(segments))
class TestOutroDetection(unittest.TestCase):
def test_finds_real_outro_marker(self):
tail_segments = [
("You can see it on my website or take a look at it if you have the media around yourself.", 0.0, 7.36),
("This is a hookah for Hacker Public Radio signing off and is always encouraging you to support free software. Bye-bye.", 7.36, 22.76),
("You have been listening to Hacker Public Radio, as Hacker Public Radio does work.", 22.76, 27.56),
]
# Bug: a too-loose fuzzy fallback once matched the host's own
# sign-off ("...Hacker Public Radio signing off...") against the
# short marker "hacker public radio does", nearly truncating the
# host's last sentence. It must match only the real marker,
# anchored at 22.76 (clip-relative), not inside the sign-off.
clip_offset = 903.14
result = markers.find_outro_start(tail_segments, clip_offset)
self.assertEqual(result, clip_offset + 22.76)
def test_host_signoff_alone_does_not_trigger_outro(self):
tail_segments = [
("This is a hookah for Hacker Public Radio signing off and is always encouraging you to support free software. Bye-bye.", 0.0, 15.4),
]
self.assertIsNone(markers.find_outro_start(tail_segments, 0.0))
class TestFuzzyContains(unittest.TestCase):
def test_strong_markers_never_use_fuzzy_fallback(self):
# "is hosted by" is a strong marker (3 words); it must not fuzzy-match
# unrelated text that merely shares a couple of words.
self.assertFalse(
markers.fuzzy_contains(["is hosted by"], "this show is mostly hosted wherever", min_words_for_fuzzy=100)
)
def test_exact_substring_always_matches(self):
self.assertTrue(markers.fuzzy_contains(["minutes long"], "and is about 15 minutes long today"))
if __name__ == "__main__":
unittest.main()