Zum Inhalt springen

Lektion 10 · Duplicates and Identifiers

Fuzzy Matching and Record Linkage

Match near-duplicate records with normalization, string similarity, blocking and scoring thresholds, and send uncertain pairs to human review in Python.

Jon Smith, 12 High St. and John Smith, 12 High Street are probably the same person. Or they could be two relatives. Matching records that are similar but not identical is called record linkage, entity resolution or fuzzy matching. It involves uncertainty, so your goal is to manage it, not to pretend it is gone.

The pipeline

flowchart LR
  A["Normalize"] --> B["Block"]
  B --> C["Score pairs"]
  C --> D["Match"]
  C --> E["Review"]
  C --> F["No match"]

Only plausible pairs are scored. Uncertain scores go to a person.

Step 1: normalize

Simple cleanup often turns near matches into exact ones: use one letter case for comparison, trim spaces, remove punctuation and expand abbreviations that are safe in your context.

Python Normalize names and addresses

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line shows the risk: "St. Mary" means Saint, but the rule turned it into "street". Rules that seem obvious break on real text. Test them on many examples.

Step 2: measure similarity

Python's difflib.SequenceMatcher gives a ratio from 0 to 1 based on matching blocks of characters. Other common measures include edit distance (the number of single-character changes) and token overlap (shared words).

Python Compare two similarity measures

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Swapped word order scores perfectly on words but much lower on characters. acme tools ltd and acme foods ltd look very close by characters, yet they are likely different companies. No single measure is reliable alone.

Step 3: block

Comparing every record with every other grows quickly: 100,000 records make about 5 billion pairs. Blocking compares only records that share a cheap key, such as postal code or the first letters of a surname. A poor block key hides true matches, so use several blocking passes if one key may contain errors.

Step 4: score with several fields

Combine evidence. A shared date of birth or email often counts for much more than a shared first name. Then use two thresholds: above the high one, accept. Below the low one, reject. In between, send to review.

Python Block, score and sort into decisions

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The weights and thresholds are illustrative. Choose yours by labeling a sample of pairs by hand, then checking how many accepted matches are wrong (false positives) and how many true matches were missed (false negatives). Precision is the share of accepted matches that are correct. Recall is the share of true matches you found.

Which error is worse?

It depends. Wrongly merging two patients or bank customers can expose private data or cause harm, so set a strict match threshold and require review. Missing a duplicate on a newsletter list is usually a smaller problem.

Watch transitive matches

If A matches B and B matches C, grouping them also joins A and C, even if A and C look nothing alike. Long chains can merge many unrelated records. Inspect large groups.

Merge reversibly

Do not overwrite records. Keep every original and add a table that maps each source ID to a cluster ID. A wrong merge can then be undone.

Common mistake: pasting names and addresses into an online AI tool to ask whether they match. That shares personal data with another service. Do it only if your rules allow it, and verify any suggestion.

Build a labeled sample

You cannot judge a matcher by looking at a few pairs that you like. Take a sample of candidate pairs across the whole range of scores, including low ones, and have someone label each as "same", "different" or "unsure" without seeing the score. Use clear instructions so two people would usually agree.

Then try different thresholds against the labels. Raising the match threshold usually improves precision and lowers recall. Lowering it does the opposite. Pick the balance that fits the cost of each error. Keep some labeled pairs aside while you tune, so that you can test the final rules on pairs they were not adjusted to.

Remember that pairs lost in blocking never receive a score. To estimate how many you miss, try a second, very different blocking rule on a sample and see whether it finds new matches.

Help reviewers decide

A review screen should show the two records side by side, highlight the differences and offer "unsure" as an answer. Record who decided and when. Reviewer decisions are valuable data: reuse them so nobody judges the same pair twice, and study them to find new normalization rules.

Matching quality can differ between groups. Naming customs vary, and rules tuned on one language may merge or split names from another unfairly. Check error rates for different kinds of names, not only the overall score.

Start with exact steps

Run the cheap, reliable matches first. Records that share a trusted unique ID can usually be linked without any similarity score. Then match on a combination of normalized fields. Use fuzzy scoring only for what remains. This order reduces the number of uncertain pairs and makes each decision easier to explain.

Store the rule that produced each link, such as same_id, exact_normalized or reviewed_fuzzy, together with the score. If a rule later turns out to be wrong, you can find and undo only the links it created, without touching the others.

Keep checking after launch

New data can behave differently from your labeled sample, for example after a new source or country is added. From time to time, review a fresh sample of automatic matches and rejections. If errors rise, revisit the normalization rules, weights and thresholds, then test them again.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What does blocking do?

    Blocking cuts the number of pairs to compare, at the risk of missing matches across blocks.
  2. 2. Why use two thresholds?

    Clear matches and clear non-matches are automatic, and the gray zone gets judgment.
  3. 3. Records A-B and B-C are matched. What is the risk?

    Chaining pairwise matches can pull unrelated records into one cluster.

Summary

  • Normalize, block, score and then decide.
  • Combine several fields and similarity measures.
  • Tune thresholds on hand-labeled pairs.
  • Keep originals and make merges reversible.

Next: Keys and Identifiers

Dein Fortschritt wird in diesem Browser gespeichert. Kein Konto nötig.