speaker_helper.eardrum.phones module
Phones: cutting IPA into units, and how far apart two of them sound.
Module summary
The Ralph Eardrum Loop compares what an engine said with what it should
have said, both as IPA. A plain string comparison would be useless: the
phoneme recogniser writes s a i n m a n where Wiktionary writes
ˈfaɪnmən, and those are the same name said by a French voice. This module
makes the comparison meaningful:
tokenize()cuts IPA into phones, dropping what carries no segment (stress marks, length, syllable dots, ties, slashes);phone_distance()says how different two phones sound, from a small articulatory feature table (height / backness / rounding / nasality for vowels; place / manner / voicing for consonants), so /e/ against /ɛ/ costs little and /e/ against /k/ costs everything;align()runs a weighted edit distance and keeps the alignment;word_distance()finds the target word inside a whole carrier sentence (“Je vous parle de Feynman aujourd’hui.”) and scores only that region, so the recogniser’s noise on the surrounding words does not count against it.
Pure Python, no model, no network: everything here is unit-testable.
Usage example
>>> tokenize("ˈfaɪn.mən")
['f', 'a', 'ɪ', 'n', 'm', 'ə', 'n']
>>> round(phone_distance("e", "ɛ"), 2)
0.2
>>> word_distance("ʒ v u p a l d ə f a j n m a n".split(),
... "ʒvupaʁldə", ["faɪnmən"], "").score
0.1
- class speaker_helper.eardrum.phones.Alignment(cost, pairs=<factory>)[source]
Bases:
objectA weighted edit-distance alignment between two phone sequences.
- pairs
(i, j, cost)per operation, in order:iindexes the heard sequence andjthe target one;Noneon one side marks an insertion (j is None) or a deletion (i is None).
- speaker_helper.eardrum.phones.INDEL_COST = 1.0
Cost of inserting or deleting an ordinary phone.
- class speaker_helper.eardrum.phones.WordScore(score, heard, heard_indices, target)[source]
Bases:
objectHow close a word, heard inside a carrier sentence, is to its target.
- score
Alignment cost on the word’s region divided by the target word’s length:
0is a perfect match,≥ 1means nothing in common.- Type:
- speaker_helper.eardrum.phones.align(heard, target)[source]
Align what was heard with what was meant, at the lowest cost.
Classic Needleman-Wunsch with
phone_distance()as the substitution cost andindel_cost()for gaps.- Parameters:
- Returns:
The total cost and the operations that achieve it.
- Return type:
Examples
>>> align(["a", "b"], ["a", "p"]).cost 0.2
- speaker_helper.eardrum.phones.describe_differences(heard, target)[source]
What went wrong, phone by phone, in words a language model can act on.
A distance says how far; a respelling is fixed by knowing what: a sound said instead of another, a sound too many, a sound missing.
- Parameters:
- Returns:
One short sentence per difference, in reading order; empty when the two match.
- Return type:
Examples
>>> describe_differences(["n", "i", "t", "ʃ", "ɛ", "ʁ"], ["n", "i", "t", "ʃ", "ə"]) ['/ɛ/ said where /ə/ was wanted', 'extra /ʁ/ at the end']
- speaker_helper.eardrum.phones.indel_cost(phone)[source]
Cost of inserting or deleting
phone.- Parameters:
phone (str) – A tokenised phone.
- Returns:
Half a phone for a mute e, a glottal stop or /h/; one phone otherwise.
- Return type:
Examples
>>> indel_cost("ə"), indel_cost("k") (0.5, 1.0)
- speaker_helper.eardrum.phones.phone_distance(a, b)[source]
How different two phones sound, from 0 (same) to 1 (unrelated).
- Parameters:
- Returns:
0.0for the same phone; a small value for neighbours (/e/–/ɛ/, /t/–/d/, any two r’s);1.0for a vowel against a consonant or for a symbol the tables do not know that differs from the other.- Return type:
Examples
>>> phone_distance("a", "a") 0.0 >>> round(phone_distance("t", "d"), 2) 0.2 >>> phone_distance("ʁ", "r") 0.1 >>> phone_distance("a", "k") 1.0
- speaker_helper.eardrum.phones.sentence_distances(heard, word_ipas)[source]
Score every word of a sentence in one alignment.
The listen-all pass hears a whole sentence and needs to know which of its words came out far from espeak’s reading: the sentence is aligned once against the concatenated words, and each word is scored on its own region, like
word_distance()does for a single word.- Parameters:
- Returns:
One score per word (
0.0for a word with no phones, such as a stray punctuation token).- Return type:
Examples
>>> sentence_distances("b ɔ̃ ʒ u ʁ".split(), ["bɔ̃", "ʒuʁ"]) [0.0, 0.0]
- speaker_helper.eardrum.phones.tokenize(ipa)[source]
Cut an IPA string into phones.
A phone is one base letter plus the combining marks that change it (only the nasal tilde is kept). Everything that is not a segment — stress, length, syllable boundaries, ties, slashes, spaces — is dropped, so
/ˈfaɪn.mən/andf a ɪ n m ə ntokenise identically. Diphthongs and affricates come out as their two halves, on both sides of a comparison alike.- Parameters:
ipa (str) – Any IPA transcription: Wiktionary’s, espeak-ng’s, the recogniser’s.
- Returns:
The phones, in order.
- Return type:
Examples
>>> tokenize("/ɑ̃.ʁi/") ['ɑ̃', 'ʁ', 'i'] >>> tokenize("t͡ʃ") ['t', 'ʃ']
- speaker_helper.eardrum.phones.word_distance(heard, prefix_ipa, target_variants, suffix_ipa)[source]
Score one word heard inside a carrier sentence.
The whole sentence is aligned (prefix + word + suffix), because the recogniser has no idea where the word starts; only the operations that touch the word’s region are then counted. When the target has several accepted pronunciations (Wiktionary often lists two), the best one wins.
- Parameters:
- Returns:
The best variant’s score and the phones heard in the word’s region.
- Return type:
- Raises:
ValueError – When
target_variantsis empty.