The R Project SVN R

Rev

Rev 68948 | Rev 79670 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
60324 ripley 1
% File src/library/utils/man/adist.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
67570 ripley 3
% Copyright 2011-2015 R Core Team
60324 ripley 4
% Distributed under GPL 2 or later
5
 
56896 hornik 6
\name{adist}
7
\alias{adist}
8
\title{Approximate String Distances}
9
\description{
10
  Compute the approximate string distance between character vectors.
11
  The distance is a generalized Levenshtein (edit) distance, giving the
12
  minimal possibly weighted number of insertions, deletions and
13
  substitutions needed to transform one string into another.
14
}
15
\usage{
57074 hornik 16
adist(x, y = NULL, costs = NULL, counts = FALSE, fixed = TRUE,
56910 hornik 17
      partial = !fixed, ignore.case = FALSE, useBytes = FALSE)
56896 hornik 18
}
19
\arguments{
60324 ripley 20
  \item{x}{a character vector.  \link{Long vectors} are not supported.}
57012 hornik 21
  \item{y}{a character vector, or \code{NULL} (default) indicating
22
    taking \code{x} as \code{y}.}
57074 hornik 23
  \item{costs}{a numeric vector or list with names partially matching
56896 hornik 24
    \samp{insertions}, \samp{deletions} and \samp{substitutions} giving
25
    the respective costs for computing the Levenshtein distance, or
26
    \code{NULL} (default) indicating using unit cost for all three
57012 hornik 27
    possible transformations.}
56896 hornik 28
  \item{counts}{a logical indicating whether to optionally return the
29
    transformation counts (numbers of insertions, deletions and
30
    substitutions) as the \code{"counts"} attribute of the return
31
    value.}
56910 hornik 32
  \item{fixed}{a logical.  If \code{TRUE} (default), the \code{x}
33
    elements are used as string literals.  Otherwise, they are taken as
34
    regular expressions and \code{partial = TRUE} is implied
35
    (corresponding to the approximate string distance used by
74363 maechler 36
    \code{\link{agrep}} with \code{fixed = FALSE}).}
56896 hornik 37
  \item{partial}{a logical indicating whether the transformed \code{x}
38
    elements must exactly match the complete \code{y} elements, or only
39
    substrings of these.  The latter corresponds to the approximate
40
    string distance used by \code{\link{agrep}} (by default).}
41
  \item{ignore.case}{a logical.  If \code{TRUE}, case is ignored for
42
    computing the distances.}
43
  \item{useBytes}{a logical.  If \code{TRUE} distance computations are
44
    done byte-by-byte rather than character-by-character.}
45
}
46
\value{
47
  A matrix with the approximate string distances of the elements of
48
  \code{x} and \code{y}, with rows and columns corresponding to \code{x}
57012 hornik 49
  and \code{y}, respectively.
50
 
51
  If \code{counts} is \code{TRUE}, the transformation counts are
52
  returned as the \code{"counts"} attribute of this matrix, as a
53
  3-dimensional array with dimensions corresponding to the elements of
54
  \code{x}, the elements of \code{y}, and the type of transformation
57024 hornik 55
  (insertions, deletions and substitutions), respectively.
56
  Additionally, if \code{partial = FALSE}, the transformation sequences
57
  are returned as the \code{"trafos"} attribute of the return value, as
58
  character strings with elements \samp{M}, \samp{I}, \samp{D} and
59
  \samp{S} indicating a match, insertion, deletion and substitution,
63698 ripley 60
  respectively.  If \code{partial = TRUE}, the offsets (positions of
57024 hornik 61
  the first and last element) of the matched substrings are returned as
62
  the \code{"offsets"} attribute of the return value (with both offsets
63
  \eqn{-1} in case of no match).
56896 hornik 64
}
65
\details{
57012 hornik 66
  The (generalized) Levenshtein (or edit) distance between two strings
67
  \var{s} and \var{t} is the minimal possibly weighted number of
68
  insertions, deletions and substitutions needed to transform \var{s}
69
  into \var{t} (so that the transformation exactly matches \var{t}).
70
  This distance is computed for \code{partial = FALSE}, currently using
71
  a dynamic programming algorithm (see, e.g.,
68939 ripley 72
  \url{https://en.wikipedia.org/wiki/Levenshtein_distance}) with space
57012 hornik 73
  and time complexity \eqn{O(mn)}, where \eqn{m} and \eqn{n} are the
74
  lengths of \var{s} and \var{t}, respectively.  Additionally computing
75
  the transformation sequence and counts is \eqn{O(\max(m, n))}.
76
 
77
  The generalized Levenshtein distance can also be used for approximate
78
  (fuzzy) string matching, in which case one finds the substring of
79
  \var{t} with minimal distance to the pattern \var{s} (which could be
80
  taken as a regular expression, in which case the principle of using
81
  the leftmost and longest match applies), see, e.g.,
68939 ripley 82
  \url{https://en.wikipedia.org/wiki/Approximate_string_matching}.  This
57012 hornik 83
  distance is computed for \code{partial = TRUE} using \samp{tre} by
67384 hornik 84
  Ville Laurikari (\url{http://laurikari.net/tre/}) and
57012 hornik 85
  corresponds to the distance used by \code{\link{agrep}}.  In this
86
  case, the given cost values are coerced to integer.
87
 
88
  Note that the costs for insertions and deletions can be different, in
89
  which case the distance between \var{s} and \var{t} can be different
90
  from the distance between \var{t} and \var{s}.
56896 hornik 91
}
92
\seealso{
93
  \code{\link{agrep}} for approximate string matching (fuzzy matching)
94
  using the generalized Levenshtein distance.
95
}
96
\examples{
68939 ripley 97
## Cf. https://en.wikipedia.org/wiki/Levenshtein_distance
56896 hornik 98
adist("kitten", "sitting")
99
## To see the transformation counts for the Levenshtein distance:
100
drop(attr(adist("kitten", "sitting", counts = TRUE), "counts"))
57012 hornik 101
## To see the transformation sequences:
102
attr(adist(c("kitten", "sitting"), counts = TRUE), "trafos")
56896 hornik 103
 
104
## Cf. the examples for agrep:
105
adist("lasy", "1 lazy 2")
106
## For a "partial approximate match" (as used for agrep):
107
adist("lasy", "1 lazy 2", partial = TRUE)
108
}
109
\keyword{character}