Skip to content
Snippets Groups Projects
Commit ce74ec5b authored by Lukas Gehrke's avatar Lukas Gehrke
Browse files

Merge branch '43-make-create_annotation-parameteters-optional' into 'master'

Resolve "Make create_annotation parameteters optional"

Closes #43

See merge request !37
parents e3702c0e 911bb9a1
No related branches found
No related tags found
1 merge request!37Resolve "Make create_annotation parameteters optional"
......@@ -25,8 +25,8 @@ init_nlp <- function(type, value) {
#' Create annotations for the given text
#'
#' @param text Text to annotate
#' @param article.id ArticleID used for cashing
#' @param article.rev.id ArticleRevisionID used for cashing
#' @param article.id ArticleID used for cashing. **Optional** as long as cache is not to be used.
#' @param article.rev.id ArticleRevisionID used for cashing. **Optional** as long as cache is not to be used.
#' @param use.cache Should cashed data be uses
#' @param write.cache Should the generated annotations be cashed
#' @param data.dir Directory the data should be read from and/or written to
......@@ -34,22 +34,29 @@ init_nlp <- function(type, value) {
#' @return Annotation object for use with cleanNLP methods
#' @export
create_annotations <- function(text, article.id, article.rev.id, use.cache = TRUE, write.cache = FALSE, data.dir = "data") {
# Generate filename, for some reason there paste0 will pad the article id with leading whitespaces
# To prevent this we stip 'em again
filename <- gsub(" ", "", paste(data.dir, "annotations", paste0(article.id, "-", article.rev.id, ".RDS"), sep = .Platform$file.sep), fixed = TRUE)
# Check if there is a cached version of the annotations for this article in this specific revision
if(use.cache & file.exists(filename)) {
res <- tryCatch({
data <- readRDS(filename)
data
}, error = function (e) {
cat("Cached data seems to be corrupted, redoing annotation.\n")
})
return(res)
if (use.cache || write.cache) {
if (!missing(article.id) && !(article.rev.id)) {
# Generate filename, for some reason there paste0 will pad the article id with leading whitespaces
# To prevent this we stip 'em again
filename <- gsub(" ", "", paste(data.dir, "annotations", paste0(article.id, "-", article.rev.id, ".RDS"), sep = .Platform$file.sep), fixed = TRUE)
# Check if there is a cached version of the annotations for this article in this specific revision
if(use.cache & file.exists(filename)) {
res <- tryCatch({
data <- readRDS(filename)
data
}, error = function (e) {
cat("Cached data seems to be corrupted, redoing annotation.\n")
})
return(res)
}
} else if (use.cache) {
warning("use.cache was set to true without providing article id and revision id, cache will not be used.")
}
}
annotation <- cleanNLP::cnlp_annotate(text, as_strings = TRUE)
......@@ -57,13 +64,18 @@ create_annotations <- function(text, article.id, article.rev.id, use.cache = TRU
# Write cache if desired
if(write.cache) {
if (!dir.exists("data")) {
dir.create("data")
if (!missing(article.id) && !missing(article.rev.id)) {
if (!dir.exists("data")) {
dir.create("data")
}
if (!dir.exists("data/annotations")) {
dir.create("data/annotations")
}
saveRDS(annotation, filename)
} else {
warning("write.cache was set to true without providing article id and revision id, cache will not be written")
}
if (!dir.exists("data/annotations")) {
dir.create("data/annotations")
}
saveRDS(annotation, filename)
}
# Return data
......@@ -71,4 +83,4 @@ create_annotations <- function(text, article.id, article.rev.id, use.cache = TRU
# But then again, it suggests snake case for variables...
return(annotation)
}
}
\ No newline at end of file
......@@ -10,9 +10,9 @@ create_annotations(text, article.id, article.rev.id, use.cache = TRUE,
\arguments{
\item{text}{Text to annotate}
\item{article.id}{ArticleID used for cashing}
\item{article.id}{ArticleID used for cashing. **Optional** as long as cache is not to be used.}
\item{article.rev.id}{ArticleRevisionID used for cashing}
\item{article.rev.id}{ArticleRevisionID used for cashing. **Optional** as long as cache is not to be used.}
\item{use.cache}{Should cashed data be uses}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment