0

メールのリストで最大 10,000 の異なる場所の出現を見つけようとしています。私が必要としているのは、電子メールごとに最も頻繁に言及される場所を持つ 1 つのベクトル、2 番目に頻繁に言及される場所、3 番目に頻繁に言及される場所を持つベクトルです!

私のデータセットは巨大であるため、パフォーマンスに問題があります。stringi と parallel パッケージで試してみましたが、それでも非常に低速です (20.000 の電子メールと 10.000 の場所で約 15 分)。入力データ (eMails と Cities) は次のようになります。

SearchVector = c('Berlin, 'Amsterdam', San Francisco', 'Los Angeles') ...
g$Message = c('This is the first mail from paris. Berlin is a nice place', 'This is the 2nd mail from San francisco. Beirut is a nice place to stay', 'This is the 3rd mail. Los Angeles is a great place') ...

stringi を使用したコードは次のとおりです。

# libraries
library(doParallel)
library(stringi)

detectCores()
registerDoParallel(cores=7)
getDoParWorkers()

# function
getCount <- function(data, keyword)
{ 
  keyword2 = paste0( "^(", keyword, ")|(", keyword, ")$|[ ](", keyword, ")[ ]" )
  wcount <- stri_count(data, regex=keyword2)
  return(data.frame(wcount))
}

SearchVector = as.vector(countryList2)
Text = g$Message

cityName1 = character()
cityName2 = character()

result = foreach(i=Text, .combine=rbind, .inorder=FALSE, .packages=c('stringi'), .errorhandling=c('remove')) %dopar% 
{

  cities = as.data.frame(t(getCount(i, SearchVector)))
  colnames(cities) = SearchVector

  if ( length(cities[which(cities > 0)]) == 1 ) {
    cityName1 = names(sort(cities, decreasing = TRUE))[1]
    cityName2 = NA
  }
  else if ( length(cities[which(cities > 0)]) > 1 ) {
    cityName1 = names(sort(cities, decreasing = TRUE))[1]
    cityName2 = names(sort(cities, decreasing = TRUE))[2] 
  }

  else  {
    cityName1 = NA
    cityName2 = NA 

  }

  return(data.frame(cityName1, cityName2))
}


g$cityName1 = result[, 1]
g$cityName2 = result[, 2]

たとえば、 index または equal を使用して、これを高速化する方法はありますか? この問題について助けていただけることを本当に楽しみにしています。

どうもありがとうクレメンス

4

1 に答える 1