Skip to content

Commit

Permalink
support #401
Browse files Browse the repository at this point in the history
  • Loading branch information
eblondel committed Jan 14, 2025
1 parent 43d80e0 commit cf984a6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
14 changes: 8 additions & 6 deletions R/geoflow_entity.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
43 changes: 31 additions & 12 deletions R/geoflow_vocabulary.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -103,30 +103,49 @@ 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
#'@return the response of the SPARQL query
query_from_uri = function(uri, graphUri = NULL, mimetype = "text/csv"){

str = paste0(
"PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?concept ?lang ?prefLabel (GROUP_CONCAT ( DISTINCT concat('\"',?altLabel,'\"@',lang(?altLabel)); separator=\"|_|\" ) as ?altLabels)
"PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
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: <http://www.w3.org/2004/02/skos/core#>
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)
}
)
Expand Down
32 changes: 32 additions & 0 deletions man/geoflow_skos_vocabulary.Rd

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

2 changes: 2 additions & 0 deletions man/geoflow_vocabulary.Rd

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

0 comments on commit cf984a6

Please sign in to comment.