Find informations about a place - sparql

I'm tring to find informations about a place (ex : New York http://dbpedia.org/page/New_York). I' try to get informations suchs as nearestCity , museum)...
My request returns nothing...
String name = "New_York";
String s4 = "PREFIX g: <http://www.w3.org/2003/01/geo/wgs84_pos#>\n" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"PREFIX onto: <http://dbpedia.org/ontology/>\n" +
"PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX dcterms: <http://purl.org/dc/terms/>\n"+
"PREFIX dp: <http://dbpedia.org/resource/>\n"+
"PREFIX dbprop: <http://dbpedia.org/property/>\n" +
"PREFIX grs: <http://www.georss.org/georss/>\n"+
"PREFIX dbpedia-oll: <http://dbpedia.org/ontology/> \n"+
"PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>PREFIX category: <http://dbpedia.org/resource/Category:>\n"+
"\n" +
"SELECT ?nearestCity \n" +
"WHERE\n" +
"{ OPTIONAL{onto:"+name+" grs:nearestCity ?nearestCity .}\n"+
" }\n" +
"LIMIT 300\n" +
"";

There are a couple problems with the query you are doing:
1) Some namespaces are wrong: the resource New_York has the URI: http://dbpedia.org/resource/New_York. In your query that would translate to "dbpedia:"+name instead of "onto:"+name.
2) The properties that you are looking for point to the resource New_York, not the other way around. For example, for the nearest city one, this is the query that works (note the namespaces again for the property):
SELECT ?nearestCity where {
?nearestCity <http://dbpedia.org/property/nearestCity> <http://dbpedia.org/resource/New_York>.
}LIMIT 300
(See results)
And for museum:
SELECT ?m where {
?m <http://dbpedia.org/ontology/museum> <http://dbpedia.org/resource/New_York>.
}LIMIT 300
(See results)

Related

Error when using STR() function when executing a SPARQL query

Data
<ns0:Address rdf:datatype="http://www.w3.org/2001/XMLSchema#string">s block, kotahena</ns0:Address>
SPARQL Query:
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"PREFIX owl: <http://www.w3.org/2002/07/owl#> " +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
"PREFIX ns0: <http://www.Jobsurveillance.com/CV#> " +
"SELECT (str(?object) as ?label) WHERE { " +
" ?subject rdf:Full_Name ?object ." +
"}"
Error during Run
Exception error occurs when running the program.
Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered "(" at line 1, column 212.
Was expecting one of:
<VAR1> ...
<VAR2> ...
"distinct" ...
"reduced" ...
"*" ...

SPARQL execdescribe query in jena only giving prefix as output

Below SPARQL execdescribe query giving me only two prefixes as output while running with Jena query but when I run this query on virtuoso SPARQL endpoint it's giving perfect output.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX db: <http://dbpedia.org/ontology/>
PREFIX prop: <http://dbpedia.org/property/>
DESCRIBE ?movie ?author ?genre
WHERE {
?movie rdf:type db:Film ;
prop:author ?author ;
prop:genre ?genre .
}
LIMIT 2
OFFSET 0
When I run with Jena I am getting only two line output like this,
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
Below is my code which I am using but with some query its working fine,
String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"PREFIX db: <http://dbpedia.org/ontology/> " +
"PREFIX prop: <http://dbpedia.org/property/>" +
"DESCRIBE ?movie ?author ?genre" +
"WHERE { " +
"?movie rdf:type db:Film ;" +
"prop:author ?author ;" +
"prop:genre ?genre ." +
"}" +
"LIMIT 2" +
"OFFSET 0";
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://localhost:8890/sparql", queryString);
Model results = qexec.execDescribe();
results.write(System.out,"TTL");
Its perfectly giving me output on virtuoso SPARQL end point.
below is screen shot,
As suggested in comments by #AKSW, There is a space issue in the query. So after edited my query, it will perfectly work fine.

German DBpedia endpoint in HTTP Sparql Queries returns no result

I am using the Jena Java framework for querying DBpedia end point using SPARQL, to get the type for all points of interest in German cities. I am facing no issue for places that have English DBpedia entries. But, when it comes to place names to be queried from the German DBpedia endpoint (http://de.dbpedia.org/resource/Schloß_Nymphenburg), this query returns no result. This problem is also mentioned over here (http://mail-archives.apache.org/mod_mbox/jena-users/201110.mbox/%3C4E877C8A.4050705#apache.org%3E). Even after referring to this, I am unable to solve the problem. I don't know how to work with QueryEngineHTTP. I am adding two code snippets - one that works (first one - query for Allianz Arena : which has an English entry in DBpedia) and one that doesn't work (second one - for Schloß Nymphenburg, that has a German entry).
This might be a very trivial issue, but I am unable to solve it. Any pointers to a solution would be very very helpful.
Thanks a lot!
Code 1 - working :
String service = "http://dbpedia.org/sparql";
final ParameterizedSparqlString query = new ParameterizedSparqlString(
"PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>" +
"PREFIX dbo: <http://dbpedia.org/ontology/>" +
"PREFIX dcterms: <http://purl.org/dc/terms/>" +
"SELECT * WHERE {" +
"?s geo:lat ?lat ." +
"?s geo:long ?long ." +
"?s dcterms:subject ?sub}");
query.setIri("?s", "http://dbpedia.org/resource/Allianz_Arena");
QueryExecution qe = QueryExecutionFactory.sparqlService(service, query.toString());
ResultSet results = qe.execSelect();
ResultSetFormatter.out(System.out, results);
Code 2 - not working :
String service = "http://dbpedia.org/sparql";
final ParameterizedSparqlString query = new ParameterizedSparqlString(
"PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>" +
"PREFIX dbo: <http://dbpedia.org/ontology/>" +
"PREFIX dcterms: <http://purl.org/dc/terms/>" +
"SELECT * WHERE {" +
"?s geo:lat ?lat ." +
"?s geo:long ?long ." +
"?s dcterms:subject ?sub}");
query.setIri("?s", "http://de.dbpedia.org/resource/Schloß_Nymphenburg");
QueryExecution qe = QueryExecutionFactory.sparqlService(service, query.toString());
ResultSet results = qe.execSelect();
ResultSetFormatter.out(System.out, results);
I don't think this is an issue with jena at all. Trying:
SELECT * WHERE {
<http://de.dbpedia.org/resource/Schloß_Nymphenburg> ?p ?o }
at http://dbpedia.org/sparql I get no results: try it yourself.
SELECT * WHERE {
<http://de.dbpedia.org/resource/Schloss_Nymphenburg> ?p ?o }
by contrast returns something, even if it's just a bunch of cross links.

SPARQL query with FILTER returning correct values for its variables BUT only the given value for the variable being filtered

I'm using Jena to make a SPARQL query to search for all a document's properties by its subject. But the documents can have more than one subject, and when I do the search, it doesn't return me all the documents' properties, including all the documents' subjects, but even if it has 3 subjects (for example) it returns me all the documents properties + only the subject I set at FILTER.
I'd like to have as a return all the properties from the found document + all the subjects (that belong to the found document) and not only the one at FILTER.
Query (this.subject is a variable that has its value set in a JSF page):
String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
"PREFIX dc: <http://purl.org/dc/elements/1.1/> " +
"PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?document ?subject" +
" ?title ?description ?language WHERE { " +
"?document dc:title ?title." +
"?document dc:subject ?subject." +
"?document dc:description ?description." +
"?document dc:language ?language." +
"FILTER ( regex(?subject, replace( \"" + this.subject + "\", ' ', '|' ), 'i' )). }";
Thank you!
You likely want to use a sub-query to restrict to the documents matching the FILTER and then select the rest of the stuff you are actually interesting in e.g.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dc : <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?document ?subject ?title ?description ?language
WHERE
{
{
SELECT ?document
WHERE
{
?document dc:subject ?subject .
FILTER(REGEX(?subject, REPLACE("search term", " ", "|"), "i"))
}
}
?document dc:title ?title ;
dc:description ?description ;
dc:subject ?subject ;
dc:language ?language .
}
Note that this is still going to give you a row for each document-subject combination so if you have a document with 3 subjects you'll get three rows for that document. If you want to combine documents into a single row then you can use GROUP BY and then a GROUP_CONCAT aggregate, there are other questions already on Stack Overflow that detail how to do that.
Notes
Also note that using simple string concatenation to inject constants into your query is ill advised, take a look at Jena's ParameterizedSparqlString for a more user friendly and SPARQL injection proof API for building queries.

SPARQL query universities

I am trying to fetch the names of universities using SPARQL:
The rdf data is the following (and a whole lot more, but that is irrelevant).
<Organization rdf:about="http://data.semanticweb.org/organization/the-university-of-queensland">
<rdfs:label>The University of Queensland</rdfs:label>
<homepage rdf:resource="http://www.uq.edu.au/"/>
<member rdf:resource="http://data.semanticweb.org/person/jane-hunter"/>
<member rdf:resource="http://data.semanticweb.org/person/kwok-cheung"/>
<member rdf:resource="http://data.semanticweb.org/person/robert-m-colomb"/>
<name>The University of Queensland</name>
</Organization>
I have written a java program which queries the data.
My string to query the data is the following:
queryString += "PREFIX swrc: <http://swrc.ontoware.org/ontology#> \n";
queryString += "PREFIX dc: <http://purl.org/dc/elements/1.1/> \n";
queryString += "PREFIX foaf: <http://xmlns.com/foaf/0.1/> \n";
queryString += "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> \n";
queryString += "PREFIX ical: <http://www.w3.org/2002/12/cal/ical#> \n";
queryString += "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n";
queryString += "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n";
queryString += "PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#> \n";
queryString += "PREFIX swrc_ext: <http://www.cs.vu.nl/~mcaklein/onto/swrc_ext/2005/05#> \n";
queryString += "SELECT ?name WHERE {\n";
queryString += "?university rdfs:label ?aff . \n ?university foaf:name ?name FILTER(str(?aff)='uni') }";
Unfortunately, this is not correct, as no result is returned:
<?xml version='1.0' encoding='UTF-8'?>
<sparql xmlns='w3.org/2005/sparql-results#'>
<head>
<variable name='name'/>
</head>
<results>
</results>
</sparql>
Can anyone point me in the right direction?
P.S. If possible I'd like to only fetch 10 university names.
I understand that you're assuming all universities have the string "uni" in their names.
Notice that you're checking for equality of a value with the string "uni". There really is no such instance in your data set.
Replacing FILTER(str(?aff)='uni') with FILTER regex(?aff, "uni", "i") will allow you to match the values that contain the string "uni" instead. It's a regular expression filter that takes three arguments.
a variable
a regular expression compliant with the syntax described here
a set of optional flags, in this case, I used one flag, "i". It means the match must be case-insensitive.
In order to limit the number of results, you can just append the query with the LIMIT keyword.
The resulting query should be:
PREFIX swrc: <http://swrc.ontoware.org/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX ical: <http://www.w3.org/2002/12/cal/ical#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX swrc_ext: <http://www.cs.vu.nl/~mcaklein/onto/swrc_ext/2005/05#>
SELECT ?name WHERE {
?university rdfs:label ?aff .
?university foaf:name ?name
FILTER regex(?aff, "uni", "i")
} LIMIT 10;
In your RDF snippet does not have the "foaf:" namespace. So, maybe you should put in your RDF. Unless of course it is the default namespace in your RDF document.