Calculating string similarity as a percentage

后端 未结 3 704
有刺的猬
有刺的猬 2021-01-21 01:02

The given function uses \"stringdist\" package in R and tells the minimum changes needed to change one string to another. I wish to find out how much similar is one string to an

3条回答
  •  隐瞒了意图╮
    2021-01-21 01:08

    Here is a function in base R. I added a check for vectors of equal length as inputs. You could change this logic if desired.

    strSim <- function(v1, v2) {
                if(length(v1) == length(v2)) 1 - (adist(v1, v2) / pmax(nchar(v1), nchar(v2)))
                else stop("vector lengths not equal")}
    

    this returns

    strSim("abc", "abcd")
         [,1]
    [1,] 0.75
    

提交回复
热议问题