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:

  1. The human — the lexicon entry’s own ipa:. Final.

  2. 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.

  3. 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.

  4. 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')]

Author

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

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: object

One 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 k is uniform in [0, min(cap, base * 2**k)], never below a server’s Retry-After.

  • cap (float) – Exponential backoff parameters: the wait before retry k is uniform in [0, min(cap, base * 2**k)], never below a server’s Retry-After.

  • sleep (callable) – Injected for tests; time.sleep in real use.

get_json(url, params)[source]

GET url with params and decode the JSON answer.

Parameters:
  • url (str) – Endpoint (https://fr.wiktionary.org/w/api.php).

  • params (dict) – Query parameters.

Returns:

The decoded body, or None when 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

refused_hosts: set[str]
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: object

The pronunciation a word should have, with its provenance.

Parameters:
variants

Accepted IPA transcriptions (any of them is a correct reading).

Type:

list of str

language

Language the transcription is written for ("en" for an English name read by a French voice: the target is then the original).

Type:

str

source

One of SOURCES.

Type:

str

url

Where it was read, for the report (empty for the lexicon / espeak).

Type:

str

weak

True when the target only restates the current respelling (espeak), so a perfect score proves consistency, not correctness.

Type:

bool

language: str
source: str
url: str = ''
variants: list[str]
weak: bool = False
class speaker_helper.eardrum.targets.TargetResolver(cache_path=PosixPath('~/.cache/speaker-helper/eardrum/targets.yaml'), *, web=True, client=None)[source]

Bases: object

Find and cache the target pronunciation of words.

Parameters:
  • cache_path (Path) – The on-disk cache of web lookups (TARGET_CACHE by 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:

None only when nothing at all is known (no IPA, no web answer, no respelling, no espeak).

Return type:

Target or None

web_candidates(word, language)[source]

Web transcriptions of word, from the cache or a fresh lookup.

Parameters:
  • word (str) – The word or phrase.

  • language (str) – Language of the text.

Returns:

Possibly empty.

Return type:

list of (ipa, language, source, url)

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:

list of str

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 word up 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.

Parameters:
  • word (str) – The word or phrase.

  • language (str) – ISO-639-1 code of the text being spoken.

  • client (PoliteClient) – The paced client.

Returns:

Every transcription found, best source first.

Return type:

list of (ipa, language, source, url)

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 word when the chunk count and the title’s word count agree, the whole thing when it is a single chunk and word ends the title, and nothing otherwise — a guess is worse than no target.

Parameters:
  • ipa (str) – The transcription, chunks separated by spaces.

  • word (str) – The word (or phrase) being looked up.

  • title (str) – The article’s title.

Returns:

The chunk(s) for word, or "".

Return type:

str

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.

Parameters:

target (Target or None) – The target.

Returns:

asdict(target), or None.

Return type:

dict or None

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:
  • wikitext (str) – The article’s wikitext.

  • edition (str) – The Wikipedia edition ("fr", "en"), the language assumed for {{API|…}}.

Returns:

(ipa, language) pairs, duplicates removed.

Return type:

list of (str, str)

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:
  • wikitext (str) – The page’s wikitext.

  • edition (str) – "fr" or "en" — the two editions use different templates.

Returns:

(ipa, language) pairs in page order, duplicates removed.

Return type:

list of (str, str)

Examples

>>> wiktionary_ipas("{{IPA|en|/ˈfaɪnmən/|/ˈfeɪnmən/}}", "en")
[('ˈfaɪnmən', 'en'), ('ˈfeɪnmən', 'en')]