Home > Web Front-end > JS Tutorial > Javascript query DBpedia small application example learning_javascript skills

Javascript query DBpedia small application example learning_javascript skills

WBOY
Release: 2016-05-16 17:40:50
Original
1267 people have browsed it

Javascript query DBpedia small application
In the previous article , we learned about SPARQL and SPARQL Endpoint, and simply made a SPARQLWrapper.js to query some data from DBpedia.
This article attempts to use SPARQLWrapper.js to read DBpedia data and display it.

Goal
By inputting an English word, and then returning the relevant information in the WIKI.
The main problem to be solved is what kind of SPARQL statement can query what we need.

First look at a simple SPARQL query statement

Copy the code The code is as follows:

PREFIX :
SELECT ?instrument
WHERE {
:andrew :playsInstrument ?instrument .
}

First define a domain name space http://aabs.purl.org/music#.
Then select the instrument variable like this, which satisfies:
The subject is http://aabs.purl.org/music#andrew, the predicate is http://aabs.purl.org/music#playsInstrument, and the object is the instrument.

bif:contains()
bif:contains() is a variant of the contains() function. As the name suggests, it is a function that determines whether it is included.
Using this, we can query the data we need.
Copy code The 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 The SPARQL statement is to query the entry containing China's name, and then return the URL of its picture, the URL of the homepage, and the introduction.
Let’s finish writing the entire program.

Complete code
Copy code The code is as follows:




SPARQL DEMO

<script> <br>var $ = function(id){ <br>return document.getElementById(id); <br>}, <br>sparql = new SPARQLWrapper("http://dbpedia.org/sparql"), <br>results = []; <br>function getInfo(name){ <br>name = name.replace(/s/g, "_" ); <br>var command = "prefix foaf: <http://xmlns.com/foaf/0.1/> " <br> "select distinct ?url ?alma ?comment " <br> "where { " <br> "?s foaf:name ?sname . " <br> "?sname bif:contains '" name "'. " <br> "?s foaf:depiction ?url . " <br> "?s dbpedia-owl :wikiPageExternalLink ?alma . " <br> "?s rdfs:comment ?comment . " <br> "} " <br> "limit 10"; <br>sparql.setQuery(command); <br>sparql.query( function(json){ <br>showInfo((eval("(" json ")")).results.bindings); <br>}); <br>} <br>function showInfo(results){ <br> var text = ""; <br>if(results.length !== 0){ <br>for(var i = 0; i < results.length; i ){ <BR>text = "<img src = '" results[i].url.value "' /><br />"; <br>text = "homepage:" "<a href = '" results[i].alma.value " ' >" results[i].alma.value "</a><br />"; <br>text = "<p>" results[i].comment.value "</p&gt ;<br /><br /><br />"; <br>$("result").innerHTML = text; <br>} <br>}else{ <br>$( "result").innerHTML = "No relevant information! "; <br>} <br>} <br></script>


Currently only supports English queries.





Remaining issues
I don’t know how to query in Chinese. If anyone knows, please let me know. Thank you

Example
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template