Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching strings with abbreviations; fuzzy matching

I am having trouble matching character strings. Most of the difficulty centers on abbreviation

I have two character vectors. I am trying to match words in vector A (typos) to the closes match in vector B.

vec.a <- c("ce", "amer", "principl")

vec.b <- c("ceo", "american", "principal")

My first crack at this was by using stringdist package fuzzy matching command. However, I can only push it so far.

amatch(vec.a, vec.b, maxDist = 3)
[1] 1 1 3

The amatch/fuzzy matching works wonderful for typos: in this case, ce -> ceo and principl -> principal. The problem arises with abbreviations. amer should be matched with american, but ce is a closer match--on account that less permutations are needed to match. How can I deal with fuzzy matching under the presence of abbreviations?

like image 641
YouLocalRUser Avatar asked Oct 15 '25 06:10

YouLocalRUser


2 Answers

Changing the dissimilarity measure to the Jaro distance or Jaro-Winkler distance works for the example provided in your question.

library(stringdist)

vec.a <- c("ce", "amer", "principl")
vec.b <- c("ceo", "american", "principal")

amatch(vec.a, vec.b, maxDist = 1, method = "jw", p = 0) # Jaro
#> [1] 1 2 3
amatch(vec.a, vec.b, maxDist = 1, method = "jw", p = .2) # Jaro-Winkler
#> [1] 1 2 3
like image 194
Till Avatar answered Oct 17 '25 20:10

Till


Maybe agrep is what the question is asking for.

vec.a <- c("ce", "amer", "principl")
vec.b <- c("ceo", "american", "principal")

sapply(vec.a, \(x){
    out <- agrep(x, vec.b)
    ifelse(length(out) > 0L, out, 0L)
})
#>       ce     amer principl 
#>        1        2        3

Created on 2022-03-07 by the reprex package (v2.0.1)

like image 41
Rui Barradas Avatar answered Oct 17 '25 22:10

Rui Barradas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!