R:带有矢量模式的 agrep

标签 r fuzzy-search agrep

我有一个模式向量,需要对它们使用 agrep。问题是 agrep 似乎一次只采用一种模式。

patt <- c("test","10 Barrel")
lut  <- c("1 Barrel","10 Barrel Brewing","Harpoon 100 Barrel Series","resr","rest","tesr")

for (i in 1:length(patt)) {
  print(agrep(patt[i],lut,max=1,v=T))
}

结果:

[1] "rest" "tesr"
[1] "1 Barrel"                  "10 Barrel Brewing"         "Harpoon 100 Barrel Series"

for 在长模式上很慢,因此尝试以矢量化形式进行:

VecMatch1 = function(string, stringVector){
  stringVector[agrep(string, stringVector, max = 1)]
}
a = VecMatch1(patt,lut)

Warning message:
In agrep(string, stringVector, max = 1) :
  argument 'pattern' has length > 1 and only the first element will be used

lapply 之类的功能可能有帮助吗?谢谢!!

最佳答案

使用lapply:

lapply(patt, agrep, x=lut, max.distance=c(cost=1, all=1), value=TRUE)

[[1]]
[1] "rest" "tesr"

[[2]]
[1] "1 Barrel"                  "10 Barrel Brewing"         "Harpoon 100 Barrel Series"

您可能可以使用 dplyr 或 data.table 获得更快的性能。

关于R:带有矢量模式的 agrep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31435313/

相关文章:

r - 将不同长度的列表组合成数据帧

iphone - 解析词典并使用外卡显示大量匹配项的最佳方法是什么

mysql - 不精确搜索最近的时间值

r - R 中的 agrep - 在字符串中查找 *all* 匹配项(全局标志)

java - 多次使用 agrep.exe 的 Java ProcessBuilder

r - 在 R 中拟合零膨胀泊松分布

r - 帮助在R中为kernlab的SVM使用predict()吗?

R 传单 map : Include multiple rows of data in label popup

lucene - 像这样模糊和像这样模糊之间的区别?

c++ - 有效地检查一个字符串是否是(大约包含在)另一个字符串的近似子字符串,直到给定的错误阈值?