Calculating string similarity as a percentage

后端 未结 3 705
有刺的猬
有刺的猬 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:17

    Something like this might work:

    d <- data.frame(original = c("abcd", "defg", "hij"), new = c("abce", "zxyv", "hijk"))
    d$dist <- stringdist(d$original, d$new, method = "lv")
    d$similarity <- 1 - d$dist / nchar(as.character(d$original))
    
    #### Returns:
    ####   original  new dist similarity
    #### 1     abcd abce    1  0.7500000
    #### 2     defg zxyv    4  0.0000000
    #### 3      hij hijk    1  0.6666667
    

提交回复
热议问题