Skip to content

Commit

Permalink
feat: use correct property for labelling HAL, let users enter property
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Sep 21, 2023
1 parent 4fa7190 commit 67c49ed
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 29 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export(spq_arrange)
export(spq_assemble)
export(spq_control_request)
export(spq_count)
export(spq_endpoint_info)
export(spq_filter)
export(spq_group_by)
export(spq_head)
Expand Down
2 changes: 1 addition & 1 deletion R/build_sparql.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' cat()
spq_assemble = function(.query, strict = TRUE) {

endpoint = .query[["endpoint"]]
endpoint = .query[["endpoint_info"]][["endpoint_url"]]

.query = spq_prefix(.query, auto = TRUE, prefixes = NULL)

Expand Down
2 changes: 1 addition & 1 deletion R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ wikidata_url <- function() {
#' \describe{
#' \item{name}{the abbreviated name of the SPARQL endpoint}
#' \item{url}{the full address of the SPARQL endpoint}
#' ...
#' \item{label_property}{the property used for labelling}
#' }
"usual_endpoints"

Expand Down
23 changes: 23 additions & 0 deletions R/spq_endpoint_info.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#' Create the endpoint info object for `spq_init()`
#'
#' @param label_property Property used by the endpoint for labelling.
#'
#' @return A list to be used in `spq_init()`'s `endpoint_info` argument.
#' @export
#'
#' @examples
#' spq_endpoint_info(label_property = "skos:preflabel")
spq_endpoint_info <- function(label_property = "rdfs:prefLabel") {

# TODO check property more
if (!is.character(label_property)) {
cli::cli_abort("Must provide a character as {.arg label_property}.")
}

structure(
list(
label_property = label_property
),
class = "glitter_endpoint_info"
)
}
22 changes: 19 additions & 3 deletions R/spq_init.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#'
#' @param endpoint Endpoint, either name if it is in `usual_endpoints`,
#' or an URL
#' @param endpoint_info Do not use for an usual endpoint in `usual_endpoints`!
#' Information about
#' the endpoint
#' @param request_control An object as returned by [`spq_control_request()`]
#'
#' @return A query object
Expand All @@ -28,22 +31,35 @@ spq_init = function(
max_seconds = getOption("glitter.max_seconds", 120L),
timeout = getOption("glitter.timeout", 1000L),
request_type = c("url", "body-form")
),
endpoint_info = spq_endpoint_info(
label_property = "rdfs:label"
)
) {
if (!inherits(request_control, "glitter_request_control")) {
cli::cli_abort("{.arg request_control} must be created by {.fun spq_control_request}.")
}
if (!inherits(endpoint_info, "glitter_endpoint_info")) {
cli::cli_abort("{.arg endpoint_info} must be created by {.fun spq_endpoint_info}.")
}

# if endpoint passed as name, get url
endpoint = tolower(endpoint)
usual_endpoint_info = usual_endpoints %>%
dplyr::filter(.data$name == endpoint)
endpoint = if (nrow(usual_endpoint_info) > 0) {
dplyr::pull(usual_endpoint_info, .data$url)
if (nrow(usual_endpoint_info) > 0) {
endpoint = dplyr::pull(usual_endpoint_info, .data$url)
label_property = dplyr::pull(usual_endpoint_info, .data$label_property)
} else {
endpoint
label_property = NULL
}

endpoint_info = list(
endpoint_url = endpoint,
label_property = label_property %||% endpoint_info[["label_property"]]
)

query = list(
prefixes_provided = tibble::tibble(name = NULL, url = NULL),
prefixes_used = NULL,
Expand All @@ -56,7 +72,7 @@ spq_init = function(
group_by = NULL,
order_by = NULL,
offset = NULL,
endpoint = endpoint,
endpoint_info = endpoint_info,
request_control = request_control
)

Expand Down
8 changes: 6 additions & 2 deletions R/spq_label.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ spq_label <- function(.query,
.required = FALSE,
.languages = getOption("glitter.lang", "en$"),
.overwrite = FALSE) {

label_property <- .query[["endpoint_info"]][["label_property"]] %||%
"rdfs:label"

vars = purrr::map_chr(rlang::enquos(...), spq_treat_argument)

if (!is.null(.languages)) .languages = tolower(.languages)
Expand All @@ -56,14 +60,14 @@ spq_label <- function(.query,
if (.required) {
q = spq_add(
query,
sprintf("%s rdfs:label %s_labell", x, x),
sprintf("%s %s %s_labell", x, label_property, x),
.required = .required
)
q = spq_filter(q, spq(filter))
} else {
q = spq_add(
query,
sprintf("%s rdfs:label %s_labell", x, x),
sprintf("%s %s %s_labell", x,label_property, x),
.required = .required,
.filter = filter
)
Expand Down
2 changes: 1 addition & 1 deletion R/spq_perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ spq_perform = function(.query,
"spq_init(endpoint)"
)
} else {
endpoint = .query[["endpoint"]]
endpoint = .query[["endpoint_info"]][["endpoint_url"]]
}


Expand Down
12 changes: 0 additions & 12 deletions data-raw/create_usual_endpoints.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,2 @@
endpoints=tibble::tibble(name=c("wikidata",
"dbpedia",
"databnf",
"isidore",
"hal",
"symogih"),
url=c("https://query.wikidata.org/",
"https://dbpedia.org/sparql",
"https://data.bnf.fr/sparql",
"https://isidore.science/sparql",
"http://sparql.archives-ouvertes.fr/sparql",
"http://bhp-publi.ish-lyon.cnrs.fr:8888/sparql"))
usual_endpoints=readr::read_csv("data-raw/usual_endpoints.csv")
usethis::use_data(usual_endpoints,overwrite=TRUE)
12 changes: 6 additions & 6 deletions data-raw/usual_endpoints.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name,url
wikidata,https://query.wikidata.org/
dbpedia,https://dbpedia.org/sparql
databnf,https://data.bnf.fr/sparql
isidore,https://isidore.science/sparql
hal,http://sparql.archives-ouvertes.fr/sparql
name,url,label_property
wikidata,https://query.wikidata.org/,rdfs:label
dbpedia,https://dbpedia.org/sparql,rdfs:label
databnf,https://data.bnf.fr/sparql,rdfs:label
isidore,https://isidore.science/sparql,rdfs:label
hal,http://sparql.archives-ouvertes.fr/sparql,skos:prefLabel
Binary file modified data/usual_endpoints.rda
Binary file not shown.
20 changes: 20 additions & 0 deletions man/spq_endpoint_info.Rd

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

7 changes: 6 additions & 1 deletion man/spq_init.Rd

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

2 changes: 1 addition & 1 deletion man/usual_endpoints.Rd

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

24 changes: 24 additions & 0 deletions tests/testthat/_snaps/spq_label.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,30 @@
}

# spq_label() for not rdfs:label

Code
spq_init(endpoint = "hal") %>% spq_add(
"haldoc:inria-00362381 dcterms:hasVersion ?version") %>% spq_add(
"?version dcterms:type ?type") %>% spq_label(type)
Output
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX haldoc: <https://data.archives-ouvertes.fr/document/>
SELECT ?type (COALESCE(?type_labell,'') AS ?type_label) ?version
WHERE {
haldoc:inria-00362381 dcterms:hasVersion ?version.
?version dcterms:type ?type.
OPTIONAL {
?type skos:prefLabel ?type_labell.
FILTER(lang(?type_labell) IN ('en'))
}
}

# spq_label() .overwrite

Code
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-spq_label.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ test_that("spq_label() works", {
)
})

test_that("spq_label() for not rdfs:label", {
expect_snapshot(
spq_init(endpoint = "hal") %>%
spq_add("haldoc:inria-00362381 dcterms:hasVersion ?version") %>%
spq_add("?version dcterms:type ?type") %>%
spq_label(type)
)
})

test_that("spq_label() .overwrite", {

expect_snapshot(
Expand Down
2 changes: 1 addition & 1 deletion vignettes/articles/glitter_for_hal.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ On cherche les types associés aux versions de documents. Ces types sont associ
query_docType=spq_init(endpoint = "hal") %>%
spq_add("haldoc:inria-00362381 dcterms:hasVersion ?version") %>% # Ce doc a des versions ?version
spq_add("?version dcterms:type ?type") %>% # ?version est un document de type ?type
spq_add("?type skos:prefLabel ?label") # ?type a pour étiquette ?label
spq_label(type) # ?type a pour étiquette ?label
tib_docType=spq_perform(query_docType)
Expand Down

0 comments on commit 67c49ed

Please sign in to comment.