1

agrepを使用して、リスト内の各要素を別のリスト内の各要素に対して効果的にチェックする必要がある以下のMWEの目的の最適な再加工された実装に関するアドバイスを探しています。この例は 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 の 2 番目の引数でベクトルを使用して、少なくとも 2 番目のループを削除できます。

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 に答える