Nested SPARQL to query collection - Dependent Lookup - sparql

I have this SPARQL query that returns the id and Label for THING X from vocabulary 1, by matching against a range of potential attribute matches (labels and notation)
PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?id ?displayText
WHERE {
VALUES ?pred {
skos:prefLabel
skos:altLabel
skos:hiddenLabel
rdfs:label
skos:notation
}
FILTER (STR(?displayText) = "THING X")
?id skos:inScheme <http://exampleuri.com/def/vocabulary1> .
?id ?pred ?displayText .
}
ORDER BY ?displayText ?concept
I have this query that returns the concepts in the collection of stuff related to THING X from vocabulary2
PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
SELECT DISTINCT ?id ?displayText
WHERE {{ <http://exampleuri.com/def/vocabulary2/THING-X-collection> skos:member ?id . ?id skos:prefLabel ?displayText }} ORDER BY ?displayText
What I need is to nest the first query into the second so that I search for THING X in vocabulary 1 and it returns the skos:members of the associated collection in vocabulary 2 (where the label of the vocab 1 concept and the vocab 2 collection are equal).

Related

SPARQL count item in an intersection

Given the following triples
#prefix ex: <http://example.org/> .
#base <http://example.com/> .
<person1> ex:has_interpretation <interpretation1> .
<interpretation1> ex:refers_to <objectA> ;
ex:resultIn <X> .
<person2> ex:has_interpretation <interpretation2> .
<interpretation2> ex:refers_to <objectA> ;
ex:resultIn <Y> .
<person2> ex:has_interpretation <interpretation3> .
<interpretation3> ex:refers_to <objectB> ;
ex:resultIn <Z> .
<person3> ex:has_interpretation <interpretation4> .
<interpretation3> ex:refers_to <objectA> ;
ex:resultIn <ZZ> .
I am trying to use SPARQL to:
count only the number of object referred to by an interpretation by both person1 and person2 (intersection)
count the number of distinct interpretations over the object
count the number of object not referred to by an interpretation by both person1 and person2
having the above count together with a list of objects referred to by an interpretation and the people who create the interpretation.
I am having trouble specifically with 1 (and consequently, 3), as I cannot find a way to count the intersection of the interpreted objects.
My current SPARQL query which does not obtain what I want:
PREFIX ex: <http://example.org/>
SELECT ?person (COUNT(distinct ?object) as ?c_object) (group_concat(distinct ?interpretation;separator="; ") as ?interpretations)
WHERE {
?person ex:has_interpretation ?interpretation .
?interpretation
ex:refers_to ?object ;
ex:resultIn ?result .
FILTER (?person = <http://example.com/person1> || ?person = <http://example.com/person2> )
}
GROUP BY ?person ?object
What instead I would like is just:
object_uri
number_object
interpretations
person_involved
<objectA>
1
<interpretation1>,<interpretation2>
<person1>,<person2>
Any ideas?
count only the number of object referred to by an interpretation by both person1 and person2 (intersection)
PREFIX ex: <http://example.org/>
SELECT DISTINCT ?obj
WHERE {
<http://example.com/person1> ex:has_interpretation/ex:refers_to ?obj .
<http://example.com/person2> ex:has_interpretation/ex:refers_to ?obj .
}
Here the query matches only those objects (?obj) which have ex:has_interpretation/ex:refers_to paths to them from both <person1> and <person2>.
count the number of distinct interpretations over the object
Just use the COUNT() function as the paths used in the answer for 1. above are distinct (different):
PREFIX ex: <http://example.org/>
SELECT DISTINCT ?obj
WHERE {
<http://example.com/person1> ex:has_interpretation/ex:refers_to ?obj .
<http://example.com/person2> ex:has_interpretation/ex:refers_to ?obj .
}
count the number of object not referred to by an interpretation by both person1 and person2
You might be able to find a fancy way to query for this but I would just count distinct objects:
PREFIX ex: <http://example.org/>
SELECT DISTINCT ?obj
WHERE {
?x ex:refers_to ?obj .
}
...and then subtract the results from 1. above. This is easy to understand and you've already got the results from 1. to work with.

Returning only label columns & population from dbpedia query

I'm new to SPARQL and I'm a bit stuck on a part of an assignment I have. I'm querying dbpedia for all the countries in the European Union which have a total population >= 3000000. For each country I'd like to show their corresponding government type. I would like the final result set to contain 3 columns:
the English label for each country
the English label for each type of government
the total population value
and then sorted descending on the total population.
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbrc: <http://dbpedia.org/resource/Category:>
SELECT DISTINCT ?country xsd:integer(?populationTotal) ?government_type ?engName ?gov_type
WHERE {
?country dct:subject dbrc:Member_states_of_the_European_Union ;
a dbo:Country ;
rdfs:label ?engName .
OPTIONAL { ?country dbo:governmentType ?government_type . ?government_type rdfs:label ?gov_type . }
OPTIONAL { ?country dbo:populationTotal ?populationTotal . }
FILTER (xsd:integer(?populationTotal) >= 3000000 && langMatches(lang(?engName), "en"))
FILTER (langMatches(lang(?gov_type), "en"))
} ORDER BY DESC(?populationTotal)
I've managed to get dbpedia to return all the info I need, however I'd like to only keep the columns above in bulletpoints and not the country column. I know there should be a way to return that country directly with the label however I'm having troubles getting any closer to a solution...

SPARQL for getting movies list with proper attributes ordered

I'm trying to get list of movies in a specific year (just say 1990) with each movie and it's attributes. I tried different ways to get SPARQL but couldn't. How do I group genres into single column?
The format I would want is with some of the properties from here
http://schema.org/Movie
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX v: <http://www.wikidata.org/prop/statement/>
PREFIX q: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?q ?film_title (GROUP_CONCAT(DISTINCT(?genreID); separator=", ") as ?genre) WHERE {
?q wdt:P31 wd:Q11424.
?q rdfs:label ?film_title filter (lang(?film_title) = "en").
?q wdt:P136 ?genreID.
?genreID rdfs:label ?genre.
?q wdt:P161 ?actorID.
?actorID rdfs:label ?actor filter (lang(?actor) = "en").
} LIMIT 100
Usage of aggregate functions usually need to build the group of things/rows on which you want to apply the function, i.e. a GROUP_BY on the projection variables that do not denote the result of an aggregate function:
SELECT ?q ?film_title (GROUP_CONCAT(DISTINCT(?genreID); separator=", ") as ?genres) WHERE {
?q wdt:P31 wd:Q11424 .
?q rdfs:label ?film_title .
FILTER (lang(?film_title) = "en")
?q wdt:P136 ?genreID .
?genreID rdfs:label ?genre .
?q wdt:P161 ?actorID .
?actorID rdfs:label ?actor
FILTER (lang(?actor) = "en")
}
GROUP BY ?q ?film_title
LIMIT 100
Some comments:
it's not clear whether you want to get the genreID or the label of the genre, for the latter indeed you have to use the variable ?genre in the GROUP_CONCAT function: GROUP_CONCAT(DISTINCT(?genre); separator=", ")
it's not allowed to re-assign a variable name that has already been used in the query, that's why I changed the name to ?genres
don't put a comma after a FILTER, I don't know why you did so
you don't select the actor, thus, it remains unclear why you use the last triple pattern + FILTER in your query
your query is quite expensive, thus, its execution might timeout

DBpedia SPARQL to eliminate unwanted data

PREFIX category: <http://dbpedia.org/resource/Category:>
SELECT DISTINCT ?attractions
?location
WHERE
{ ?attractions dcterms:subject ?places
. ?places skos:broader ?border
. ?attractions dbpprop:location|dbpedia-owl:locatedInArea|dbpprop:locale ?location
. FILTER( ?border = category:Visitor_attractions_in_Delhi )
}
I have above query giving result of attraction location of Delhi. I need to make it generic for all places, and secondly I want to filter out unwanted data. I want only attraction places, e.g., I didn't want List of Monuments and SelectCityWalk like data in my output.

sparql to retrieve the value of a min constraint

How can I retrieve a min constraint on a class' attribute using sparql? I have value min 1000 decimal, and I would like to get 1000
In a hypothetical world that you have such a statement:
Class: X subClassOf: hasObjectProperty min 1 Y
If you write a SPARQL query as:
SELECT *
WHERE {
?s rdfs:subClassOf ?o.
}
You must extract all the refs:subClassOf axioms. However, if you need to precise and know which ones have cardinality restrictions, you need to go further:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix : <http://example.com#>
SELECT *
WHERE {
?s rdfs:subClassOf ?o.
?o ?x ?y.
filter(?s = :X)
}
Among others, you can see the following result:
As you can see, there are 2 relevant items, one is Y and one is the number presented as a non-negative integer. Therefore, one way to get each item is to put a filter for ?x in the SPARQL query and get each one one by one out. For example, filter owl:onClass will give you ?y:
prefix : <http://example.com#>
SELECT *
WHERE {
?s rdfs:subClassOf ?o.
?o owl:onClass ?y.
filter(?s = :X)
Here is the sparql query I used following Artemis' answer
SELECT ?min
WHERE {?s rdfs:subClassOf ?o.
?o owl:minQualifiedCardinality ?min.
FILTER(?s = :value) }
And with jena, I use getLiteral("min").getFloat();