使用参考表的每个值计算数据表中每个元素的Levenshtein比率,并以最大比率合并 [英] Computing the Levenshtein ratio of each element of a data.table with each value of a reference table and merge with maximum ratio

查看:90
本文介绍了使用参考表的每个值计算数据表中每个元素的Levenshtein比率,并以最大比率合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个data.table dt,其中包含3列:

I have a data.table dt with 3 columns:


  • id

  • name为字符串

  • 阈值为num

示例为:

dt <- <- data.table(nid = c("n1","n2", "n3", "n4"), rname = c("apple", "pear", "banana", "kiwi"), maxr = c(0.5, 0.8, 0.7, 0.6))

nid | rname  | maxr
n1  | apple  |  0.5
n2  | pear   |  0.8
n3  | banana |  0.7
n4  | kiwi   |  0.6

我有第二个表dt.ref有两列:

I have a second table dt.ref with 2 columns:


  • id

  • 名称为字符串

一个示例是:

dt.ref <- <- data.table(cid = c("c1", "c2", "c3", "c4", "c5", "c6"), cname = c("apple", "maple", "peer", "dear", "bonobo", "kiwis"))

cid | cname
c1  | apple
c2  | maple
c3  | peer
c4  | dear
c5  | bonobo
c6  | kiwis

每个 rname > dt ,我想用 dem.ref <的每个 cname 来计算Levenshtein比率。 / em> 这样:

Lr = 1 - (stringdist(cname, rname, method = "lv") / pmax(nchar(cname),nchar(rname)))

然后,我想在 dname 的每个 rname cname 上找到max(Lr) > 并获得以下数据表。

Then, I would like to find max(Lr) over the cname for each rname of dt and get as an output the following data.table:

nid | rname  | maxr | maxLr | cid
n1  | apple  |  0.5 | 1     | c1
n2  | pear   |  0.8 | 0.75  | c3
n2  | pear   |  0.8 | 0.75  | c4
n3  | banana |  0.7 | 0.33  | c5
n4  | kiwi   |  0.6 | 0.8   | c6

基本上,我们采用dt并添加2列,即最大Levenshtein比率和相应的cid,知道

Basically, we take dt and add 2 columns, the maximum Levenshtein ratio and the corresponding cid, knowing that ties are all added, 1 per row as for n2.

我使用 data.table ,但解决方案可以使用 dplyr 或任何其他软件包。

I use data.table but the solution can use dplyr or any other package.

推荐答案

您可以尝试类似

f1 <- function(x, y) {
  require(stringdist)
  require(matrixStats)
  dis  <- stringdistmatrix(x, y, method = "lv")
  mat <- sapply(nchar(y), function(i) pmax(i, nchar(x)))
  r <- 1 - dis / mat
  w <- apply(r, 1, function(x) which(x==max(x)))
  m <- rowMaxs(r)
  list(m = m, w = w)
}

r <- f1(dt[[2]], dt.ref[[2]])
r
$m
[1] 1.0000000 0.7500000 0.3333333 0.8000000

$w
$w[[1]]
[1] 1

$w[[2]]
[1] 3 4

$w[[3]]
[1] 5

$w[[4]]
[1] 6


dt[, maxLr := r$m ]
#dtnew <- dt[rep(1:.N, sapply(r$w, length)),]
dtnew <- dt[rep(1:.N, lengths(r$w),] # thanks to Frank
dtnew[, cid := dt.ref[unlist(r$w), 1]]

结果:

dtnew
   nid  rname maxr     maxLr cid
1:  n1  apple  0.5 1.0000000  c1
2:  n2   pear  0.8 0.7500000  c3
3:  n2   pear  0.8 0.7500000  c4
4:  n3 banana  0.7 0.3333333  c5
5:  n4   kiwi  0.6 0.8000000  c6

这篇关于使用参考表的每个值计算数据表中每个元素的Levenshtein比率,并以最大比率合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆