Skip to content

Commit

Permalink
Keep all loaded models with their path in the .loaded_models environm…
Browse files Browse the repository at this point in the history
…ent so that users of the function udpipe don't need to care about loading models efficiently
  • Loading branch information
jwijffels committed Sep 7, 2018
1 parent 97a319a commit ac2c024
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- Added txt_tagsequence
- Added 1 general function called udpipe which does annotation of data in TIF format.
- Add option in udpipe_download_model to download the model only it does not exist on disk
- Last loaded model is put into an environment
- Loaded model are put into an environment such that users of the function udpipe do not need to care about loading

# CHANGES IN udpipe VERSION 0.6.1

Expand Down
3 changes: 1 addition & 2 deletions R/pkg.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
NULL


.current_model <- new.env()
.current_model$udpipe_model <- structure(list(file = character(0), model = NULL), class = "udpipe_model")
.loaded_models <- new.env()
2 changes: 1 addition & 1 deletion R/udpipe_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,6 @@ udpipe_load_model <- function(file) {
out <- structure(
list(file = file, model = ptr),
class = "udpipe_model")
.current_model$udpipe_model <- out
.loaded_models[[out$file]] <- out
out
}
22 changes: 15 additions & 7 deletions R/udpipe_parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ read_connlu <- function(x, is_udpipe_annotation = FALSE, ...){
#' object = ud_dutch)
#'
#' ## You can also directly pass on the language in the call to udpipe
#' x <- udpipe("Dit werkt ook.", object = "dutch-lassysmall")
#' x <- udpipe(txt, object = "dutch-lassysmall")
#' x <- udpipe(data.frame(doc_id = names(txt), text = txt, stringsAsFactors = FALSE),
#' object = "dutch-lassysmall")
Expand All @@ -347,33 +348,40 @@ udpipe <- function(x, object, ...) {
}

getmodel <- function(object, ...){
## It already a loaded udpipe model
if(inherits(object, "udpipe_model")){
return(object)
}
## Load the model if it is a file and if it was not the last model which was loaded
already_loaded_model <- .current_model$udpipe_model
## It's a data.frame returned by udpipe_download_model
## Load the model, but only if it was not the already loaded
if(is.data.frame(object) && nrow(object) == 1 && "file_model" %in% colnames(object)){
if(identical(already_loaded_model$file, object$file_model)){
return(already_loaded_model)
if(object$file_model %in% names(.loaded_models)){
return(getmodel(.loaded_models[[object$file_model]]))
}else{
return(udpipe_load_model(object))
}
}
## It's just the path to the udpipe model
if(file.exists(object)){
if(identical(already_loaded_model$file, object)){
return(already_loaded_model)
if(object %in% names(.loaded_models)){
return(getmodel(.loaded_models[[object]]))
}else{
return(udpipe_load_model(object))
}
}
## It's the language
## Download if needed, and check again if it is the same model which was already loaded
getmodel(udpipe_download_model(object, overwrite = FALSE, ...))
}

#' @export
udpipe.character <- function(x, object, ...){
udmodel <- getmodel(object, ...)
x <- udpipe_annotate(udmodel, x = x, doc_id = names(x), ...)
if(length(names(x)) == 0){
x <- udpipe_annotate(udmodel, x = x, ...)
}else{
x <- udpipe_annotate(udmodel, x = x, doc_id = names(x), ...)
}
x <- as.data.frame(x, detailed = TRUE)
x
}
Expand Down
1 change: 1 addition & 0 deletions man/udpipe.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ac2c024

Please sign in to comment.