How to query DBpedia small application instances using Javascript

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.