Approximate string-matching algorithms, part 2

Part 1, Part 2

Similarity metric

Other procedures discussed here use different types of textual similarity metric. They can be used in a fault-tolerant search.

Most of those procedures calculate the similarity of two strings as number between 0 and 1. Value 0 means the strings are completely different. Value 1 means perfect match of the strings. Intermediate values correspond to a partial match.

The similarity metric algorithms are useful for spell checking, speech recognition, DNA analysis, plagiarism detection, to find difference between files and for remote screen update problem.

Hamming distance

For two strings of the same length, the Hamming distance is the number of positions in which the two strings have different characters.

  1. Hamming distance from ALEXANDRE to ALEKSANDR is 6 (ALE coincide, the resting 6 characters are different).

Edit distance

The edit distance of two strings is defined as the minimum number of insertions, deletions and substitutions required to transform the first string into the second. The distance 0 means the strings are identical.

The algorithm was devised in 1965 by the sovietic scientist Vladimir Levenshtein. The edit distance is called also Levenshtein distance.

There are modern versions of Edit distance algorithm assigning different weights to insertions, deletions and substitutions.

  1. Edit distance from ALEXANDRE to ALEKSANDER is 4 (substitute X by K, insert S after K, insert E after D, delete E at the end).


The trigram similarity between two strings is determined by the number of matching letter triples in both strings. The algorithm can be generalized to n-grams, where n=1, 2, ...

The strings string1 and string2 are padded to left and to right by one space symbol. Then the strings are divided into letter triplets (trigrams). Finally, the similarity is calculated as:

s = 2*m / (a + b).


m is number of matching trigrams
a is number of trigrams in string1
b is number of trigrams in string2

  1. The similarity of ALEXANDRE and ALEKSANDER is 2*3 / (9+10) = 0.32 (matching _AL, ALE, AND).

Ratcliff/Obershelp pattern recognition

The Ratcliff/Obershelp algorithm computes the similarity of two strings as the doubled number of matching characters divided by the total number of characters in the two strings. Matching characters are those in the longest common subsequence plus, recursively, matching characters in the unmatched region on either side of the longest common subsequence.

  1. The similarity of ALEXANDRE and ALEKSANDER is 2 * (3+3+1+1) / (9+10) = 0.84 (matching ALE, AND, E, R).


The Jaro-Winkler similarity was developed at the U.S. Census and used in post-enumeration analysis.

Given strings string1 and string2, their similarity is:

s = m/3a + m/3b + (m-t)/3m.


m is number of matching characters
a is length of string1
b is length of string2
t is number of transpositions

Two characters are considered matched only if they are no further apart than (max(a,b)/2 - 1). The first matched character on string1 is compared to the first matched character on string2; the second matched character on string1 is compared to the second matched character on string2, etc. The number of mismatched characters is divided by two to yield the number of transpositions.

The improved Jaro-Winkler method uses weights different from 1/3. It also penalizes less some types of error: visual scanning and keypunch errors, errors at the end of a string.

  1. The similarity of ALEXANDRE and ALEKSANDER is (8/9 + 8/10 + (8-1)/8) / 3 = 0.85 (matching A, L, E, A, N, D, R, E; 1 transposition).

Typewriting errors

There are some algorithms taking into account errors resulting from the pressing of erroneous buttons on the keyboard: V instead of B, 6 instead of Y etc.

Comparison of algorithms

The choice of one from the following string-matching algorithms mainly depends on the nature of error influencing the text data.

Algorithm Type When to use
Soundex phonetic misspelled English words
Daitch-Mokotoff phonetic differently spelled European surnames
NYSIIS phonetic misspelled English and some foreign words
Metaphone, Double metaphone phonetic misspelled English words
Caverphone 2.0 phonetic misspelled English words
Hamming, Edit distance similarity local spelling errors
Trigram, n-gram similarity spelling errors, edited text
Ratcliff/Obershelp similarity spelling errors, edited text
Jaro-Winkler similarity spelling and typewriting errors


Handbooks, lectures

  1. The dictionary of algorithms and data structures.
  2. Handbook of algorithms and data structures by Gaston H. Gonnet and Ricardo Baeza-Yates.
  3. Exact string matching algorithms: lectures of Laboratoire d'Informatique de Rouen, France.


  1. Soundex reference.
  2. Usage of Soundex and NYSIIS in NameSearch system.
  3. Article on Soundex and NYSIIS by Andrew Coates.
  4. Introduction to Daitch-Mokotoff soundex system by Gary Mokotoff.
  5. Description of Caverphone 2.0.
  6. Metaphone reference.
  7. Article by Reinhard Rapp on Trigram method.
  8. Article by Bruce L. Lambert on usage of N-gram and Edit distance in drug naming.
  9. Paper by William E. Winkler and Yves Thibaudeau on Jaro-Winkler string comparator.
  10. Articles on Fuzzy string searching, Phonetic algorithm, Soundex, NYSIIS, Metaphone, Daitch-Mokotoff Soundex, Levenshtein distance and Trigram search in Wikipedia, the free encyclopedia.
  11. Article on SQL Server 2005 Fuzzy Lookup and Grouping technology on MSDN Magazine.


  1. Implementation of Soundex in C.
  2. Implementation of Metaphone and Double metaphone in Basic, C, Perl, and C++.
  3. Implementation of Edit distance in Java, C++ and Visual Basic.
  4. Dynamic programming algorithm for Edit distance.
  5. Fuzzy string search in TCL.
  6. Description of PHP string functions levenshtein, soundex, similar_text and metaphone.
  7. Fuzzy Matching & Deduplication SourceGorge project.

Approximate string-matching algorithms
Part 1, Part 2