【问题标题】:Fuzzy matching between two lists - improving double loop for efficiency两个列表之间的模糊匹配——提高双循环效率
【发布时间】:2015-07-29 15:17:13
【问题描述】:

我正在寻找一些关于以下 MWE 目标的最佳返工实现的建议,该目标必须使用 agrep 有效地检查列表中的每个元素与另一个列表中的每个元素;这个例子是 2x2,但我的实际问题是 2,500x75,000——所以任何关于并行化的技巧也可能有用。

text<-c("The quack brown fox jumps over a lazy dog.", "Pack my box with five dozzen liquor jugs.")
texts<-data.frame(text, stringsAsFactors = FALSE)

words<-c("quick","dozen")
search<-data.frame(words, stringsAsFactors = FALSE)

texts$match<-""
for (i in 1:nrow(search)) {
  print(i)
  for (j in 1:nrow(texts)) {
    print(j)
    temp<- agrep(search$words[i], texts$text[j], max.distance = 0.1, costs = NULL,
                  ignore.case = TRUE, value = TRUE, fixed = TRUE,
                  useBytes = FALSE)
    #   print(temp)
    if (!((length(temp) == 0) && (typeof(temp) == "character"))) {
      texts$match[j]<-paste0(texts$match[j], search$words[i],';')
    }
    rm(temp)
  }
}
texts
                                        text  match
1 The quack brown fox jumps over a lazy dog. quick;
2  Pack my box with five dozzen liquor jugs. dozen;

【问题讨论】:

    标签: r parallel-processing string-matching lapply mclapply


    【解决方案1】:

    您至少可以删除第二个循环,在 agrep 的第二个参数中使用一个向量。

    text<-c("The quack brown fox jumps over a lazy dog.", "Pack my box with five dozzen liquor jugs.")
    texts<-data.frame(text, stringsAsFactors = FALSE)
    
    words<-c("quick","dozen")
    search<-data.frame(words, stringsAsFactors = FALSE)
    
    texts$matchs <- ""
    for (i in 1:nrow(search)) { # i <- 1
      print(i)
        temp<- agrepl(search$words[i], texts$text, max.distance = 0.1, costs = NULL,
                     ignore.case = TRUE, fixed = TRUE,
                     useBytes = FALSE)
        #   print(temp)
        if (max(temp) == 1 ) {
          texts$matchs[temp] <- paste(texts$matchs[temp], search$words[i], sep = ';')
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      相关资源
      最近更新 更多