Algorithm Search⌘K

Proofs: Metaphone

These are the human proofs of Metaphone, at full length.

METAPHONE computes the code that the rule table defines full

This proof shows: correctness.

The specification is the rule table of the page. Let ss be the letters of the input in upper case (line L1). Indices are 0-based. The prepared word ww comes from ss in two steps: (P2) remove each letter that is equal to the letter just before it in ss, unless the letter is C; (P3) if the result starts with KN, GN, PN, AE or WR, remove its first letter. The code of the word is the concatenation, for i=0,1,…,∣w∣−1i = 0, 1, \ldots, |w| - 1, of the output of the first rule in the table for the letter w[i]w[i] whose context condition holds at position ii of ww. Each context condition reads only ww, never the output.

Lemma 1. After line L3, the program holds the prepared word ww.

Proof. Line L2 scans ss from left to right and appends c=s[t]c = s[t] unless the list is non-empty, its last element equals cc, and c≠Cc \ne \mathrm{C}. We show by induction on tt that, after the step for s[t]s[t], the list is the result of P2 on s[0…t]s[0 \ldots t], and its last element equals s[t]s[t]. For t=0t = 0, the list is [s[0]][s[0]]. For t≥1t \ge 1: if s[t]=s[t−1]s[t] = s[t-1] and s[t]≠Cs[t] \ne \mathrm{C}, P2 removes s[t]s[t], and the last element of the list is s[t−1]=s[t]s[t-1] = s[t] by the induction hypothesis, so line L2 does not append it. Otherwise P2 keeps s[t]s[t]. If s[t]≠s[t−1]s[t] \ne s[t-1], then the last element s[t−1]s[t-1] differs from s[t]s[t] and line L2 appends s[t]s[t]. If s[t]=Cs[t] = \mathrm{C}, line L2 appends it in any case. In both cases the last element is now s[t]s[t]. Line L3 is step P3 word for word. □\square

Lemma 2. For each ii, the step of line L4 for position ii appends the output of the first rule of the table for w[i]w[i] whose condition holds at ii.

Proof. The table has a group of rules for each letter, and the rules of one letter are disjoint from those of another letter, so only the group of w[i]w[i] matters. Line L5 handles the vowels: the rule "first letter" outputs the vowel at i=0i = 0, and the next rule outputs nothing. For a consonant, the branch of the program for w[i]w[i] tests the conditions of its group in the order of the table, and it takes the first test that holds. We check each group where a condition is not a direct transcription. For G, the program tests "H follows, and H is neither the last letter nor followed by a vowel", which is the first condition, then "N is the last letter after G, or the rest of the word is NED", then "D before and E, I or Y after", then "E, I or Y after", then the default K. For H, the program tests "C, G, P, S or T before, or W before at position 1", then "a vowel before and no vowel after", then the default H. For W, it tests "position 0 and H after", then "a vowel after", then the default, which outputs nothing. For C, the case "H after" outputs K when S is before and X otherwise, which is the order "S before and H after" then "H after" of the table. The other groups have one or two conditions, and the program tests them in the order of the table. The helper at returns the empty string outside ww, so a test on a letter before position 0 or after position ∣w∣−1|w| - 1 is false, as the table means. □\square

Theorem. METAPHONE returns the code of its input.

Proof. By Lemma 1, the loop of line L4 runs over the prepared word ww. By Lemma 2, each step appends the output of the rule of the table for that position. The steps run for i=0,1,…,∣w∣−1i = 0, 1, \ldots, |w| - 1 in order, and no step changes ww, so line L8 returns the concatenation that defines the code. ■\blacksquare

Each step reads at most three letters after ii and one before it, so the time is linear in the length of the input.

Back to Metaphone