From cf984a63d27450b5ec04759b939f50726e70cb3b Mon Sep 17 00:00:00 2001 From: eblondel Date: Tue, 14 Jan 2025 12:25:30 +0100 Subject: [PATCH] support #401 --- R/geoflow_entity.R | 14 ++++++----- R/geoflow_vocabulary.R | 43 ++++++++++++++++++++++++---------- man/geoflow_skos_vocabulary.Rd | 32 +++++++++++++++++++++++++ man/geoflow_vocabulary.Rd | 2 ++ 4 files changed, 73 insertions(+), 18 deletions(-) diff --git a/R/geoflow_entity.R b/R/geoflow_entity.R index fc168a5..0bf5dad 100644 --- a/R/geoflow_entity.R +++ b/R/geoflow_entity.R @@ -1880,17 +1880,19 @@ geoflow_entity <- R6Class("geoflow_entity", target_vocab = target_vocab[[1]] subject$uri = target_vocab$uri subject$keywords = lapply(subject$keywords, function(keyword){ + rs = NULL if(!is.null(keyword$uri)){ #enrich from URI to add labels rs = target_vocab$query_from_uri(uri = keyword$uri) - if(nrow(rs)>0){ - keyword$name = rs[rs$lang == "en",]$prefLabel - for(lang in rs$lang){ - attr(keyword$name, paste0("locale#",toupper(lang))) = rs[rs$lang == lang,]$prefLabel - } - } }else{ #enrich from an existing term to get URI + other labels + rs = target_vocab$query_from_term(term = keyword$name) + } + if(!is.null(rs)) if(tibble::is_tibble(rs)) if(nrow(rs)>0){ + keyword$name = rs[rs$lang == "en",]$prefLabel + for(lang in rs$lang){ + attr(keyword$name, paste0("locale#",toupper(lang))) = rs[rs$lang == lang,]$prefLabel + } } return(keyword) }) diff --git a/R/geoflow_vocabulary.R b/R/geoflow_vocabulary.R index d86cf77..0d13326 100644 --- a/R/geoflow_vocabulary.R +++ b/R/geoflow_vocabulary.R @@ -25,7 +25,7 @@ geoflow_vocabulary <- R6Class("geoflow_vocabulary", #'@field software software software = NULL, - #'@name Initializes a vocabulary + #'@description Initializes a vocabulary #'@param id id #'@param def def #'@param uri uri @@ -37,7 +37,7 @@ geoflow_vocabulary <- R6Class("geoflow_vocabulary", self$software_type = software_type }, - #'@name Set software + #'@description Set software #'@param software software setSoftware = function(software){ #TODO @@ -64,7 +64,7 @@ geoflow_skos_vocabulary <- R6Class("geoflow_skos_vocabulary", #'@field endpoint endpoint endpoint = NA, - #'@name Initializes a vocabulary + #'@description Initializes a vocabulary #'@param id id #'@param def def #'@param uri uri @@ -74,7 +74,7 @@ geoflow_skos_vocabulary <- R6Class("geoflow_skos_vocabulary", self$endpoint = endpoint }, - #'@name query + #'@description query #'@param str str #'@param graphUri graphUri #'@param mimetype mimetype @@ -103,7 +103,7 @@ geoflow_skos_vocabulary <- R6Class("geoflow_skos_vocabulary", self$query(str) }, - #'@name query_from_uri + #'@description query_from_uri #'@param uri uri #'@param graphUri graphUri #'@param mimetype mimetype @@ -111,22 +111,41 @@ geoflow_skos_vocabulary <- R6Class("geoflow_skos_vocabulary", query_from_uri = function(uri, graphUri = NULL, mimetype = "text/csv"){ str = paste0( - "PREFIX skos: - PREFIX rdf: - SELECT ?concept ?lang ?prefLabel (GROUP_CONCAT ( DISTINCT concat('\"',?altLabel,'\"@',lang(?altLabel)); separator=\"|_|\" ) as ?altLabels) + "PREFIX skos: + SELECT ?concept ?lang ?prefLabel WHERE { BIND(<",uri,"> AS ?concept) ?concept skos:prefLabel ?prefLabel . BIND(lang(?prefLabel) AS ?lang) - OPTIONAL{ - ?concept skos:altLabel ?altLabel . - FILTER(lang(?altLabel) = ?lang) - } } GROUP BY ?concept ?lang ?prefLabel ORDER BY ?lang " ) + self$query(str = str, graphUri = graphUri, mimetype = mimetype) + }, + + #'@description query_from_term + #'@param term term + #'@param graphUri graphUri + #'@param mimetype mimetype + #'@return the response of the SPARQL query + query_from_term = function(term, graphUri = NULL, mimetype = "text/csv"){ + + str = paste0( + "PREFIX skos: + SELECT ?concept ?lang ?prefLabel + WHERE { + ?concept skos:prefLabel ?searchLabel . + ?concept skos:prefLabel ?prefLabel . + FILTER (STR(?searchLabel) = \"", term, "\") + FILTER (LANG(?prefLabel) != \"\") + BIND (LANG(?prefLabel) AS ?lang) + } + GROUP BY ?concept ?lang ?prefLabel + ORDER BY ?lang " + ) + self$query(str = str, graphUri = graphUri, mimetype = mimetype) } ) diff --git a/man/geoflow_skos_vocabulary.Rd b/man/geoflow_skos_vocabulary.Rd index cb898d9..3c3f921 100644 --- a/man/geoflow_skos_vocabulary.Rd +++ b/man/geoflow_skos_vocabulary.Rd @@ -35,6 +35,7 @@ geoflow_skos_vocabulary \item \href{#method-geoflow_skos_vocabulary-query}{\code{geoflow_skos_vocabulary$query()}} \item \href{#method-geoflow_skos_vocabulary-ping}{\code{geoflow_skos_vocabulary$ping()}} \item \href{#method-geoflow_skos_vocabulary-query_from_uri}{\code{geoflow_skos_vocabulary$query_from_uri()}} +\item \href{#method-geoflow_skos_vocabulary-query_from_term}{\code{geoflow_skos_vocabulary$query_from_term()}} \item \href{#method-geoflow_skos_vocabulary-clone}{\code{geoflow_skos_vocabulary$clone()}} } } @@ -49,6 +50,7 @@ geoflow_skos_vocabulary \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-geoflow_skos_vocabulary-new}{}}} \subsection{Method \code{new()}}{ +Initializes a vocabulary \subsection{Usage}{ \if{html}{\out{
}}\preformatted{geoflow_skos_vocabulary$new(id, def, uri, endpoint)}\if{html}{\out{
}} } @@ -71,6 +73,7 @@ geoflow_skos_vocabulary \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-geoflow_skos_vocabulary-query}{}}} \subsection{Method \code{query()}}{ +query \subsection{Usage}{ \if{html}{\out{
}}\preformatted{geoflow_skos_vocabulary$query(str, graphUri = NULL, mimetype = "text/csv")}\if{html}{\out{
}} } @@ -104,6 +107,7 @@ Ping query \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-geoflow_skos_vocabulary-query_from_uri}{}}} \subsection{Method \code{query_from_uri()}}{ +query_from_uri \subsection{Usage}{ \if{html}{\out{
}}\preformatted{geoflow_skos_vocabulary$query_from_uri( uri, @@ -119,6 +123,34 @@ Ping query \item{\code{graphUri}}{graphUri} +\item{\code{mimetype}}{mimetype} +} +\if{html}{\out{
}} +} +\subsection{Returns}{ +the response of the SPARQL query +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-geoflow_skos_vocabulary-query_from_term}{}}} +\subsection{Method \code{query_from_term()}}{ +query_from_term +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{geoflow_skos_vocabulary$query_from_term( + term, + graphUri = NULL, + mimetype = "text/csv" +)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{term}}{term} + +\item{\code{graphUri}}{graphUri} + \item{\code{mimetype}}{mimetype} } \if{html}{\out{
}} diff --git a/man/geoflow_vocabulary.Rd b/man/geoflow_vocabulary.Rd index 7a764c4..08bfa43 100644 --- a/man/geoflow_vocabulary.Rd +++ b/man/geoflow_vocabulary.Rd @@ -44,6 +44,7 @@ geoflow_vocabulary \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-geoflow_vocabulary-new}{}}} \subsection{Method \code{new()}}{ +Initializes a vocabulary \subsection{Usage}{ \if{html}{\out{
}}\preformatted{geoflow_vocabulary$new(id, def, uri, software_type)}\if{html}{\out{
}} } @@ -66,6 +67,7 @@ geoflow_vocabulary \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-geoflow_vocabulary-setSoftware}{}}} \subsection{Method \code{setSoftware()}}{ +Set software \subsection{Usage}{ \if{html}{\out{
}}\preformatted{geoflow_vocabulary$setSoftware(software)}\if{html}{\out{
}} }