
Live AGROVOC alignment for a vector of free-text terms
Source:R/causal_ontology.R
causal_ontology_agrovoc_align.RdFor each input term, queries the AGROVOC SPARQL endpoint for
concepts whose skos:prefLabel or skos:altLabel contains the
term, ranks the matches by Levenshtein distance to the canonical
label, and returns the best hit. Results are cached on disk via
cache_path so that repeated runs over the same vocabulary do
not re-query FAO.
Usage
causal_ontology_agrovoc_align(
terms,
cache_path = NULL,
max_per_term = 5L,
endpoint = "https://agrovoc.fao.org/sparql",
timeout_sec = 60L,
verbose = FALSE
)Arguments
- terms
Character vector of free-text terms (typically the nodes of a Knowledge Graph).
- cache_path
Optional
.rdsfile path. When supplied, the function reads cached matches on entry and writes updated matches on exit.- max_per_term
Integer — how many candidates to request per term (AGROVOC query LIMIT). The best one is kept.
- endpoint
Override the SPARQL endpoint URL.
- timeout_sec
Per-query timeout.
- verbose
Logical — print one line per queried term.
Value
A data frame with columns term (input), uri
(AGROVOC concept), label (AGROVOC pref label), distance
(Levenshtein of lowercased label vs term).
Examples
if (FALSE) { # \dontrun{
# Live SPARQL over HTTP: one round trip per uncached term, several
# seconds each. Point `cache_path` at a location that survives the
# session -- tempdir() is a fresh directory every time R starts, so
# a cache written there never gets read back.
cache <- "tools/.agrovoc_cache.rds"
ag <- causal_ontology_agrovoc_align(
c("soil organic carbon", "bulk density", "gibbsite"),
cache_path = cache, verbose = TRUE
)
ag
# `distance` is the Levenshtein distance between the AGROVOC pref
# label and the query, not a confidence. Zero means the label
# repeats the query verbatim; a large value means AGROVOC returned
# a much longer label that merely contains the query as a
# substring. Filter on it before trusting the mapping.
subset(ag, !is.na(uri) & distance <= 3)
# Failures are cached as well as hits, so an endpoint outage is
# persisted as a permanent NA row and later runs will not retry it.
# Delete the cache file after an outage.
unlink(cache)
# unique() is applied before the terms are lower-cased and trimmed,
# so two spellings of one concept both survive it and the result
# gains a duplicate row (only one query is sent -- the in-flight
# cache absorbs the second). De-duplicate yourself if the row count
# matters downstream.
nrow(causal_ontology_agrovoc_align(c("gibbsite", "Gibbsite")))
} # }