好的,我现在有更好的解决方案了:)
x <- c("This is the longest sentence in world, so now just make it longer","No in fact, this is the longest sentence in entire world, world, world, world, the whole world")
extract <- function(x){
result <- stri_extract_first_regex(x, "^.{0,40}( |$)")
longer <- stri_length(x) > 40
result[longer] <- stri_paste(result[longer], "...")
result
}
extract(x)
## [1] "This is the longest sentence in world, ..." "No in fact, this is the longest sentence ..."
新旧基准(32 000 个句子):
microbenchmark(sapply(x, cutAndAddDots, USE.NAMES = FALSE), extract(x), times=5)
Unit: milliseconds
expr min lq median uq max neval
sapply(x, cutAndAddDots, USE.NAMES = FALSE) 3762.51134 3762.92163 3767.87134 3776.03706 3788.139 5
extract(x) 56.01727 57.18771 58.50321 79.55759 97.924 5
旧版
该解决方案需要stringi
包装并始终添加三个点...
到字符串末尾。
require(stringi)
sapply(x, function(x) stri_paste(stri_wrap(x, 40)[1],"..."),USE.NAMES = FALSE)
## [1] "This is the longest sentence in world..." "No in fact, this is the longest..."
这个仅在长度超过 40 个字符的句子中添加三个点:
require(stringi)
cutAndAddDots <- function(x){
w <- stri_wrap(x, 40)
if(length(w) > 1){
stri_paste(w[1],"...")
}else{
w[1]
}
}
sapply(x, cutAndAddDots, USE.NAMES = FALSE)
## [1] "This is the longest sentence in world" "No in fact, this is the longest..."
性能说明环境normalize=FALSE
in stri_wrap
可能会加速大约 3 倍(测试了 30000 个句子)
测试数据:
x <- stri_rand_lipsum(3000)
x <- unlist(stri_split_regex(x,"(?<=\\.) "))
head(x)
[1] "Lorem ipsum dolor sit amet, vel commodo in."
[2] "Ultricies mauris sapien lectus dignissim."
[3] "Id pellentesque semper turpis habitasse egestas rutrum ligula vulputate laoreet mollis id."
[4] "Curabitur volutpat efficitur parturient nibh sociosqu, faucibus tellus, eleifend pretium, quis."
[5] "Feugiat vel mollis ultricies ut auctor."
[6] "Massa neque auctor lacus ridiculus."
stri_length(head(x))
[1] 43 41 90 95 39 35
cutAndAddDots <- function(x){
w <- stri_wrap(x, 40, normalize = FALSE)
if(length(w) > 1){
stri_paste(w[1],"...")
}else{
w[1]
}
}
cutAndAddDotsNormalize <- function(x){
w <- stri_wrap(x, 40, normalize = TRUE)
if(length(w) > 1){
stri_paste(w[1],"...")
}else{
w[1]
}
}
require(microbenchmark)
microbenchmark(sapply(x, cutAndAddDots, USE.NAMES = FALSE),sapply(x, cutAndAddDotsNormalize, USE.NAMES = FALSE),times=3)
Unit: seconds
expr min lq median uq max
sapply(x, cutAndAddDots, USE.NAMES = FALSE) 3.917858 3.967411 4.016964 4.055571 4.094178
sapply(x, cutAndAddDotsNormalize, USE.NAMES = FALSE) 13.493732 13.651451 13.809170 13.917854 14.026538