I know your time is precious.
Annotations can take a while if you have quite some text to annotate. Luckily performing text annotation is trivially paralleliseable. In order to gain some of your precious time, the udpipe package integrates with the parallel package which is shipped by R-core. To run annotations in parallel, you can do as follows:
library(udpipe) data(brussels_reviews) <- subset(brussels_reviews, language %in% "fr") x <- data.frame(doc_id = x$id, text = x$feedback, stringsAsFactors = FALSE) x <- head(x, n = 75) x dim(x)
 75 2
<- udpipe_download_model(language = "french")model
<- model$file_model path path
<- udpipe(x, path, parallel.cores = 2)annotation
Don’t forget to save your annotation such that you can load it back in later.
saveRDS(annotation, file = "anno.rds") <- readRDS(file = "anno.rds")annotation
It only makes sense to run annotation in parallel if you have many CPU cores and have enough data to annotate. As udpipe models are Rcpp pointers to the loaded models on disk which can not be passed on to the parallel processes, each process will load the model again which takes some time next to the internal setup of the parallel backend.
You can gain a speedup similar as the amount of cores you have on your machine. That can be done by using the parallel.cores argument wisely alongside parallel.chunksize argument which indicates the size of the chunks that the text data will be splitted into to perform the annotation (the default of this argument is set to the size of the data / parallel.cores).
<- udpipe(x, path, parallel.cores = 2) annotation <- udpipe(x, path, parallel.cores = 2, parallel.chunksize = 50) annotation <- udpipe(x, path, parallel.cores = 1)annotation
The following calls are also possible (pass on a character vector or a pre-tokenised list)
<- setNames(x$text, x$doc_id) x <- udpipe(x, path, parallel.cores = 2) annotation <- split(annotation$token, annotation$doc_id) x <- udpipe(x, path, parallel.cores = 2)annotation
Another possibility is to use the future.apply R package. Below is a snippet how such a parallel annotation looks like with that package
library(udpipe) library(data.table) library(future.apply) data(brussels_reviews) x <- subset(brussels_reviews, language %in% "fr") x <- data.frame(doc_id = x$id, text = x$feedback, stringsAsFactors = FALSE) ## Download the model udpipe_download_model(language = "french-gsd", model_dir = getwd()) ## Run annotation over 4 CPU cores plan(multiprocess, workers = 4L) anno <- split(x, seq(1, nrow(x), by = 50)) anno <- future_lapply(anno, FUN=function(x, ...) udpipe(x, "french-gsd", ...), model_dir = getwd()) anno <- rbindlist(anno)
Enjoy the speed gain.