Tamam, şimdi daha iyi bir çözüm :)
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 ..."
Deneyler yeni eski vs (32 000 cümle) vardır:
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
ESKİ VERSION
Bu çözüm, stringi
packag gerektirir e ve HER ZAMAN, dizenin sonuna üç nokta ...
ekler.
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..."
Bu seferki sadece daha uzun 40 karakter olan cümlelere yanındaki üç noktayı ekler: Bu kabaca 3 kez hızlandırabilir stri_wrap
yılında normalize=FALSE
Ayar
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..."
PERFORMANS NOT (30 üzerinde test 0001)
Test verileri:
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
birlikte henüz bir çözüm koyarak denediniz mi? “strsplit”, “nchar”, “cumsum” ve “substr” kullanmanız gereken bileşenler olacaktır ... –
Evet, beklendiği gibi çalışmayan çeşitli şeyleri denedim. Aslında, sentezin strsplit tarafından ayrıştırılması, yolun ... – Maximilian
'strwrap (x, width = 40)'? – lukeA