给定一个字符串向量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/