JavaScript Query DBpedia Small application
In the previous article, we learned about SPARQL, SPARQL Endpoint, and simply made a sparqlwrapper.js to query some data from the DBpedia.
This article attempts to use Sparqlwrapper.js to read the DBpedia data and display it.
Target
By entering an English word, you return the relevant information in the wiki.
So the main problem to solve is what kind of SPARQL statement can query what we need.
First look at a simple SPARQL query statement
Copy Code code as follows:
PREFIX: SELECT? instrument
WHERE {
: Andrew:p laysinstrument? instrument.
}
First, define a domain namespace http://aabs.purl.org/music#.
Then choose such a instrument variable that he satisfies:
The subject is http://aabs.purl.org/music#andrew, the predicate is http://aabs.purl.org/music#playsInstrument, the object is the instrument.
bif:contains ()
Bif:contains () is a variant function of the CONTAINS () function, as the name implies, to determine whether or not a function is included.
Using this we can find the data we need.
Copy Code code as follows:
Prefix FOAF: Select distinct? url? Alma? comment
where {
? s Foaf:name sname.
Bif:contains ' sname '.
? s foaf:depiction? URL.
? s Dbpedia-owl:wikipageexternallink Alma.
? s rdfs:comment comment.
}
Limit 10
The SPARQL statement is the query that contains the name of the term, and then returns the URL of the picture, the URL of the homepage, and the profile.
Let's finish writing the whole program below.
Complete Code
Copy Code code as follows:
<meta charset= "Utf-8" >
<TITLE>SPARQL demo</title>
<script src= "Sparqlwrapper.js" ></script>
<script>
var $ = function (ID) {
return document.getElementById (ID);
},
SPARQL = new Sparqlwrapper ("HTTP://DBPEDIA.ORG/SPARQL"),
results = [];
function GetInfo (name) {
Name = Name.replace (/\s/g, "_");
var command = "prefix foaf: + "SELECT distinct? url? Alma? Comment"
+ "where {"
+ "? s Foaf:name sname."
+ "? Sname bif:contains '" + name + "'."
+ "? s foaf:depiction? URL."
+ "? s Dbpedia-owl:wikipageexternallink Alma."
+ "? s rdfs:comment comment."
+ "} "
+ "Limit 10";
Sparql.setquery (command);
Sparql.query (function (JSON) {
Showinfo ((eval ("+ JSON +"))). Results.bindings);
});
}
function Showinfo (results) {
var text = "";
if (results.length!== 0) {
for (var i = 0; i < results.length; i++) {
Text + + "<br/>";
Text + "Homepage:" + "<a href = '" + results[i].alma.value + "' >" + results[i].alma.value + "</a><br/> ;";
Text + + "<p>" + Results[i].comment.value + "</p><br/><br/><br/>";
$ ("result"). InnerHTML = text;
}
}else{
$ ("result"). InnerHTML = "No relevant information!" ";
}
}
</script>
<body>
<p> currently only supports English Query. </p>
<input type= "text" id= "name"/>
<input type= button "onclick=" GetInfo (document.getElementById (' name '). Value); "Value= Wiki Search"/>
<div id= "Result" ></p>
</body>
Legacy Issues
Not very clear how to query Chinese, if you know friends, please tell me, thank you.
Example