
Last chance! 50% off unlimited learning
Sale ends in
This page gives an overview of the string dissimilarity measures offered by stringdist.
String metrics are ways of quantifying the dissimilarity between two finite sequences, usually text strings. Over the years, many such measures have been developed. Some are based on a mathematical understanding of the set of all strings that can be composed from a finite alphabet, others are based on more heuristic principles, such as how a text string sounds when pronounced by a native English speaker.
The terms 'string metrics' and 'string distance' are used more or less
interchangibly in literature. From a mathematical point of view, string
metrics often do not obey the demands that are usually required from a
distance function. For example, it is not true for all string metrics that a
distance of 0 means that two strings are the same (e.g. in the
The metric you need to choose for an application strongly depends on both the nature of the string (what does the string represent?) and the cause of dissimilarities between the strings you are measuring. For example, if you are comparing human-typed names that may contain typo's, the Jaro-Winkler distance may be of use. If you are comparing names that were written down after hearing them, a phonetic distance may be a better choice.
Currently, the following distance metrics are supported by stringdist.
Method name | Description |
osa |
Optimal string aligment, (restricted Damerau-Levenshtein distance). |
lv |
Levenshtein distance (as in R's native adist ). |
dl |
Full Damerau-Levenshtein distance. |
hamming |
Hamming distance (a and b must have same nr of characters). |
lcs |
Longest common substring distance. |
qgram |
|
cosine |
cosine distance between |
jaccard |
Jaccard distance between |
jw |
Jaro, or Jaro-Winker distance. |
See Van der Loo (2014) for an extensive description and references. The review papers of Navarro (2001) and Boytsov (2011) provide excellent technical overviews of respectively online and offline string matching algorithms.
The Hamming distance (method='hamming'
) counts the number of
character substitutions that turns b
into a
. If a
and b
have different number of characters the distance is Inf
.
The Levenshtein distance (method='lv'
) counts the number of
deletions, insertions and substitutions necessary to turn b
into
a
. This method is equivalent to R
's native adist
function.
The Optimal String Alignment distance (method='osa'
) is like the Levenshtein
distance but also allows transposition of adjacent characters. Here, each
substring may be edited only once. (For example, a character cannot be transposed twice
to move it forward in the string).
The full Damerau-Levenshtein distance (method='dl'
) is like the optimal
string alignment distance except that it allows for multiple edits on substrings.
The longest common substring (method='lcs') is defined as the longest string that can be
obtained by pairing characters from a
and b
while keeping the order
of characters intact. The lcs-distance is defined as the number of unpaired characters.
The distance is equivalent to the edit distance allowing only deletions and insertions,
each with weight one.
A a
(b
), the q
is is larger than the length of
any of the strings. In that case Inf
is returned.
The cosine distance (method='cosine') is computed as
Let a
and b
. The Jaccard distance (method='jaccard'
) is given by
The Jaro distance (method='jw'
, p=0
), is a number
between 0 (exact match) and 1 (completely dissimilar) measuring
dissimilarity between strings. It is defined to be 0 when both strings have
length 0, and 1 when there are no character matches between a
and
b
. Otherwise, the Jaro distance is defined as
a
, a
, characters in b
and with transpositions. A character
a
matches a character from b
when b
, and the index of a
differs less than
b
. Two matching characters are transposed when they are
matched but they occur in different order in string a
and b
.
The Jaro-Winkler distance (method=jw
, 0<p<=0.25
) adds a
correction term to the Jaro-distance. It is defined as
For the soundex distance (method='soundex'), strings are translated to a soundex code
(see phonetic
for a specification). The
distance between strings is 0 when they have the same soundex code,
otherwise 1. Note that soundex recoding is only meaningful for characters
in the ranges a-z and A-Z. A warning is emitted when non-printable or non-ascii
characters are encountered. Also see printable_ascii
.
MPJ van der Loo (2014) The stringdist package for approximate string matching. The R Journal 6(1) 111-122.
L. Boytsov (2011). Indexing methods for approximate dictionary searching: comparative analyses. ACM Journal of experimental algorithmics 16 1-88.
G. Navarro (2001). A guided tour to approximate string matching. ACM Computing Surveys 33 31-88.
Functions applying string metrics to text: stringdist
,
stringdistmatrix
, amatch
Functions applying string metrics to integer sequences:
seq_dist
, seq_distmatrix
, seq_amatch
Encoding issues: stringdist-encoding