JavaScript Query DBpedia Small application example Learning _javascript skills

Source: Internet
Author: User

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" >
&LT;TITLE&GT;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/&gt ;";
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

Related Article

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.