speaker_helper.eardrum.targets module
Targets: what a word should sound like, and where that knowledge comes from.
Module summary
The loop can only correct a pronunciation against a reference. This module finds that reference, in decreasing order of trust:
The human — the lexicon entry’s own
ipa:. Final.Wiktionary — the French and English editions carry IPA for a great many proper nouns, foreign words and initialisms (
{{pron|ɛs.kil|fr}},{{IPA|en|/ˈfaɪnmən/}}), and they list every accepted variant.Wikipedia — the lead sentence of an article about a person or place usually gives the name’s IPA (
{{IPAc-en|ˈ|f|aɪ|n|m|ə|n}},{{API|…}}); trusted only when the article’s title ends with the word.espeak-ng reading the current respelling — a weak target: it checks that the engine reads the respelling the way it was designed to be read, not that the respelling was right in the first place. Reported as such.
Web lookups go through one keep-alive client, paced with a random pause
between requests, backed off with full jitter on 429 / 503, with an honest
User-Agent; every answer, found or not, is cached on disk
(~/.cache/speaker-helper/eardrum/targets.yaml) so a word is looked up once
per machine, ever.
Usage example
>>> wiktionary_ipas("{{pron|e.ʃil|ɛs.kil|fr}}", "fr")
[('e.ʃil', 'fr'), ('ɛs.kil', 'fr')]
- class speaker_helper.eardrum.targets.PoliteClient(*, min_gap=0.3, max_gap=1.5, attempts=4, base=5.0, cap=300.0, sleep=<built-in function sleep>)[source]
Bases:
objectOne keep-alive-minded HTTP client that never hammers a server.
- Parameters:
min_gap (float) – Bounds of the random pause before every request, in seconds.
max_gap (float) – Bounds of the random pause before every request, in seconds.
attempts (int) – How many times a refused request (429, 503, timeout) is tried in all.
base (float) – Exponential backoff parameters: the wait before retry
kis uniform in[0, min(cap, base * 2**k)], never below a server’sRetry-After.cap (float) – Exponential backoff parameters: the wait before retry
kis uniform in[0, min(cap, base * 2**k)], never below a server’sRetry-After.sleep (callable) – Injected for tests;
time.sleepin real use.
- get_json(url, params)[source]
GET
urlwithparamsand decode the JSON answer.- Parameters:
- Returns:
The decoded body, or
Nonewhen the server refused us after every retry or the network is down (logged, never raised: a missing target degrades the loop, it does not stop it).- Return type:
dict or None
- speaker_helper.eardrum.targets.SOURCES = ('lexicon', 'wiktionary', 'wikipedia', 'espeak')
Trust order of the sources, most trusted first. Used to sort candidates and printed in reports.
- speaker_helper.eardrum.targets.TARGET_CACHE: Path = PosixPath('~/.cache/speaker-helper/eardrum/targets.yaml')
Where looked-up targets persist, one file per machine.
- class speaker_helper.eardrum.targets.Target(variants, language, source, url='', weak=False)[source]
Bases:
objectThe pronunciation a word should have, with its provenance.
- language
Language the transcription is written for (
"en"for an English name read by a French voice: the target is then the original).- Type:
- weak
Truewhen the target only restates the current respelling (espeak), so a perfect score proves consistency, not correctness.- Type:
- class speaker_helper.eardrum.targets.TargetResolver(cache_path=PosixPath('~/.cache/speaker-helper/eardrum/targets.yaml'), *, web=True, client=None)[source]
Bases:
objectFind and cache the target pronunciation of words.
- Parameters:
cache_path (Path) – The on-disk cache of web lookups (
TARGET_CACHEby default).web (bool) – Allow web lookups. Off means: the lexicon’s IPA, else espeak.
client (PoliteClient or None) – Injected for tests; a fresh paced client otherwise.
- resolve(word, language, *, ipa='', respelling='', espeak=None)[source]
The target pronunciation of
word, most trusted source first.- Parameters:
word (str) – The word or phrase.
language (str) – Language of the text it is spoken in.
ipa (str) – The lexicon entry’s own IPA, if any — final when present.
respelling (str) – The current respelling, for the weak espeak fallback.
espeak (callable or None) –
(text, language) -> ipa;speaker_helper.eardrum.espeak.espeak_ipa()in real use.
- Returns:
Noneonly when nothing at all is known (no IPA, no web answer, no respelling, no espeak).- Return type:
Target or None
- speaker_helper.eardrum.targets.USER_AGENT = 'speaker-helper-eardrum/0.16.0 (https://github.com/warith-harchaoui/speaker-helper; warithmetics@deraison.ai)'
Honest identification, as the Wikimedia API policy asks.
- speaker_helper.eardrum.targets.expand_optional(ipa)[source]
Spell out the optional sounds a transcription marks in parentheses.
Wiktionary writes
k(j)uːbəˈnɛtɪs: the /j/ may or may not be said. Dropping the parentheses would make it mandatory; both readings are accepted instead.- Parameters:
ipa (str) – A transcription, possibly with
(…)groups.- Returns:
Every combination, the fullest first (at most 8, for safety).
- Return type:
Examples
>>> expand_optional("k(j)uːb") ['kjuːb', 'kuːb'] >>> expand_optional("ɡʁas") ['ɡʁas']
- speaker_helper.eardrum.targets.lookup_web(word, language, client)[source]
Look
wordup on Wiktionary, then Wikipedia.Wiktionary is asked in the text’s language first (a French page may give the French way of saying a foreign name), then in English. Wikipedia is only asked when Wiktionary had nothing.
- speaker_helper.eardrum.targets.pick_for_word(ipa, word, title)[source]
The part of a full-name transcription that belongs to
word.A Wikipedia lead transcribes the article’s title (“Richard Feynman”), sometimes only its unusual part (the surname). This returns the chunk for
wordwhen the chunk count and the title’s word count agree, the whole thing when it is a single chunk andwordends the title, and nothing otherwise — a guess is worse than no target.- Parameters:
- Returns:
The chunk(s) for
word, or"".- Return type:
Examples
>>> pick_for_word("ˈrɪtʃərd ˈfaɪnmən", "Feynman", "Richard Feynman") 'ˈfaɪnmən' >>> pick_for_word("ˈfaɪnmən", "Feynman", "Richard Feynman") 'ˈfaɪnmən' >>> pick_for_word("ˈfaɪnmən", "Richard", "Richard Feynman") ''
- speaker_helper.eardrum.targets.target_as_dict(target)[source]
A JSON-ready view of a target, for the report.
- speaker_helper.eardrum.targets.wikipedia_lead_ipas(wikitext, edition)[source]
IPA transcriptions in the lead of a Wikipedia article.
Only the first few thousand characters are read: pronunciations further down are of other names (relatives, places, works).
- Parameters:
- Returns:
(ipa, language)pairs, duplicates removed.- Return type:
Examples
>>> wikipedia_lead_ipas("'''Feynman''' ({{IPAc-en|ˈ|f|aɪ|n|m|ə|n}})", "en") [('ˈfaɪnmən', 'en')]
- speaker_helper.eardrum.targets.wiktionary_ipas(wikitext, edition)[source]
Every IPA transcription in a Wiktionary page, with its language.
- Parameters:
- Returns:
(ipa, language)pairs in page order, duplicates removed.- Return type:
Examples
>>> wiktionary_ipas("{{IPA|en|/ˈfaɪnmən/|/ˈfeɪnmən/}}", "en") [('ˈfaɪnmən', 'en'), ('ˈfeɪnmən', 'en')]