Skip to content

Instantly share code, notes, and snippets.

@LaurensRietveld
Last active November 22, 2019 14:41
Show Gist options
  • Save LaurensRietveld/3549c02f5727346ae89c to your computer and use it in GitHub Desktop.
Save LaurensRietveld/3549c02f5727346ae89c to your computer and use it in GitHub Desktop.
#YASQE: Using your own SPARQL query to fetch auto-completions

This example shows to build your own autocompletion plugin. Here, the default property and class autocompleters are extended to fetch a list of possible completions from the SPARQL endpoint itself. To try it out, go e.g. to the predicate position, and type '<http'.

This particular example does not execute a (relatively expensive) query to fetch all predicates and classes, but queries for Bio2RDF dataset metrics data instead. This is cheaper/faster, though of course the dataset needs to publish this particular type of dataset metric.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Loading autocompletions via SPARQL query</title>
<link href='http://cdn.jsdelivr.net/yasqe/2.2/yasqe.min.css' rel='stylesheet' type='text/css'/>
</head>
<body>
<div id="yasqe"></div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src='http://cdn.jsdelivr.net/yasqe/2.2/yasqe.bundled.min.js'></script>
<script src="init.js"></script>
</body>
YASQE.defaults.sparql.showQueryButton = true;
YASQE.defaults.sparql.endpoint = "http://clinicaltrials.bio2rdf.org/sparql";
YASQE.defaults.sparql.callbacks.success = function(data){console.log("success", data);};
/**
* We use most of the default settings for the property and class autocompletion types. This includes:
* - the pre/post processing of tokens
* - detecting whether we are in a valid autocompletion position
* - caching of the suggestion list. These are cached for a period of a month on the client side.
*/
var getAutocompletionsArrayFromCsv = function(csvString) {
var completionsArray = [];
csvString.split("\n").splice(1).forEach(function(url) {//remove first line, as this one contains the projection variable
completionsArray.push(url.substring(1, url.length-1));//remove quotes
});
return completionsArray;
}
var customPropertyCompleter = function(yasqe) {
//we use several functions from the regular property autocompleter (this way, we don't have to re-define code such as determining whether we are in a valid autocompletion position)
var returnObj = {
isValidCompletionPosition: function(){return YASQE.Autocompleters.properties.isValidCompletionPosition(yasqe)},
preProcessToken: function(token) {return YASQE.Autocompleters.properties.preProcessToken(yasqe, token)},
postProcessToken: function(token, suggestedString) {return YASQE.Autocompleters.properties.postProcessToken(yasqe, token, suggestedString)}
};
//In this case we assume the properties will fit in memory. So, turn on bulk loading, which will make autocompleting a lot faster
returnObj.bulk = true;
returnObj.async = true;
//and, as everything is in memory, enable autoShowing the completions
returnObj.autoShow = true;
returnObj.persistent = "customProperties";//this will store the sparql results in the client-cache for a month.
returnObj.get = function(token, callback) {
//all we need from these parameters is the last one: the callback to pass the array of completions to
var sparqlQuery = "PREFIX void: <http://rdfs.org/ns/void#>\n" +
"PREFIX ds: <http://bio2rdf.org/bio2rdf.dataset_vocabulary:>\n" +
"SELECT DISTINCT *\n" +
" { [] void:subset [\n" +
" void:linkPredicate ?property;\n" +
" ]\n" +
"} ORDER BY ?property";
$.ajax({
data: {query: sparqlQuery},
url: YASQE.defaults.sparql.endpoint,
headers: {Accept: "text/csv"},//ask for csv. Simple, and uses less bandwidth
success: function(data) {
callback(getAutocompletionsArrayFromCsv(data));
}
});
};
return returnObj;
};
//now register our new autocompleter
YASQE.registerAutocompleter('customPropertyCompleter', customPropertyCompleter);
//excellent, now do the same for the classes
var customClassCompleter = function(yasqe) {
var returnObj = {
isValidCompletionPosition: function(){return YASQE.Autocompleters.classes.isValidCompletionPosition(yasqe)},
preProcessToken: function(token) {return YASQE.Autocompleters.classes.preProcessToken(yasqe, token)},
postProcessToken: function(token, suggestedString) {return YASQE.Autocompleters.classes.postProcessToken(yasqe, token, suggestedString)}
};
returnObj.bulk = true;
returnObj.async = true;
returnObj.autoShow = true;
returnObj.get = function(token, callback) {
var sparqlQuery = "PREFIX void: <http://rdfs.org/ns/void#>\n" +
"PREFIX ds: <http://bio2rdf.org/bio2rdf.dataset_vocabulary:>\n" +
"SELECT *\n" +
"{ [] void:subset [\n" +
" a ds:Dataset-Type-Count;\n" +
" void:class ?type\n"+
" ]\n" +
"} ORDER BY ?type";
$.ajax({
data: {query: sparqlQuery},
url: YASQE.defaults.sparql.endpoint,
headers: {Accept: "text/csv"},//ask for csv. Simple, and uses less bandwidth
success: function(data) {
callback(getAutocompletionsArrayFromCsv(data));
}
});
};
return returnObj;
};
YASQE.registerAutocompleter('customClassCompleter', customClassCompleter);
//And, to make sure we don't use the other property and class autocompleters, overwrite the default enabled completers
YASQE.defaults.autocompleters = ['customClassCompleter', 'customPropertyCompleter'];
//finally, initialize YASQE
var yasqe = YASQE(document.getElementById("yasqe"));
@YaserJaradeh
Copy link

YaserJaradeh commented Nov 22, 2019

Is it possible to do auto-completion on label but using the ids as the value, similarly to how Wikidata SPARQL query editor is able to do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment