Query small applications of DBpedia using Javascript
In the previous article, we learned about SPARQL and SPARQL Endpoint, and made a simple SPARQLWrapper. js to query some data from DBpedia.
This article tries to use SPARQLWrapper. js to read the data of DBpedia and display it.
Target
Enter an English word and return relevant information in the WIKI.
The main problem to be solved is how SPARQL statements can query what we need.
First look at a simple SPARQL query statement
Copy codeThe Code is as follows: PREFIX: SELECT? Instrument
WHERE {
: Andrew: playsInstrument? Instrument.
}
First define a Domain Name Space http://aabs.purl.org/music.pdf #.
Then select the instrument variable to meet the following requirements:
Subject: callback.
Bif: contains ()
Bif: contains () is a variant function of the contains () function. As its name suggests, it is a function used to determine whether or not it contains.
We can use this to query the data we need.Copy codeThe Code is as follows: prefix foaf: Select distinct? Url? Alma? Comment
Where {
? S foaf: name? Sname.
? Sname bif: contains 'China '.
? S foaf: depiction? Url.
? S dbpedia-owl: wikiPageExternalLink? Alma.
? S rdfs: comment? Comment.
}
Limit 10
This SPARQL statement queries the entries that contain the name of China, and then returns the url of the image, the url of the homepage, and the description.
Next let's write the entire program.
Complete code Copy codeThe Code is as follows: <Head>
<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/> ";
$ ("Result"). innerHTML = text;
}
} Else {
$ ("Result"). innerHTML = "no relevant information! ";
}
}
</Script>
</Head>
<Body>
<P> currently, only English queries are supported. </P>
<Input type = "text" id = "name"/>
<Input type = "button" onclick = "getInfo (document. getElementById ('name'). value);" value = "Wiki Search"/>
<Div id = "result"> </p>
</Body>
</Html>
Legacy problems
It is not clear how to query Chinese characters. If you have any knowledge, please let me know. Thank you.
Example