Finding linked topics in DBPedia using SPARQL - sparql

Consider we have these two topics:
http://dbpedia.org/page/Jason_Furman
and
http://dbpedia.org/page/London
For the first topic, Jason Furman, we can see on the property almaMater that he went to:
http://dbpedia.org/page/London_School_of_Economics
And looking at London School of Economics we see that it has London as value of the city property.
So my question is:
If we have two topics, A and B, how can we find property values of A that has topic B in any of it's properties?

select distinct ?a ?p ?y ?q ?b where {
values ?a { dbpedia:Jason_Furman }
values ?b { dbpedia:London }
?a ?p ?y .
?y ?q ?b .
}
SPARQL results
(Note that there are two results because there are two such properties: dbpprop:almaMater and dbpedia-owl:almaMater.)

Related

Counting SparQL subjects with certain properties without Nestation

I have a SparQL query which first gets me the subjects with one of two predicates.
Then, I group them by a third predicate (that can also be blank) and count the grouped contained subjects.
How (if possible) can I do this without nestation?
My Query currently looks like this:
SELECT ?d (COUNT(?a) AS ?aCount)
WHERE{
SELECT ?d ?a
WHERE {
?a ?b ?c
FILTER (?b=:x||?b=:y)
OPTIONAL {?a :z ?d}
}
GROUP BY ?a ?d
}
GROUP BY ?d
This works, as far as I know, but is with nestation.
Thanks!

Type of returned value in SPARQL Query

Is it possible to know the type of the return values in a SPARQL query?
For example, is there a function to define the type of ?x ?price ?p
in the following query?
SELECT DISTINCT ?x ?price ?p
WHERE {
?x a :Product .
?x :price ?price .
?x ?p ?o .
}
I want to know that
typeOf(x) = resource
typeOf(?p) = property
typeOf(?price) = property target etc.
datatype(?x)
The datatype function will tell you whether a result is a resource or a literal and in the latter case tell you which datatype it has exactly.
For example, the following query on https://dbpedia.org/sparql...
SELECT DISTINCT ?x ?code ?p datatype(?x) datatype(?code) datatype(?p)
WHERE {
?x a dbo:City.
?x dbo:areaCode ?code .
?x ?p ?o .
} limit 1
...will return:
x
code
p
callret-3
callret-4
callret-5
http://dbpedia.org/resource/Aconchi
"+52 623"
http://www.w3.org/1999/02/22-rdf-syntax-ns#type
http://www.w3.org/2001/XMLSchema#anyURI
http://www.w3.org/2001/XMLSchema#string
http://www.w3.org/2001/XMLSchema#anyURI
However this will not differentiate between "resource" and "property" because a resource may be a property. What you probably mean is "individual" and "property" but even a property can be treated as an individual, for example in the triple rdfs:label rdfs:label "label".
However you can always query the rdf:type of a resource, which may give you rdf:Property, owl:DatatypeProperty or owl:ObjectProperty.

Classes and subclasses in OWL for SPARQL queries with inference

I have a question with OWL and SPARQL that I can't resolve. I have defined several classes, but for the question in question only 3 are the important ones: People, Men and Women; and its definitions would be the following:
<#People> a owl:Class ;
rdfs:label "People"#en .
<#Men> a owl:Class ;
rdfs:subClassOf <#People> ;
rdfs:label "Men"#en .
<#Women> a owl:Class ;
rdfs:subClassOf <#People> ;
rdfs:label "Women"#en .
And then a data in RDF for example:
<rdf:Description rdf:about="Registration#1">
<rdfs:label>ARCHEOLOGY GRADUATE</rdfs:label>
<ex:BranchKnowledge>ARTS AND HUMANITIES</ex:BranchKnowledge>
<ex:Degree>ARCHEOLOGY GRADUATE</ex:Degree>
<ex:Men rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger">63</ex:Men>
<ex:Women rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger">99</ex:Women>
<dcterms:coverage>2015/2016</dcterms:coverage>
</rdf:Description>
If I wanted to obtain the number of Men and Women of each data, I get it with the following query:
SELECT ?X ?degree ?branch ?men ?women
WHERE {
?X ex:Degree ?degree .
?X ex:BranchKnowledge ?branch .
?X ex:Men ?men .
?X ex:Women ?women
}
If I now want to get the total number of Men and Women inferring that both are People, I had thought that since both are subclasses of People, I could make the following query:
SELECT ?X ?degree ?branch ?people
WHERE {
?X ex:Degree ?degree .
?X ex:BranchKnowledge ?branch .
?X ex:People ?people
}
However I don't get any results.
Have I misconstructed the relationship between classes and subclasses for what I want to do or what would be the problem? (I'm working on a Virtuoso server).
You should be aware of the fact that you're using punning, i.e. the same URI for Men and Women as OWL classes and OWL data properties. I don't understand why you're doing this. Why don't you introduce properties like numberOfMen, numberOfWomen and numberOfPeople?
Next, you're using ex:People as a property, thus, you would have to define that ex:Men and ex:Women are sub-properties of ex:People.
<#People> a owl:DatatypeProperty ;
rdfs:label "People"#en .
<#Men> a owl:DatatypeProperty ;
rdfs:subPropertyOf <#People> ;
rdfs:label "Men"#en .
<#Women> a owl:DatatypeProperty ;
rdfs:subPropertyOf <#People> ;
rdfs:label "Women"#en .
And then you have to enable reasoning in the triple store if supported or use SPARQL 1.1 property paths.
But, more important, this would only lead to two rows, which means you have to do the aggregation in your SPARQL query by using the sum function, e.g.:
SELECT ?X ?degree ?branch (sum(?_people) as ?people)
WHERE {
?X ex:Degree ?degree .
?X ex:BranchKnowledge ?branch .
?p rdfs:subPropertyOf* ex:People .
?X ?p ?_people
}
GROUP BY ?X ?degree ?branch
Note, this query only works if there is just a single degree and branch, otherwise it would sum duplicate values.

how group graph pattern work in SPARQL

First of all, i don't know if this really called group graph pattern or not. Anyway,
Look at this query please
select ?x ?y where {
{?x rdf:type rs:Recommendable}
union
{?xd rs:doesntexist ?y}
}
there is no rs:doesntexist but with union i got the results only from the first sub graph which is {?x rdf:type rs:Recommendable}
but if i remove the union, so the query will be:
select ?x ?y where {
{?x rdf:type rs:Recommendable}
{?xd rs:doesntexist ?y}
}
I get empty results, may I ask you please who this work?
and the weird thing to me that this query
select ?x ?y where {
{?x rdf:type rs:Recommendable}.{}
}
works perfectly so the one before it doesnt?
Update
I think the union is like optional, I'm not sure. is it right please? (and by optional i didn't mean the optional from sparql, but i meant like when extracting the data from a union two graphs, its optional that two of them have data but if one of them is empty, we'll have the data from the other)
select ?x ?y where {
{?x rdf:type rs:Recommendable}
{?xd rs:doesntexist ?y}
}
is like (not identical) to:
select ?x ?y where {
?x rdf:type rs:Recommendable
?x rs:doesntexist ?y
}
Both patterns must match. If there is no rs:doesntexist the whole thing fails.
{}
matches all (zero) its patterns so it always works.

How to select the one equals to each others in Sparql

For example,there is one simple select sparql query:
SELECT * Where
{
CT:A skos:broader ?A.
CT:B skos:broader ?B.
FILTER(?A=?B)
}
With the FILTER,it is easy to select the objects that equal to each others.
But when there are even more triples to match ,for example,from CT:A skos:broader ?A to CT:Z skos:broader ?Z,how can I filter those meet ?A=?B=?C=....?Z?
Wish to make it clear.
I think your query should be written as
select ?a where { ct:a skos:broader ?a. ct:b skos:broader ?a }
which means that you want to find an ?a whose the value of skos:broader for both ct:a and ct:b. If you want to make the equivalent for all ct:a, ct:b, ... ct:z then you just repeat the pattern:
select ?a where { ct:a skos:broader ?a. ct:b skos:broader ?a. ct:c skos:broader ?a ... ct:z skos:broader ?a }