Sounds the same
- Lesson 13
- 30 min
- Module 4: Compare records: similarity measures
Where we are
String measures compare letters. Some names are spelled differently but sound the same. In this lesson, we use Soundex and Metaphone, and we see where they help and where they fail, for example on French names.
Lesson 13 of 29 · Entity Resolution and Record Matching: Is This the Same Thing?Sounds the same
1. The matching problem
2. Clean first, then match exactly
3. Blocking: do not compare everything
4. Compare records: similarity measures
5. Probabilistic linkage
6. Learned matching: classifiers, embeddings and language models
7. From pairs to entities
8. Run matching in production
What you will be able to do
Compare names by how they sound. Compute a Soundex code by hand and compare it with Metaphone. Find where sound codes help, where they fail on French names, and how much they add on TermLarkfieldA made-up Canadian online shop for home and garden products. It is not a real company. All Nextia courses use its help desk and data as the example.Learn more Meet Larkfield’s candidate pairs.
NotebookCompute sound codes in a notebook
The notebook writes Soundex by hand, checks it against the library jellyfish, and measures how often Soundex and Metaphone agree on Larkfield’s labelled pairs. Its setup cells download the data, so it needs no local setup.
Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.
Before you startWhat to know and to have ready
What you need to know
- String similarity, from String similarity.
What you need on your computer
- The project
larkfield-matchwith the files of the last lesson. Or use the notebook only.
Tested on macOS with Python 3.12.13.
A name on the phone
Section titled “A name on the phone”A clinic takes bookings by phone. A patient says her name: “Phillips”. The receptionist types what she hears: “Filips”. The patient’s file, made two years earlier from a form, says “Phillips”.
The two spellings share few letters in the same places. But anyone who reads them aloud hears the same name. The receptionist did not make a typing error. She wrote the sound.
The problem: same sound, different letters
Section titled “The problem: same sound, different letters”String measures compare letters. When a spelling follows the sound, the letters can differ a lot:
match similar Phillips Filips'phillips' / 'filips' levenshtein distance 3 levenshtein 0.625 jaro_winkler 0.819 token_set_ratio 0.714 soundex P412 / F412 different metaphone FLPS / FLPS sameLevenshtein gives 0.625 and Jaro–Winkler 0.819. These are low scores. A threshold that is low enough to accept them also accepts many pairs of different people.
The last line shows another idea. The Metaphone code of both spellings is FLPS.
The idea: a code for the sound
Section titled “The idea: a code for the sound”A sound code (also called a phonetic code) turns a name into a short code that records how it sounds, not how it is spelled. Two names with the same code probably sound alike. Then we compare the codes for equality, as in Module 2.
Sound codes are old. Soundex was invented about a hundred years ago, and the United States used it to index its census records. Metaphone is newer: it has many more rules of English spelling.
Soundex step by step
Section titled “Soundex step by step”Soundex keeps the first letter. It turns each later consonant into a digit. Consonants that sound alike get the same digit: b, f, p and v all become 1. It drops the vowels. The code always has 4 characters.
Text description of the diagram
The Soundex rules, then the letters of two names with the digit under each letter. Robert: R is kept; o is dropped; b is 1; e is dropped; r is 6; t is 3. The code is R163. Rupert: R is kept; u is dropped; p is 1; e is dropped; r is 6; t is 3. The code is also R163.
Soundex gives “Robert” and “Rupert” the same code. That is the price of a short code: it groups names that sound a little alike. Soundex also keeps the first letter, not the first sound. So “Phillips” (P412) and “Filips” (F412) get different codes, although they sound the same.
Metaphone: more spelling rules
Section titled “Metaphone: more spelling rules”Metaphone knows more about English spelling. A few of its rules:
- “ph” sounds like “f”, so it writes
F. - “th” sounds like the start of “thin”, so it writes
0(a zero). - It keeps b and p apart. “Robert” is
RBRTand “Rupert” isRPRT. - It drops most vowels and many silent letters.
So Metaphone gives “Phillips” and “Filips” the same code, FLPS. It separates “Robert” from “Rupert”. It looks better. But its rules are English rules.
Eleven pairs, two codes
Section titled “Eleven pairs, two codes”The project’s measurement script coded eleven pairs of names. Some are one name spelled two ways. Others are two different names that look or sound alike. Accents are removed before coding, as the project does.
Text description of the diagram
For each pair: is it one name spelled two ways, and is each code right? Soundex is right on 8 of 11 pairs and Metaphone on 6. Metaphone is wrong on three French names. It reads the “th” of “Thériault” as the English “th”. It keeps the silent “s” of “Lévesque”. It gives the French first name “Jean” the code of the English name “Gene”. Both codes give the different surnames “Beaulieu” and “Boileau” the same code. Both fail on “Thompson” / “Tomson”.
Why French names break them
Section titled “Why French names break them”Metaphone’s rules describe how English speakers read English spelling. French spelling follows other rules:
- In French, “th” sounds like “t”. So “Thériault” and “Terriault” sound alike, but Metaphone writes
0RLTandTRLT. - French often has silent letters. The “s” of “Lévesque” is silent, so “Leveque” sounds the same. Metaphone keeps the “s”:
LFSKagainstLFK. - French vowels carry much of the sound. “Beaulieu” and “Boileau” sound quite different, but both codes drop the vowels and keep only “B” and “L”.
- “Jean” in French starts with a soft “zh” sound, not the “j” of “Gene”. Metaphone does not know that the name is French.
Larkfield’s customers live mostly in Quebec and Ontario, and many of their names are French. The same problem appears with names from other languages. People write Vietnamese, Arabic or Chinese names in Latin letters in several ways, and an English rule does not know them.
On Larkfield’s candidate pairs
Section titled “On Larkfield’s candidate pairs”The table above shows chosen examples. What happens on real candidate pairs? We took every candidate pair of the five blocking keys whose two surnames are spelled differently (after normalising). There are 150,554 such pairs. For each pair, we checked three things: the same Soundex code, the same Metaphone code, and a Jaro–Winkler score of 0.9 or more.
- One customer (true pairs)
- Two people
Text description of the chart
A dot chart with six rows. Soundex agrees on 0.465 of the one-customer pairs and on 0.167 of the pairs of two people. Metaphone agrees on 0.358 and 0.125. A Jaro–Winkler score of 0.9 or more covers 0.637 and 0.395.
The sound codes catch fewer than half of the misspelt surnames of one customer. Jaro–Winkler at 0.9 catches more of them, but it also passes more pairs of two people.
Why do the codes do so badly here? Look at the errors. Our synthetic records have typing errors: a missing letter, two swapped letters, a key next to the right one. Those are not spellings by sound. The most common misspelt pairs that Metaphone misses are “smit” / “smith”, “ganon” / “gagnon” and “ouelle” / “ouellet”. One missing letter changes the code: “smit” is SMT and “smith” is SM0.
| Surname origin | One-customer pairs | Soundex agrees | Metaphone agrees | Two-people pairs | Soundex agrees | Metaphone agrees |
|---|---|---|---|---|---|---|
| English | 1,403 | 0.459 | 0.354 | 33,038 | 0.289 | 0.217 |
| French | 1,276 | 0.484 | 0.389 | 106,473 | 0.129 | 0.098 |
The origin is the generator’s name list (English, French or other) that holds one of the two spellings.
On these typing errors, the codes do about as well on French surnames as on English ones. The French weakness of the eleven pairs does not show here. Our generator does not make French spellings by sound, such as “Terriault” for “Thériault”. This is a limit of our synthetic data. Real records typed from phone calls or handwriting would have more spellings by sound. Measure on your own data before you trust either result.
Use it in the project
Section titled “Use it in the project”The sound codes are already in larkfield_match/similarity.py, which you copied in the last lesson. match similar prints them on its last two lines:
match similar "Thériault" "Terriault"match similar Beaulieu Boileau'theriault' / 'terriault' levenshtein distance 2 levenshtein 0.778 jaro_winkler 0.933 token_set_ratio 0.889 soundex T643 / T643 same metaphone 0RLT / TRLT different'beaulieu' / 'boileau' levenshtein distance 5 levenshtein 0.375 jaro_winkler 0.782 token_set_ratio 0.533 soundex B400 / B400 same metaphone BL / BL sameThe code removes accents first, then calls the library jellyfish:
def soundex(s): s = strip_accents(s) return jellyfish.soundex(s) if s else ""
def metaphone(s): s = strip_accents(s) return jellyfish.metaphone(s) if s else ""An empty name gets an empty code, never a code of its own. Two empty codes must not count as “the same sound”. The next lesson treats empty values as missing.
In the next lesson, the matcher uses one sound field: last_sound, which is 1 when the two surnames have the same Metaphone code. It is one small piece of evidence among sixteen.
When to use sound codes, and when not
Section titled “When to use sound codes, and when not”| Situation | Sound codes help? | Why |
|---|---|---|
| Names taken by phone, at a desk or from handwriting | yes, as one signal | people write what they hear |
| English names, spelling variants (“Smith”, “Smyth”; “Phillips”, “Filips”) | yes | the codes were made for them |
| Typing errors (a missing or swapped letter) | little | one letter can change the code; Jaro–Winkler does better |
| French and other non-English names | with care | English rules mislead; test on your names first |
| Blocking (a key for candidate pairs) | sometimes | a code makes big blocks: many names share a short code |
| The only evidence for a merge | never | different names share codes (“Beaulieu”, “Boileau”) |
The cost is small: you compute a code once per record, not once per pair. The real cost is false agreement: a code that agrees on two different names adds evidence for a wrong merge.
Common mistake
A sound code as the decision
Symptom: TermTomásLarkfield’s AI engineer. He often suggests a quick shortcut, and the lesson shows why it fails. adds a rule: “same first name, and the same Soundex code of the surname, means the same customer”. He hopes to catch the misspelt surnames. The rule merges Jacques Beaulieu with a Jacques Boileau, and a Robert with a Rupert.
Cause: a 4-character code has few possible values, so many different names share one. Among the 147,410 candidate pairs of two people with different surnames, 0.167 share a Soundex code: about 24,617 pairs. And the code still misses most typing errors: it agrees on only 0.465 of the misspelt surnames of one customer.
Fix: use a sound code as one field of the comparison, with a small weight, next to Jaro–Winkler and the other fields. The next lesson does this. Check the code on your own names, and on each language, before you give it more weight.
Consequence in AI work: a rule made for one population can fail on another. Soundex was made for English names in one census. Test every tool on the groups in your own data, and report results by group, as in What blocking loses.
Knowledge check
Section titled “Knowledge check”Check your understanding
Soundex gives “Phillips” P412 and “Filips” F412. Metaphone gives both FLPS. Explain each result.
Show the answer
Soundex keeps the first letter as it is: “P” and “F”. Then it codes the next consonants: l → 4, p → 1, s → 2, for both names. So the codes differ only in the first letter, and Soundex calls them different. Metaphone has a rule for “ph”: it sounds like “f”, so it writes F. The rest of both names gives L, P, S. So both codes are FLPS.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
On Larkfield's candidate pairs with two different surname spellings, Metaphone agrees on fewer than half of the pairs of one customer. Why so few, when sound codes were made to catch spelling variants?
Show the answer
Most of these spelling differences are typing errors, not spellings by sound: a missing letter, swapped letters or a key next to the right one. A sound code is not made for them, and one missing letter can change the code (“smit” SMT against “smith” SM0). Our synthetic data has typing errors and almost no spellings by sound. So the result says “sound codes do not catch typing errors”, not “sound codes never help”.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
A colleague wants to block on the Soundex code of the surname, in place of the first 4 letters of the surname. What would you expect for the number of candidate pairs and for pair completeness? How would you check?
Show the answer
More candidate pairs, because a Soundex code is coarser than 4 letters: different names such as “Beaulieu” and “Boileau” share a block. Pair completeness may rise a little for spellings by sound and for some typing errors after the first letter. But a typing error in the first letter still splits the pair, as with the 4-letter key. Check it as in Module 3: make the candidate pairs and measure pair completeness and the number of pairs. The block sizes decide the cost, not the idea.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- A sound code records how a name sounds. Two names with the same code probably sound alike.
- Soundex keeps the first letter and 3 digits. Consonants that sound alike share a digit, so “Robert” and “Rupert” both get R163.
- Metaphone has more English spelling rules (“ph” → F, “th” → 0). It is right more often on English names, and wrong on French ones such as “Thériault” / “Terriault”.
- On Larkfield’s candidate pairs, the codes agree on fewer than half of the misspelt surnames of one customer. Our errors are typing errors, not spellings by sound.
- Use a sound code as one small piece of evidence, never as the decision.
We now have similarity measures and sound codes, one field at a time. Next, we compare every field of a pair and combine them into one score.