string - R中的快速部分字符串匹配

标签 string r performance string-matching

给定一个字符串向量texts和模式向量patterns,我想为每个文本找到任何匹配的模式。

对于小型数据集,可以使用grepl在R中轻松完成:

patterns = c("some","pattern","a","horse")
texts = c("this is a text with some pattern", "this is another text with a pattern")

# for each x in patterns
lapply( patterns, function(x){
  # match all texts against pattern x
  res = grepl( x, texts, fixed=TRUE )
  print(res)
  # do something with the matches
  # ...
})


此解决方案是正确的,但无法扩展。即使使用相对较大的数据集(〜500个文本和模式),此代码也非常缓慢,在现代计算机上每秒只能解决大约100个案例-考虑到这是粗略的字符串部分匹配,而没有正则表达式(设置为< cc>)。即使使fixed=TRUE并行也不能解决问题。
有没有办法有效地重写此代码?

谢谢,
木兰

最佳答案

使用stringi软件包-甚至比grepl还要快。检查基准!
我使用@ Martin-Morgan帖子中的文字

require(stringi)
require(microbenchmark)

text = readLines("~/Desktop/pg100.txt")
pattern <-  strsplit("all the world's a stage and all the people players", " ")[[1]]

grepl_fun <- function(){
    lapply(pattern, grepl, text, fixed=TRUE)
}

stri_fixed_fun <- function(){
    lapply(pattern, function(x) stri_detect_fixed(text,x,NA))
}

#        microbenchmark(grepl_fun(), stri_fixed_fun())
#    Unit: milliseconds
#                 expr      min       lq   median       uq      max neval
#          grepl_fun() 432.9336 435.9666 446.2303 453.9374 517.1509   100
#     stri_fixed_fun() 213.2911 218.1606 227.6688 232.9325 285.9913   100

# if you don't believe me that the results are equal, you can check :)
xx <- grepl_fun()
stri <- stri_fixed_fun()

for(i in seq_along(xx)){
    print(all(xx[[i]] == stri[[i]]))
}

关于string - R中的快速部分字符串匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24257850/

相关文章:

R: += (plus equals) 和++ (plus plus) 等价于 c++/c#/java 等?

r - as.numeric()删除R中的小数位,如何更改?

Javascript - 动态创建它?

javascript - 将字符串转换为时间 JavaScript (h :m)

python - 在给定索引处将一些字符串插入给定字符串

string - 将 Vec<String> 作为 IntoIterator<&'a str> 传递

r - 成对获胜者;或向量值 group_by 变异?

java - 如何从 java 字符串中删除这个字符 <U+2028> ?

performance - 有效地查找 float 组中最接近的值

python - 哪种方法可以更快地替换字符串中最后一次出现的子字符串?