feat: Enhance speaker analysis with gender inference and update related tests

This commit is contained in:
JB
2025-10-08 07:09:12 -07:00
parent 3b07df9708
commit 1bae37477b
9 changed files with 361 additions and 155 deletions
+13
View File
@@ -0,0 +1,13 @@
import importlib
import sys
# Ensure real optional dependencies are imported before tests that install stubs
# so that available packages (like ebooklib, bs4) aren't replaced with dummy modules.
for module_name in ("ebooklib", "bs4"):
if module_name not in sys.modules:
try:
importlib.import_module(module_name)
except Exception:
# On environments without the optional dependency, downstream tests
# will install lightweight stubs as needed.
pass
+44
View File
@@ -0,0 +1,44 @@
from abogen.speaker_analysis import analyze_speakers
def _chapters():
return [
{
"id": "0001",
"index": 0,
"title": "Test",
"text": "",
"enabled": True,
}
]
def _chunk(text: str, idx: int) -> dict:
return {
"id": f"chunk-{idx}",
"chapter_index": 0,
"chunk_index": idx,
"text": text,
}
def test_analyze_speakers_infers_gender_from_pronouns():
chunks = [
_chunk("\"Greetings,\" said John. He adjusted his hat as he smiled.", 0),
_chunk("\"Hello,\" said Mary. She straightened her dress as she introduced herself.", 1),
_chunk("\"Nice to meet you,\" said Alex.", 2),
]
analysis = analyze_speakers(_chapters(), chunks, threshold=1, max_speakers=0)
john = analysis.speakers.get("john")
mary = analysis.speakers.get("mary")
alex = analysis.speakers.get("alex")
assert john is not None
assert mary is not None
assert alex is not None
assert john.gender == "male"
assert mary.gender == "female"
assert alex.gender == "unknown"
+27
View File
@@ -0,0 +1,27 @@
from pathlib import Path
from abogen.text_extractor import extract_from_path
from abogen.utils import calculate_text_length
ASSET = Path("test_assets/alexandre-dumas_the-count-of-monte-cristo_chapman-and-hall.epub")
def test_epub_character_counts_align_with_calculated_total():
result = extract_from_path(ASSET)
combined_total = calculate_text_length(result.combined_text)
chapter_total = sum(chapter.characters for chapter in result.chapters)
assert result.total_characters == combined_total == chapter_total
def test_epub_metadata_composer_matches_artist():
result = extract_from_path(ASSET)
composer = result.metadata.get("composer") or result.metadata.get("COMPOSER")
artist = result.metadata.get("artist") or result.metadata.get("ARTIST")
assert composer
assert composer == artist
assert composer != "Narrator"