Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/gn_debug.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
#' @export
#' @param ... ignored
gn_debug <- function(...) {
.Defunct(msg="gn_debug is defunct, functionality was removed from gnparser")
.Defunct(msg = "gn_debug is defunct, functionality was removed from gnparser")
}
17 changes: 13 additions & 4 deletions R/gn_parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,24 @@ gn_parse <- function(
cultivar = FALSE,
capitalize = FALSE,
diaereses = FALSE,
details = FALSE) {

details = FALSE
) {
gnparser_exists()
ver_check(1)
assert(x, "character")
file <- tempfile(fileext = ".txt")
on.exit(unlink(file))
cat(x, file = file, sep = "\n")
res <- parse_one(file, "compact", threads, batch_size,
ignore_tags, cultivar, capitalize, diaereses, details)
res <- parse_one(
file,
"compact",
threads,
batch_size,
ignore_tags,
cultivar,
capitalize,
diaereses,
details
)
lapply(strsplit(res, "\n")[[1]], jsonlite::fromJSON)
}
4 changes: 2 additions & 2 deletions R/gn_parse_tidy.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ gn_parse_tidy <- function(
cultivar = FALSE,
capitalize = FALSE,
diaereses = FALSE,
ignore_tags = FALSE) {

ignore_tags = FALSE
) {
gnparser_exists()
ver_check(1)
assert(x, "character")
Expand Down
10 changes: 6 additions & 4 deletions R/gn_version.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#' gn_version
#'
#'
#' get gnparser version information
#'
#'
#' @export
#' @return named list, with `version` and `build`
#' @examples
#' @examples
#' trys <- function(x) try(x, silent=TRUE)
#' if (interactive()) {
#' trys(gn_version())
Expand All @@ -13,7 +13,9 @@ gn_version <- function() {
gnparser_exists()
# z <- gnparser_cmd("-V", error = FALSE)
z <- gnparser_cmd("-V", error = FALSE)
if (z$status != 0) z <- gnparser_cmd("-v", error = FALSE)
if (z$status != 0) {
z <- gnparser_cmd("-v", error = FALSE)
}
err_chk(z)
process_version_string(z$stdout)
# txt <- rawToChar(z$stdout)
Expand Down
120 changes: 83 additions & 37 deletions R/gnparser.R
Original file line number Diff line number Diff line change
@@ -1,44 +1,65 @@
#' @noRd
#' @param ... Arguments to be passed to `sys::exec_internal('gnparser', ...)`
gnparser_cmd = function(...) {
gnparser_cmd <- function(...) {
sys::exec_internal(find_gnparser(), ...)
}

# find an executable from PATH, APPDATA, system.file(), ~/bin, etc
find_exec = function(cmd, dir, info = '') {
find_exec <- function(cmd, dir, info = '') {
for (d in bin_paths(dir)) {
exec = if (is_windows()) paste0(cmd, ".exe") else cmd
path = file.path(d, exec)
if (utils::file_test("-x", path)) break else path = ''
exec <- if (is_windows()) paste0(cmd, ".exe") else cmd
path <- file.path(d, exec)
if (utils::file_test("-x", path)) break else path <- ''
}
path2 = Sys.which(cmd)
path2 <- Sys.which(cmd)
if (path == '' || same_path(path, path2)) {
if (path2 == '') stop(cmd, ' not found. ', info, call. = FALSE)
return(cmd) # do not use the full path of the command
if (path2 == '') {
stop(cmd, ' not found. ', info, call. = FALSE)
}
return(cmd) # do not use the full path of the command
} else {
if (path2 != '') warning(
'Found ', cmd, ' at "', path, '" and "', path2, '". The former will be used. ',
"If you don't need both copies, you may delete/uninstall one."
)
if (path2 != '') {
warning(
'Found ',
cmd,
' at "',
path,
'" and "',
path2,
'". The former will be used. ',
"If you don't need both copies, you may delete/uninstall one."
)
}
}
normalizePath(path)
}

gnpenv <- new.env()
find_gnparser = local({
gnpenv$path = NULL # cache the path to gnparser
find_gnparser <- local({
gnpenv$path <- NULL # cache the path to gnparser
function() {
if (is.null(gnpenv$path)) gnpenv$path <- find_exec(
'gnparser', 'gnparser', 'You need to install gnparser'
)
if (is.null(gnpenv$path)) {
gnpenv$path <- find_exec(
'gnparser',
'gnparser',
'You need to install gnparser'
)
}
gnpenv$path
}
})

parse_one <- function(x, format = NULL, threads = NULL,
batch_size = NULL, ignore_tags = NULL, cultivar = FALSE,
capitalize = FALSE, diaereses = FALSE, details = FALSE) {

parse_one <- function(
x,
format = NULL,
threads = NULL,
batch_size = NULL,
ignore_tags = NULL,
cultivar = FALSE,
capitalize = FALSE,
diaereses = FALSE,
details = FALSE
) {
assert(format, "character")
assert(threads, c("integer", "numeric")) # NULL OK
assert(batch_size, c("integer", "numeric"))
Expand All @@ -49,32 +70,53 @@ parse_one <- function(x, format = NULL, threads = NULL,
assert(details, "logical")

args <- character(0)
if (!is.null(format)) args <- c(args, "--format", format)
if (!is.null(threads)) args <- c(args, "--jobs", threads)
if (!is.null(batch_size)) args <- c(args, "--batch_size", batch_size)
if (ignore_tags) args <- c(args, "--ignore_tags")
if (cultivar) args <- c(args, "--cultivar")
if (capitalize) args <- c(args, "--capitalize")
if (diaereses) args <- c(args, "--diaereses")
if (details) args <- c(args, "--details")
if (!is.null(format)) {
args <- c(args, "--format", format)
}
if (!is.null(threads)) {
args <- c(args, "--jobs", threads)
}
if (!is.null(batch_size)) {
args <- c(args, "--batch_size", batch_size)
}
if (ignore_tags) {
args <- c(args, "--ignore_tags")
}
if (cultivar) {
args <- c(args, "--cultivar")
}
if (capitalize) {
args <- c(args, "--capitalize")
}
if (diaereses) {
args <- c(args, "--diaereses")
}
if (details) {
args <- c(args, "--details")
}
z <- gnparser_cmd(c(args, x), error = FALSE)
err_chk(z)
return(rawToChar(z$stdout))
}

gnparser_exists <- function() {
check_gnp <- gnparser_cmd()
if (check_gnp$status != 0) stop("You need to install gnparser")
if (check_gnp$status != 0) {
stop("You need to install gnparser")
}
return(TRUE)
}

process_version_string <- function(x) {
txt <- rawToChar(x)
txt <- strsplit(txt, "\n")[[1]]
unlist(lapply(txt[nzchar(txt)], function(w) {
tmp <- gsub("\\s", "", strsplit(w, ":\\s")[[1]])
stats::setNames(list(tmp[2]), tmp[1])
}), FALSE)
unlist(
lapply(txt[nzchar(txt)], function(w) {
tmp <- gsub("\\s", "", strsplit(w, ":\\s")[[1]])
stats::setNames(list(tmp[2]), tmp[1])
}),
FALSE
)
}

ver_check <- function(version) {
Expand All @@ -83,7 +125,9 @@ ver_check <- function(version) {
# ver <- process_version_string(ver$stdout)
ver <- gn_version()
ver_first_num <- as.numeric(substring(gsub("v|\\.", "", ver$version), 1, 1))
if (ver_first_num < version) stop("you need to install `gnparser` v1 or greater")
if (ver_first_num < version) {
stop("you need to install `gnparser` v1 or greater")
}
return(TRUE)
}

Expand All @@ -93,7 +137,9 @@ same_path <- function(p1, p2, ...) {
}
# from xfun::normalize_path
normalize_path <- function(path, winslash = "/", must_work = FALSE) {
res = normalizePath(path, winslash = winslash, mustWork = must_work)
if (is_windows()) res[is.na(path)] = NA
res <- normalizePath(path, winslash = winslash, mustWork = must_work)
if (is_windows()) {
res[is.na(path)] <- NA
}
res
}
11 changes: 6 additions & 5 deletions R/install_gnparser.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
#' installed. This may be useful when upgrading gnparser.
#' @export

install_gnparser = function(version, force) {
lifecycle::deprecate_stop(
when = "0.3.0",
what = "install_gnparser()",
details = "Please see help page for deprecation reason and solution.")
install_gnparser <- function(version, force) {
lifecycle::deprecate_stop(
when = "0.3.0",
what = "install_gnparser()",
details = "Please see help page for deprecation reason and solution."
)
}
11 changes: 7 additions & 4 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

assert <- function(x, y) {
if (!is.null(x)) {
if (!inherits(x, y)) {
stop(deparse(substitute(x)), " must be of class ",
paste0(y, collapse = ", "), call. = FALSE)
stop(
deparse(substitute(x)),
" must be of class ",
paste0(y, collapse = ", "),
call. = FALSE
)
}
}
}
Expand All @@ -29,7 +32,7 @@ readcsv <- function(x) {
is_windows <- function() .Platform$OS.type == "windows"
is_macos <- function() unname(Sys.info()["sysname"] == "Darwin")
dir_exists <- function(x) utils::file_test("-d", x)
pkg_file = function(..., mustWork = TRUE) {
pkg_file <- function(..., mustWork = TRUE) {
system.file(..., package = 'rgnparser', mustWork = mustWork)
}

Expand Down
2 changes: 1 addition & 1 deletion inst/precompile.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ load_all()

knit("vignettes/rgnparser.Rmd.orig", "vignettes/rgnparser.Rmd")

build_vignettes()
build_vignettes()
35 changes: 24 additions & 11 deletions tests/testthat/test-gn_parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ skip_on_cran()
skip_on_ci()

test_that("gn_parse", {
x <- c("Quadrella steyermarkii (Standl.) Iltis &amp; Cornejo",
"Parus major Linnaeus, 1788", "Helianthus annuus var. texanus")
x <- c(
"Quadrella steyermarkii (Standl.) Iltis &amp; Cornejo",
"Parus major Linnaeus, 1788",
"Helianthus annuus var. texanus"
)
w <- gn_parse(x)
vapply(w, "[[", "", "normalized")

expect_is(w, "list")
for (i in w) expect_is(i, "list")

for (i in w) {
expect_is(i, "list")
}

expect_is(w[[1]]$parsed, "logical")
expect_is(w[[1]]$verbatim, "character")
expect_is(w[[1]]$normalized, "character")
Expand All @@ -22,33 +27,41 @@ test_that("cultivar arg works", {
with_cult <- gn_parse("Sarracenia flava 'Maxima'", cultivar = TRUE)
without_cult <- gn_parse("Sarracenia flava 'Maxima'", cultivar = FALSE)
expect_equal(
with_cult[[1]]$canonical$simple, "Sarracenia flava ‘Maxima’"
with_cult[[1]]$canonical$simple,
"Sarracenia flava ‘Maxima’"
)
expect_equal(
without_cult[[1]]$canonical$simple, "Sarracenia flava"
without_cult[[1]]$canonical$simple,
"Sarracenia flava"
)
})

test_that("capitalize arg works", {
with_capital <- gn_parse("parus major", capitalize = TRUE)
without_capital <- gn_parse(
"parus major", capitalize = FALSE)
"parus major",
capitalize = FALSE
)
expect_equal(
with_capital[[1]]$canonical$simple, "Parus major"
with_capital[[1]]$canonical$simple,
"Parus major"
)
# Without capitalization, name cannot be parsed
expect_equal(
without_capital[[1]]$parsed, FALSE
without_capital[[1]]$parsed,
FALSE
)
})

test_that("diaereses arg works", {
with_dia <- gn_parse("Leptochloöpsis virgata", diaereses = TRUE)
without_dia <- gn_parse("Leptochloöpsis virgata", diaereses = FALSE)
expect_equal(
with_dia[[1]]$canonical$simple, "Leptochloöpsis virgata"
with_dia[[1]]$canonical$simple,
"Leptochloöpsis virgata"
)
expect_equal(
without_dia[[1]]$canonical$simple, "Leptochlooepsis virgata"
without_dia[[1]]$canonical$simple,
"Leptochlooepsis virgata"
)
})
7 changes: 5 additions & 2 deletions tests/testthat/test-gn_parse_tidy.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ skip_on_cran()
skip_on_ci()

test_that("gn_parse_tidy", {
x <- c("Quadrella steyermarkii (Standl.) Iltis & Cornejo",
"Parus major Linnaeus, 1788", "Helianthus annuus var. texanus")
x <- c(
"Quadrella steyermarkii (Standl.) Iltis & Cornejo",
"Parus major Linnaeus, 1788",
"Helianthus annuus var. texanus"
)
z <- gn_parse_tidy(x)

expect_is(z, "data.frame")
Expand Down
Loading