speaker_helper.eardrum.loop module

The Ralph Eardrum Loop: say it, hear it, fix the spelling, say it again.

Module summary

For each word under test — a lexicon entry, or a word discovered in a document — the loop:

  1. finds the target pronunciation (targets: the lexicon’s IPA, Wiktionary, Wikipedia, else espeak);

  2. has the real engine say the word inside two carrier sentences (“Je vous parle de … aujourd’hui.”), because a word said alone is not said the way it is said in a sentence;

  3. hears each take with a phoneme recogniser (ear), finds the word inside the sentence and scores it against the target (phones); the take’s score is the mean over the carriers;

  4. when the score is above the threshold, asks the local LLM for another respelling, showing it the target and every earlier attempt with what was heard and how far it was, and goes back to 2 — at most max_iter times;

  5. keeps the best attempt, not the last one, and records it in the lexicon’s verified sidecar (lexicon.yaml.eardrum.yaml) for this engine, so every later synthesis uses it.

A hand-written respelling is never replaced: when it fails, the report says so and proposes the better one, and the human decides. For an engine that takes phonemes (kokoro), there is nothing to respell — the loop checks that the target IPA itself comes out right and records it.

Every take is written to disk, and the report (index.html) puts each word’s clips side by side: the measure points, the ear decides.

Usage example

>>> carriers_for("fr")[0]
"Je vous parle de {w} aujourd'hui."

Author

Warith HARCHAOUI — https://linkedin.com/in/warith-harchaoui

class speaker_helper.eardrum.loop.Attempt(candidate, kind, origin, score=1.0, takes=<factory>)[source]

Bases: object

One candidate spelling (or IPA) and its takes.

Parameters:
candidate

The respelling (or IPA) tried.

Type:

str

kind

"respelling" or "ipa".

Type:

str

origin

Where it came from: "written" (the word as written), "current" (what the lexicon applies today), "llm", "target" (the target IPA itself, for phoneme engines).

Type:

str

score

Mean score over the takes.

Type:

float

takes

One per carrier sentence.

Type:

list of Take

candidate: str
kind: str
origin: str
score: float = 1.0
takes: list[Take]
speaker_helper.eardrum.loop.CARRIERS: dict[str, list[str]] = {'de': ['Ich spreche heute über {w} mit euch.', 'Es war {w}, der das sagte.'], 'en': ['I am talking about {w} today.', 'It was {w} who said it.'], 'es': ['Hoy les hablo de {w} otra vez.', 'Fue {w} quien lo dijo.'], 'fr': ["Je vous parle de {w} aujourd'hui.", "C'est bien {w} qui l'a dit."], 'it': ['Oggi vi parlo di {w} di nuovo.', 'È stato {w} a dirlo.'], 'pt': ['Hoje falo de {w} outra vez.', 'Foi {w} quem o disse.']}

Carrier sentences per language. Neutral, short, the word in the middle, with plain words around it that every engine and espeak read alike.

speaker_helper.eardrum.loop.CLIP_PAD_S = 0.08

Padding around a word’s clip, so its first and last phones are audible.

speaker_helper.eardrum.loop.DEFAULT_THRESHOLD = 0.25

Below this score a take counts as said right. Calibrated on chatterbox French takes: “Faïnemane” heard as s a i n m a n against /fajnman/ scores 0.06, the wrong “fènman” scores 0.39.

speaker_helper.eardrum.loop.IMPROVEMENT_MARGIN = 0.05

A new respelling must beat the current one by this much to replace it: two takes of the same text differ by a few hundredths on their own.

class speaker_helper.eardrum.loop.Listener(speaker, ear, espeak, out_dir)[source]

Bases: object

Says a candidate through the real engine and scores what is heard.

Parameters:
  • speaker (Speaker) – An open Speaker (engine loaded once).

  • ear (Ear) – The phoneme recogniser.

  • espeak (callable) – (text, language) -> ipa for the carrier sentences.

  • out_dir (Path or None) – Where takes are written (None writes nothing — tests).

async attempt(job, target, candidate, *, kind, origin, number)[source]

Say candidate in every carrier and score the takes.

Parameters:
  • job (WordJob) – The word.

  • target (Target) – What it should sound like.

  • candidate (str) – Respelling (or IPA when kind == "ipa").

  • kind (str) – "respelling" or "ipa".

  • origin (str) – See Attempt.

  • number (int) – Attempt number, for file names.

Returns:

With its takes and mean score.

Return type:

Attempt

property phoneme_input: bool

Whether the engine takes IPA itself (kokoro) rather than text.

speaker_helper.eardrum.loop.SPELLING_GUIDE: dict[str, str] = {'en': '/ʃ/ sh · /tʃ/ ch · /ʒ/ zh · /dʒ/ j · /k/ k · /ɡ/ g · /ŋ/ ng · /j/ y · /i/ ee · /ɪ/ i · /u/ oo · /ʊ/ u · /y/ ew · /e/ ay · /ɛ/ e · /æ/ a · /ɑ/ ah · /ɔ/ aw · /o/ oh · /ə/ uh · /aɪ/ eye · /aʊ/ ow · /ʁ/ r · stress the syllable written in capitals only if needed.', 'fr': '/ʃ/ ch · /tʃ/ tch · /dʒ/ dj · /ʒ/ j · /k/ k or qu · /ɡ/ g (gu before e, i) · /s/ ss between vowels · /z/ z · /ŋ/ ng · /j/ y or ï · /w/ ou · /h/ (silent, drop it) · /i/ i · /u/ ou · /y/ u · /e/ é · /ɛ/ è · /ə/ e (eu at the end) · /ø/ eu · /o/ ô · /ɔ/ o · /a/ a · /aɪ/ aï · /eɪ/ èï · /aʊ/ aou · /ɔɪ/ oï · /ʌ/ eu · /ɪ/ i · /ʊ/ ou · /θ/ ss · /ð/ d · /ɹ/ r · a final consonant that must be heard gets a final e (Sutton → Seutone), a vowel before n or m that must not be nasal is followed by ne / me (Feynman → Faïnemane).'}

a cheat sheet that turns a small model’s guesswork into lookups. Measured need: without it, gemma3:12b answered “Nitcherche” for /ˈniːtʃə/ three times in a row.

Type:

How each sound is usually written, per orthography

class speaker_helper.eardrum.loop.Take(carrier, score, heard, heard_sentence, audio='', clip='')[source]

Bases: object

One carrier sentence said with one candidate, and what was heard.

Parameters:
carrier

The sentence template.

Type:

str

score

Distance of the word’s region to the target (0 = perfect).

Type:

float

heard

The phones heard in the word’s region, space-separated.

Type:

str

heard_sentence

Every phone heard in the sentence.

Type:

str

audio

Path of the whole take, relative to the report directory.

Type:

str

clip

Path of the word’s own clip, relative to the report directory.

Type:

str

audio: str = ''
carrier: str
clip: str = ''
heard: str
heard_sentence: str
score: float
class speaker_helper.eardrum.loop.WordJob(word, language, entry=None, reasons=<factory>)[source]

Bases: object

A word to put through the loop.

Parameters:
word

The word or phrase.

Type:

str

language

Language it is spoken in.

Type:

str

entry

The lexicon entry, when there is one.

Type:

LexiconEntry or None

reasons

Why it is tested.

Type:

list of str

entry: LexiconEntry | None = None
language: str
reasons: list[str]
word: str
class speaker_helper.eardrum.loop.WordResult(word, language, engine, reasons=<factory>, source='new', target=None, attempts=<factory>, best=-1, verdict='unresolved', saved=False, note='')[source]

Bases: object

Everything the loop did for one word.

Parameters:
word, language, engine

What was tested, in which language, on which backend.

Type:

str

reasons

Why the word was tested (lexicon, proper, acronym…).

Type:

list of str

source

The lexicon entry’s source (user, builtin, llm, eardrum) or "new" for a discovered word.

Type:

str

target

The Target, as a dict.

Type:

dict or None

attempts

In the order tried.

Type:

list of Attempt

best

Index of the best attempt (-1 when nothing was tried).

Type:

int

verdict

ok (already right), fixed (a new spelling passes and was saved), improved (better, saved, still above the threshold), proposal (a hand-written spelling fails; a better one is proposed, not applied), unresolved, no-target.

Type:

str

saved

Whether the verified sidecar was written for this word.

Type:

bool

note

A sentence for the human, in French (the report’s language).

Type:

str

attempts: list[Attempt]
best: int = -1
engine: str
language: str
note: str = ''
reasons: list[str]
saved: bool = False
source: str = 'new'
target: dict[str, Any] | None = None
verdict: str = 'unresolved'
word: str
speaker_helper.eardrum.loop.carriers_for(language)[source]

The carrier sentences for language (English ones as a last resort).

Parameters:

language (str) – ISO-639-1 code.

Returns:

Templates with a {w} slot.

Return type:

list of str

speaker_helper.eardrum.loop.chosen_variant(variants, intended_ipa)[source]

The accepted pronunciation closest to the one a respelling encodes.

Parameters:
  • variants (list of str) – Accepted IPA transcriptions.

  • intended_ipa (str) – espeak’s reading of the current respelling.

Returns:

The closest variant (the first one on a tie).

Return type:

str

Examples

>>> chosen_variant(["e.ʃil", "ɛs.kil"], "ɛskil")
'ɛs.kil'
speaker_helper.eardrum.loop.phones_of(ipa)[source]

Space-separated phones of an IPA string, as shown in reports.

Parameters:

ipa (str) – Any IPA.

Returns:

The tokenised phones joined by spaces.

Return type:

str

Examples

>>> phones_of("ˈfaɪn.mən")
'f a ɪ n m ə n'
speaker_helper.eardrum.loop.propose_respelling(word, language, target, history)[source]

Ask the local LLM for a respelling not tried yet.

The model sees the target, a sound-to-spelling guide for the orthography, and every earlier attempt with what the ear heard and, phone by phone, what went wrong — the information a human would use to adjust a spelling by ear. A repeat is answered in the same conversation (“already tried, another one”), with a little more temperature each time.

Parameters:
  • word (str) – The word.

  • language (str) – ISO-639-1 code of the orthography to respell into.

  • target (Target) – The pronunciation to reach.

  • history (list of Attempt) – Earlier attempts, any order.

Returns:

A new respelling, or "" when the LLM is unavailable, declines or only repeats earlier attempts.

Return type:

str

speaker_helper.eardrum.loop.save_verified(path, key, record)[source]

Write one verified record into the sidecar (read-modify-write).

Parameters:
  • path (Path) – <lexicon>.eardrum.yaml.

  • key (str) – "engine:language:word" (word casefolded).

  • record (dict) – word, respelling or ipa, score, verified, target, target_source, heard, date.

Return type:

None

speaker_helper.eardrum.loop.slug(word)[source]

A filesystem-safe name for a word.

Parameters:

word (str) – Any word.

Returns:

Lowercase ASCII-ish, hyphen-separated.

Return type:

str

Examples

>>> slug("Van Gogh"), slug("l'ONU")
('van-gogh', 'l-onu')
async speaker_helper.eardrum.loop.tune_word(job, listener, resolver, *, engine, max_iter=4, threshold=0.25, llm=True, verified_path=None, current='', proposer=<function propose_respelling>)[source]

Run the loop on one word.

Parameters:
  • job (WordJob) – The word.

  • listener (Listener) – Says and hears candidates.

  • resolver (TargetResolver) – Finds the target.

  • engine (str) – Backend name, for the verified key and the report.

  • max_iter (int) – Maximum number of LLM proposals.

  • threshold (float) – Score at or below which a take counts as right.

  • llm (bool) – Allow LLM proposals (off: measure only).

  • verified_path (Path or None) – Where to save a winning spelling (None: dry run).

  • current (str) – The respelling applied today for this engine (lexicon, verified sidecar…); empty when the word is read as written.

  • proposer (callable) – Injected for tests; propose_respelling() in real use.

Returns:

Everything tried, the best attempt and the verdict.

Return type:

WordResult