stringdist
Approximate string matching and string distance functions.
String Distance
library(stringdist)
# Single comparison
stringdist("hello", "hallo")
# Multiple comparisons
stringdist(c("hello", "world"), c("hallo", "word"))
Distance Methods
# Levenshtein (default)
stringdist("hello", "hallo", method = "lv")
# Optimal string alignment
stringdist("hello", "hallo", method = "osa")
# Damerau-Levenshtein
stringdist("hello", "hallo", method = "dl")
# Longest common substring
stringdist("hello", "hallo", method = "lcs")
# q-gram
stringdist("hello", "hallo", method = "qgram", q = 2)
# Cosine
stringdist("hello", "hallo", method = "cosine", q = 2)
# Jaccard
stringdist("hello", "hallo", method = "jaccard", q = 2)
# Jaro
stringdist("hello", "hallo", method = "jw")
# Jaro-Winkler
stringdist("hello", "hallo", method = "jw", p = 0.1)
# Soundex
stringdist("hello", "hallo", method = "soundex")
Distance Matrix
# Compute distance matrix
strings <- c("hello", "hallo", "hullo", "world")
stringdistmatrix(strings)
# Between two sets
stringdistmatrix(c("hello", "world"), c("hallo", "word"))
Approximate Matching
# Find closest match
amatch("hello", c("hallo", "world", "help"), maxDist = 2)
# All matches within distance
ain("hello", c("hallo", "world", "help"), maxDist = 2)
String Grouping
# Group similar strings
strings <- c("hello", "hallo", "hullo", "world", "word")
stringdist_grouping <- function(x, maxDist = 2) {
d <- stringdistmatrix(x)
hc <- hclust(as.dist(d))
cutree(hc, h = maxDist)
}
Phonetic Coding
# Soundex
phonetic("hello", method = "soundex")
# Metaphone
phonetic("hello", method = "metaphone")
Q-grams
# Get q-grams
qgrams("hello", q = 2)
# Q-gram table
qgrams(c("hello", "world"), q = 2)
Sequence Alignment
# Get alignment
seq_dist("hello", "hallo", method = "lv")
seq_distmatrix(c("hello", "world"), c("hallo", "word"))