Files
hpr-intro-stripper/tests/test_synopsis_alignment.py
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

124 lines
6.8 KiB
Python

"""
Tests for markers.find_intro_end_via_synopsis -- sequence-alignment
against the deterministically-known expected TTS text, rather than a
fixed phrase list. See markers.py's docstring for why this exists.
"""
import unittest
from hpr_stripper import markers
class TestSynopsisAlignment(unittest.TestCase):
def test_scattered_garbling_throughout_still_aligns(self):
# hpr2157, real metadata (metadata.synopsis_text) and real
# Whisper tiny transcription. The title, host name, and series
# fields are each garbled *differently* from the real text
# ("Markham Manchester" for "BarCamp Manchester", "Tony H1,212"
# for "TonyH1 2 1 2") -- no single phrase marker, literal or
# fuzzy, could reconstruct this. But the surrounding boilerplate
# words still align, giving enough confirmed evidence overall.
expected = (
'This is Hacker Public Radio episode 2157 for Tuesday the 8 of November 2016. '
'Todays show is entitled. BarCamp Manchester part 3. It is part of the series '
'"Interviews". It is the 60th show of Tony Hughes A.K.A TonyH1 2 1 2, and is '
'about 8 minutes long. It carries a clean flag. . The summary is. This is an '
"interview with Alan O'Donohoe which I did at BarCamp Manchester."
)
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),
]
result = markers.find_intro_end_via_synopsis(segments, expected)
self.assertEqual(result, 19.8)
def test_garbling_concentrated_at_the_start_correctly_declines(self):
# hpr1892, real metadata and real Whisper tiny transcription: "This
# image PR episode" (garbling "This is HPR episode") plus a wrong
# host name and episode number leaves no confident match anywhere
# near the start of the clip. Unlike hpr2157, there's no early
# anchor to bridge from -- correctly returns None rather than
# trusting a match that only appears deep into the transcript
# (leading_anchor_words). This is an accepted limitation, not a
# bug: some transcripts are too degraded for any method to
# safely resolve.
expected = (
'This is Hacker Public Radio episode 1892 for Tuesday the 3 of November 2015. '
'Todays show is entitled. my chicken coop. It is part of the series '
'"Hobby Electronics". It is hosted by Jezra, and is about 10 minutes long. '
'It carries an explicit flag. . The summary is. '
'jezra blabs about the brok brok brok house'
)
segments = [
("This image PR episode £1,802 entitled Magic in Cube.", 0.0, 5.2),
("It is posted by General and in about 10 minutes long.", 5.2, 8.4),
("The summary is, General Blam about the Brock Brock House.", 8.4, 12.3),
]
self.assertIsNone(markers.find_intro_end_via_synopsis(segments, expected))
def test_sponsor_ad_read_is_appended_after_the_synopsis_anchor(self):
# hpr2263-style: once the per-episode synopsis is confirmed by
# alignment, a following sponsor ad-read (no deterministic
# per-episode text of its own) should still extend the boundary
# via the existing marker list, not get dropped on the floor.
expected = (
"This is Hacker Public Radio episode 2263 for Monday the 1st of January 2018. "
"Todays show is entitled. Test Episode. It is hosted by Test Host, and is about "
"5 minutes long. It carries a clean flag. The summary is. A short test episode."
)
segments = [
("This is Hacker Public Radio episode 2263 for Monday the 1st of January 2018.", 0.0, 8.0),
("Todays show is entitled. Test Episode.", 8.0, 10.0),
("It is hosted by Test Host, and is about 5 minutes long.", 10.0, 14.0),
("It carries a clean flag.", 14.0, 16.0),
("The summary is. A short test episode.", 16.0, 19.0),
("This episode of HPR is brought to you by An Honest Host.com.", 19.0, 23.0),
("Get 15% discount on all shared hosting, with the offer code HPR15.", 23.0, 28.0),
]
result = markers.find_intro_end_via_synopsis(segments, expected)
self.assertEqual(result, 28.0)
def test_no_expected_text_returns_none(self):
self.assertIsNone(markers.find_intro_end_via_synopsis([("anything", 0.0, 1.0)], None))
self.assertIsNone(markers.find_intro_end_via_synopsis([("anything", 0.0, 1.0)], ""))
def test_no_segments_returns_none(self):
self.assertIsNone(markers.find_intro_end_via_synopsis([], "some expected text"))
def test_unrelated_content_does_not_align(self):
# A host's own content that happens to share a few common words
# with the expected synopsis must not be mistaken for it -- the
# min_total_matched_words / min_block_words floor should reject
# weak, scattered single-word overlaps.
expected = (
"This is Hacker Public Radio episode 4000 for Friday the 1st of January 2021. "
"Todays show is entitled. Introduction to Rust. It is hosted by Dave, and is "
"about 20 minutes long. It carries a clean flag. The summary is. "
"A gentle introduction to the Rust programming language."
)
segments = [
("So today I want to talk about my garden and the tomatoes I'm growing this year.", 0.0, 6.0),
("It's been a long season and I think the summary is that it's going well.", 6.0, 12.0),
]
result = markers.find_intro_end_via_synopsis(segments, expected)
self.assertIsNone(result)
def test_coincidental_mid_transcript_resemblance_is_not_trusted(self):
# The expected text happens to resemble something later in the
# transcript, but NOT near the start -- there's no confirmed
# anchor that the episode actually opens with the TTS block, so
# this must not be trusted (leading_anchor_words).
expected = "Todays show is entitled. A Test Topic. The summary is. A short test."
segments = [
("Completely unrelated host chatter that goes on for a while before anything else.", 0.0, 10.0),
("More unrelated chatter continuing on and on with different topics entirely here.", 10.0, 20.0),
("Todays show is entitled. A Test Topic. The summary is. A short test.", 20.0, 24.0),
]
result = markers.find_intro_end_via_synopsis(segments, expected)
self.assertIsNone(result)
if __name__ == "__main__":
unittest.main()