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

Author

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

class speaker_helper.eardrum.phones.Alignment(cost, pairs=<factory>)[source]

Bases: object

A weighted edit-distance alignment between two phone sequences.

Parameters:
cost

Total cost of the alignment.

Type:

float

pairs

(i, j, cost) per operation, in order: i indexes the heard sequence and j the target one; None on one side marks an insertion (j is None) or a deletion (i is None).

Type:

list of tuple

cost: float
pairs: list[tuple[int | None, int | None, float]]
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: object

How close a word, heard inside a carrier sentence, is to its target.

Parameters:
score

Alignment cost on the word’s region divided by the target word’s length: 0 is a perfect match, ≥ 1 means nothing in common.

Type:

float

heard

The phones the recogniser produced for the word’s region.

Type:

list of str

heard_indices

Their indices in the heard sequence (to cut the audio clip).

Type:

list of int

target

The target variant this score was measured against.

Type:

list of str

heard: list[str]
heard_indices: list[int]
score: float
target: list[str]
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 and indel_cost() for gaps.

Parameters:
  • heard (list of str) – Phones the recogniser produced.

  • target (list of str) – Phones that should have been said.

Returns:

The total cost and the operations that achieve it.

Return type:

Alignment

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:
  • heard (list of str) – Phones heard for the word.

  • target (list of str) – Phones it should have had.

Returns:

One short sentence per difference, in reading order; empty when the two match.

Return type:

list of str

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:

float

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:
  • a (str) – Two tokenised phones.

  • b (str) – Two tokenised phones.

Returns:

0.0 for the same phone; a small value for neighbours (/e/–/ɛ/, /t/–/d/, any two r’s); 1.0 for a vowel against a consonant or for a symbol the tables do not know that differs from the other.

Return type:

float

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:
  • heard (list of str) – Phones recognised for the sentence.

  • word_ipas (list of str) – Expected IPA of each word, in order.

Returns:

One score per word (0.0 for a word with no phones, such as a stray punctuation token).

Return type:

list of float

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/ and f a ɪ n m ə n tokenise 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:

list of str

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:
  • heard (list of str) – Phones recognised for the whole carrier sentence.

  • prefix_ipa (str) – IPA of the carrier around the word (from espeak-ng).

  • suffix_ipa (str) – IPA of the carrier around the word (from espeak-ng).

  • target_variants (list of str) – Accepted IPA transcriptions of the word itself.

Returns:

The best variant’s score and the phones heard in the word’s region.

Return type:

WordScore

Raises:

ValueError – When target_variants is empty.