Fundamental understanding of SPARQL - sparql

I try to understand SPARQL and mess around with the SPARQL Tool that is provided by dbpedia. I've read the w3 documentation and now I want to create my very own query. I would like to find the names of all books in dbpedia written by J. J. R. Tolkien.
Therefore I "designed" this query:
SELECT ?name WHERE { ?name ?author "J._R._R._Tolkien".
?name ?mediaType "Print"}
The result is empty, but I would at least expect this book popping up:
http://dbpedia.org/page/The_Lord_of_the_Rings
Can someone tell me, what my conceptual mistake is?

I good approach would be to review his DBpedia page and then choose the desired properties. In your case two good candidates are notableWork and author. Here's a query with both of them, effectively using the latter.
PREFIX : <http://dbpedia.org/resource/>
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX p: <http://dbpedia.org/property/>
SELECT DISTINCT ?is_author_of #?has_notable_work
FROM <http://dbpedia.org/>
WHERE {
:J._R._R._Tolkien rdf:type o:Writer ;
#o:notableWork ?has_notable_work ;
^p:author ?is_author_of .
}
I have used rdf:type o:Writer to reduce possible ambiguity (none, in case of using URI of an individual resource in dbpedia), and ^ to get the right direction. My preference for ?is_author_of and ?has_notable_workinstead of e.g. ?book and ?popular_book was because I'm not sure what kind of work is he author of.

Related

Unexpected behavior in Visual Graph, and when returning triples with SPARQL

How are you?
I 'm taking my first steps with GraphDb and I've found a couple of behaviors that I don't quite understand. Let me build the case.
I have the following Ontology, which I have loaded into GraphDb.
enter image description here
Let's only consider the tree under Persona.
When I go to Visual Graph and type :Persona in the search bar, this is what returns:
enter image description here
As you can see all the nodes corresponding to the ontology are there, but also some other nodes like Class (in different colors), and Thing and Nothing. (Ignore Paul which is an instance I added)
What are these other nodes? and how can I prevent them from appearing in this view?
Now, when I query with SPARQL, and I run
select * where {
?s ?p ?o .
}
If I have "Include inferred data in results" Off, I get this
enter image description here
Which is fine. It's what I would expect.
But ... when I turn inference On , which I believe is one of the true powers of working with rdf, I get around 800 records with all sort of triples with definitions for owl and rdf generic objects.
In order to get only my stuff and keep the inference capability on, I filtered as follows
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX pe: <http://www.semanticlab.com/ontologias/EmpresasYPersonas#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
select * where {
?s ?p ?o .
FILTER (REGEX(str(?s),"Persona*" )).
FILTER (!REGEX(str(?o),"owl*" )).
FILTER (!REGEX(str(?o),"rdf*" )).
}
order by ?s
Which returns
enter image description here
Here I see some inferencing, for example Paul classified as a Persona, which is not explicitly stated.
But on the other hand there are a bunch of new triples that I don't understand nor want, as they don't bring any value: for example the ones with owl:sameAs, owl:equivalentClass, or the ones stating that Persona is a subClassOf Persona (which in fact I believe is wrong).
Could you please explain why this is happening, and how to prevent this behavior?
I'm aware that I might be making some mistakes, so if you spot any, please let me know.

How to filter results by city?

I am using DBpedia to retrieve data on specific towns' POIs, for instance Barcelona. I've tried to find appropriate answers to my problems on Stack Overflow but almost all the answers provided did not work out for me, and given that I am new in SPARQL, I couldn't manage to figure out whether or not it was the syntax that was at fault or if there was something else. I am using the DBpedia SPARQL Endpoint available at the following address : http://dbpedia.org/sparql
I've tried to use some things such as ?citylabel or ?location, and to either use the FILTER command or directly fill a value for these classes. I've also tried a few other things, without satisfying results (and most of the times syntax errors that I could not resolve). These solutions have been, in most cases, applied and seemed to work just fine for people, so I do not understand what's going on.
BASE <http://www.dbpedia.org/resource/>
PREFIX : <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia3: <http://dbpedia.org/ontology/>
SELECT (SAMPLE(?label) as ?activity_name)
(SAMPLE(?latitude) as ?activity_lat)
(SAMPLE(?longitude) as ?activity_lon)
(SAMPLE(?homepage) as ?URL)
(SAMPLE(?type) as ?activity_type)
(SAMPLE(?abstract) as ?descriptor)
WHERE
{ ?Museum a dbo:Museum ;
rdfs:label ?label ;
dbo:abstract ?abstract ;
dbo:type ?type ;
geo:lat ?latitude ;
geo:long ?longitude ;
foaf:homepage ?homepage .
}
GROUP BY ?Museum ?label
The results of this query are, I think, pretty much any museum that is known by DBpedia and categorized as such. What I'd like to have is a list of museums within Barcelona. Can somebody give me an rather in-depth answer so I can understand how it is working ? Thanks, in advance.

DBPedia SPARQL, return certain number of relevant page URIs for entity EXCEPT the URIs where the entity belongs to a set of subclasses of Owl:Thing

Looking for SPARQL query to do the following:
For example, I have the word Apple. Apple may refer to the organization Apple_Inc or the Species of Plants class as per the ontology. Owl: Thing has a subclass called Species, so I want to return those most relevant/maximum-hit URIs where the keyword Apple does not belong to the Species subclass. So when you return all the URIs, http://dbpedia.org/page/Apple should not be one of them, neither must ANY relevant link that comes under Species subclass.
By maximum-hit/most relevant I mean the top returned results that match the query! Like when you access the PrefixSearch (i.e. Autocomplete) API, it has the parameter called MaxHits.
For example http://lookup.dbpedia.org/api/search/PrefixSearch?QueryClass=&MaxHits=2&QueryString=berl is a link where you want to return the top 2 URIs that match the QueryString=berl.
Like I'm actually really struggling to even explain the work I've done so far because I'm not able to understand the structure and how to formulate a proper query..
with respect to negation in SPARQL, I found a relevant portion of the documentation in the link here.. But I do not know how and where to proceed from there, and cannot understand why keywords like ?person are used.. I can understand the person is used to selected well.. PEOPLE names, but I would like to know how and where to find these keywords like ?person, ?name to represent a specific entity..
SELECT ?uri ?label
WHERE {
?uri rdfs:label ?label .
filter(?label="car"#en)
}
I would really appreciate if someone could link me the part of the documentation I can clearly read and understand that ?uri is used to select a URI in the form www.dbpedia.org'/page/SomeEntity and what these ?person, ?name, ?label represent.
I'm actually so lost.. I will go up and start eating one elephant at a time. For now, I'll be very grateful if I get an answer to this.
If there is anyway you know where I can avoid learning and using SPARQL, that would work too! I know Python well enough, so leveraging an API to pull this information is also fine by me. This question was posted by me.
Answer posted by #Stanislav-Kravin --
SELECT DISTINCT ?s
WHERE
{ ?s a owl:Thing .
?s rdfs:label ?label .
FILTER ( LANGMATCHES ( LANG ( ?label ), 'en' ) )
?label bif:contains '"apple"' .
FILTER NOT EXISTS { ?s rdf:type/rdfs:subClassOf* dbo:Species }
}

Sparql: getting all politicians who ruled a city

I'm new to sparql and I'm trying to understand how to get the resources I need for building a query. I started trying to get all the politicians that ruled a city or a country, and at the moment I could do just the following:
I started by following the links in snorql (in the prefixes) and looking for an entity by adding "politician" at the end. I found one :
PREFIX : <http://dbpedia.org/resource/>
So I wrote http://dbpedia.org/resource/Politician and the resource does exist. I tryed to use it in this way:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT ?thing WHERE {
?thing a :Politician .
?thing dbo:birthPlace dbpedia:Italy.
}
LIMIT 50
Run in virtuoso.
Even if I remove the second line of the SELECT, I have no results. But if I change the first line with: ?thing a dbo:Person. or even if I remove it, I get the people born in Italy. But not just the politicians. A second problem is I don't need the politicians that were born but ruled that place. How or where can I find that kind of "relations/descriptors"? Now I am just googling and copy-pasting some existing examples, but I would like to understand how to look for more specific things.
Thanks in advance
Your first query isn't working because Politician is not part of the default (:) namespace, but instead it is present in DBpedia Ontology namespace (dbo).
So, your query should be:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT ?thing WHERE {
?thing a dbo:Politician .
?thing dbo:birthPlace dbpedia:Italy.
}
LIMIT 50
To list all politician who ruled Italy you would need to know which is the predicate for "ruled". Once you have it you can construct a query.
To list all predicates present in the database you can write something like this
SELECT DISTINCT(?b) WHERE {
?a ?b ?c.
}
And it will list all predicates.
I would recommend you to browse through one or two politician and see the predicates they have to check if one works for you.

Simple SPARQL query of foaf RDF not working

I'm new at semantic web, where I learn basics, but I'm stuck at the beginning.
After reading this link: http://www.cambridgesemantics.com/semantic-university/sparql-by-example, I try some examples by myself, but without success.
For example, using OpenLink Virtuoso SPARQL Query Editor http://demo.openlinksw.com/sparql, I put at Default Graph URI this http://njh.me/foaf.rdf and simple query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
select ?name
where
{
?person foaf:name ?name .
}
to get name of a person who is owner of this RDF file. Also, if I put following query
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
select ?name
where
{
?OnlineAcount foaf:name ?name .
}
nothing is happening.
Where I make mistake?
The same situation is with https://ai.wu.ac.at/~polleres/foaf.rdf, http://richard.cyganiak.de/foaf.rdf for the first query.
Any help at the beginning of my semantic tour will be appreciated :)
Thanks in advance.
Below the query input pane, there's an option for Sponging. You need to select the option Retrieve remote RDF data for all missing source graphs. Then you'll get results. However, the file that you mentioned actually has lots of foaf:name triples, not just one for the creator of the file. You might try select ?person ?name to see what else is in that data.