1

我正在寻找一些关于以下 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;
4

1 回答 1

0

您至少可以删除第二个循环,在 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 = ';')
    }

}
于 2015-07-29T15:43:04.280 回答