Skip to content

Ground truth text alignment (--ground-truth)#33

Merged
roelvangils merged 1 commit into
mainfrom
ground-truth-alignment
May 7, 2026
Merged

Ground truth text alignment (--ground-truth)#33
roelvangils merged 1 commit into
mainfrom
ground-truth-alignment

Conversation

@roelvangils
Copy link
Copy Markdown
Member

Summary

  • Replace Whisper's approximate transcription with the real book text while preserving word-level audio sync, using Myers diff + Jaro-Winkler fuzzy matching.
  • New crate dpub-align (~1000 lines, 44 unit + 1 integration test) — testable in isolation, no whisper.cpp build dependency.
  • Auto-detects four input formats: markdown, plain text, structured JSON (per-chapter array), and bulk JSON (whole book in one blob with inline ALL-CAPS chapter titles).
  • Audiobook preambles and outros are detected via anchor runs (≥5 consecutive matches) and discarded so they never smear into the first/last real word.
  • New CLI flag: --ground-truth <PATH> (requires --transcribe); --ground-truth-strategy <drop|no-sync|bracket> controls how book-only material (colophon, index) is handled.

Bug fixes surfaced during end-to-end testing

  • OPF manifest IDs prefixed with s- when the stem starts with a digit. XML Names cannot start with a digit, so any DAISY book with 001_*.smil filenames previously failed EPUBCheck RSC-005. The reference book happened to use letter-prefixed filenames so the bug was latent.
  • Empty <seq> elements no longer leak into Media Overlay SMIL (EPUBCheck RSC-005). Builder skips empty paragraph wrappers; SMIL writer defensively drops recursively-empty subtrees; the heading-level shell is preserved when alignment would produce an entirely empty word tree.
  • Words rounding to the same millisecond as their neighbour are now filtered (EPUBCheck MED-009). The builder mirrors the writer's ms rounding so two distinct f64 timestamps that collide post-rounding can't ship as clipBegin == clipEnd.

End-to-end verification

Tested on De verwarde Cavia (109 sections, 4h22m audio) with the bulk-JSON ground truth:

  • 103/109 sections matched (94%)
  • 209 boundary trim events (e.g. "Einde van het boek" correctly caught as a Whisper-only outro)
  • Final EPUB passes EPUBCheck clean (0 fatals / 0 errors / 0 warnings)

Test plan

  • cargo test --workspace — all green
  • cargo run --example match_sections dry-run dependency-free
  • Real book conversion + EPUBCheck validation
  • Open the resulting EPUB in Thorium and confirm karaoke highlight tracks audio across at least one chapter
  • (Follow-up) On-disk Whisper transcription cache so re-runs don't re-transcribe

🤖 Generated with Claude Code

Replace Whisper's approximate transcription with the real book text
while preserving word-level audio sync via Myers diff + Jaro-Winkler
fuzzy matching. Auto-detects markdown / plain text / structured-JSON /
bulk-JSON ground-truth inputs.

New crate `dpub-align` (~1000 lines, 44 unit + 1 integration test):

- normalize.rs   — Unicode-aware lowercase + punctuation stripping
- diff.rs        — Myers diff via `similar` 3.x with Jaro-Winkler ≥ 0.85
                   fuzzy promotion (catches "Antwerpe" → "Antwerpen")
- boundary.rs    — Anchor detection (≥5 consecutive matches) classifies
                   ops as Leading/Core/Trailing so audiobook preambles
                   and outros never smear into the first/last real word
- transfer.rs    — Timestamp transfer with three boundary strategies
                   (Drop / NoSync / Bracket), monotonicity enforcement,
                   character-proportional interpolation for inserts
- section_split.rs — Markdown vs plain-text auto-detect; fuzzy heading
                   matching against DAISY NCC titles
- json_format.rs — Structured (per-chapter) and bulk (whole-book blob)
                   JSON formats; pass-through with NBSP normalisation

CLI:
- --ground-truth <PATH> (requires --transcribe)
- --ground-truth-strategy <drop|no-sync|bracket> (default: no-sync)
- Config file fields `ground_truth` and `ground_truth_strategy`

Bug fixes surfaced by end-to-end testing on a digit-prefixed DAISY book:
- OPF manifest IDs now prefixed with `s-` when stems start with a digit
  (XML Names cannot start with digits — broke any DAISY book with
  `001_*.smil` filenames; the reference book happened to use letter
  prefixes so nobody noticed).
- Empty `<seq>` elements no longer leak into Media Overlay SMIL files
  (EPUBCheck RSC-005). Empty paragraph wrappers are skipped at the
  builder layer; the SMIL writer also defensively drops recursively
  empty seq subtrees; the heading-level overlay shell is preserved
  when alignment would have produced an entirely empty word tree.
- Words that round to the same millisecond as their neighbour no
  longer ship in SMIL (EPUBCheck MED-009). The builder mirrors the
  writer's millisecond rounding when filtering zero-duration words.

End-to-end verified on "De verwarde Cavia" (109 sections, 4h22m
audio): 103/109 sections matched, 209 boundary trim events recorded,
final EPUB passes EPUBCheck clean (0 fatals / 0 errors / 0 warnings).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@roelvangils roelvangils merged commit 5a28b99 into main May 7, 2026
5 of 6 checks passed
@roelvangils roelvangils deleted the ground-truth-alignment branch May 7, 2026 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant