Skip to content

Instantly share code, notes, and snippets.

@ozjimbob
Created January 27, 2025 06:27
Show Gist options
  • Save ozjimbob/102b2fc0db3eed4d296c66ef98d14364 to your computer and use it in GitHub Desktop.
Save ozjimbob/102b2fc0db3eed4d296c66ef98d14364 to your computer and use it in GitHub Desktop.
library(plumber)
#* @apiTitle Chunder
#* @apiDescription Chunder
file_path="pg16389.txt"
text_content <- readLines(file_path, warn = FALSE)
text_content <- paste(text_content, collapse = " ")
words_vector <- unlist(strsplit(text_content, "\\s+"))
text_content <- readLines(file_path, warn = FALSE)
text_content <- paste(text_content, collapse = " ")
sentences_vector <- unlist(strsplit(text_content, "(?<=\\.)\\s+", perl = TRUE))
trim_to_first_five_words <- function(sentence) {
words <- unlist(strsplit(sentence, "\\s+"))
first_five_words <- paste(words[1:min(5, length(words))], collapse = " ")
return(first_five_words)
}
trimmed_sentences <- sapply(sentences_vector, trim_to_first_five_words)
trimmed_sentences <- as.character(trimmed_sentences)
#' Return result
#' @get /<ht>
#' @serializer html
function(ht="") {
leng <- floor(runif(1,1000,5000))
out <- list()
pflag<-1
for(i in 1:leng){
st_sen <- sample(trimmed_sentences,1)
st_wor <- sample(words_vector,floor(runif(1,15,25)))
sel_ht <- sample(1:length(st_wor),1)
a_wor <- st_wor[sel_ht]
a_wor <- paste0("<a href='/",sample(words_vector,1),"'>",a_wor,"</a>")
st_wor[sel_ht]<-a_wor
wlist <- paste0(st_wor,collapse=" ")
wlist <- paste0(st_sen,", ",wlist,"? ")
if(pflag==1){
wlist <- paste0("<p>",wlist)
}
pflag=pflag+1
if(pflag==5){
wlist <- paste0(wlist,"</p>")
pflag<-1
}
out[[i]]<-wlist
}
out[[i]]<-paste0(out[[i]],"</p>")
out <- paste0(out)
out <- paste0(out,collapse=" ")
return(out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment