Get birthplace and date of American investors from DBpedia people in SPARQL - sparql

I'm trying to get a list of Investors from the US, their birth dates, and places.
But the SPARQL code below doesn't seem to work. I tried it on both
DBpedia pages:
https://live.dbpedia.org/sparql or
https://dbpedia.org/snorql/
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?name ?birthPlace ?birthDate
WHERE {
?person a dbo:BusinessPerson, dbo:Entrepreneur, dbo:Investor .
?person dbo:birthPlace ?birthPlace .
?person dbo:birthDate ?birthDate .
?person dbo:country dbr:United_States .
?person rdfs:label ?name .
FILTER(LANG(?name) = 'en')
}
Can't find any good literature on this subject either so having trouble understanding what I should alter.
All advice is appreciated.

Try the following query here:
SELECT distinct ?person ?label ?birthPlace ?birthDate
WHERE {
{
?person rdf:type dbo:Person.
?person ?p dbr:Investor.
?person dbo:nationality dbr:United_States.
?person rdfs:label ?label. FILTER (lang(?label) = 'en')
OPTIONAL{?person dbp:birthPlace ?birthPlace.}
OPTIONAL{?person dbo:birthDate ?birthDate }
}
Union
{
?person rdf:type dbo:Person.
?person ?p dbo:Entrepreneur.
?person dbo:nationality dbr:United_States.
?person rdfs:label ?label. FILTER (lang(?label) = 'en')
OPTIONAL{?person dbp:birthPlace ?birthPlace.}
OPTIONAL{?person dbo:birthDate ?birthDate }
}
Union
{
?person rdf:type dbo:Person.
?person a dbo:BusinessPerson.
?person dbo:nationality dbr:United_States.
?person rdfs:label ?label. FILTER (lang(?label) = 'en')
OPTIONAL{?person dbp:birthPlace ?birthPlace.}
OPTIONAL{?person dbo:birthDate ?birthDate }
}
}
you can also try this equivalent query on wikidata SPARQL endpoint here:
SELECT ?person ?personLabel ?dob ?pobLabel
WHERE
{
?person wdt:P31 wd:Q5.
?person wdt:P106 wd:Q557880.
?person wdt:P27 wd:Q30.
OPTIONAL {?person wdt:P569 ?dob.}
OPTIONAL {?person wdt:P19 ?pob. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Related

Bad aggregate in Sparql request for Wikidata

I try to get all the (famous because referenced in wikidata) people alive and their occupation (concatenated), but the query engine query editor disclaim a "syntax" problem that i can't debugg. Could you help me to re-arrange the request to make it work?
SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?occupationLabel;separator=", ") AS ?positions)
WHERE {
?person wdt:P106 ?occupation.
?person wdt:P27 wd:Q142.
FILTER NOT EXISTS {?person wdt:P570|wdt:P509|wdt:P20 ?o}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
}
GROUP BY ?person
To fix this, you can add all your non-aggregated variables to the GROUP BY clause:
SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?occupationLabel;separator=", ") AS ?positions)
WHERE {
?person wdt:P106 ?occupation.
?person wdt:P27 wd:Q142.
FILTER NOT EXISTS {?person wdt:P570|wdt:P509|wdt:P20 ?o}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
}
GROUP BY ?person ?personLabel ?personDescription
However, I wasn't able to get that to run without timing out. Using a query hint to turn off the query optimizer and moving this around allowed the query to run:
SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?occupationLabel;separator=", ") AS ?positions)
WHERE {
hint:Query hint:optimizer "None".
{
?person wdt:P27 wd:Q142.
FILTER NOT EXISTS {?person wdt:P570|wdt:P509|wdt:P20 ?o}
}
?person wdt:P106 ?occupation.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
}
GROUP BY ?person ?personLabel ?personDescription
At this point, we can see that there isn't any data for the ?positions variable.
That suggests to me that Wikidata's magical population of the Label variables (e.g. ?occupationLabel) might not be working with the aggregation.
Pulling the aggregation out into a new top-level query, and doing the matching in a subquery allows the variables to be assigned correctly, but I still had trouble getting this to not timeout:
SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?occupationLabel;separator=", ") AS ?positions) WHERE {
SELECT ?person ?personLabel ?personDescription ?occupationLabel
WHERE {
hint:Query hint:optimizer "None".
{
?person wdt:P27 wd:Q142.
FILTER NOT EXISTS {?person wdt:P570|wdt:P509|wdt:P20 ?o}
}
?person wdt:P106 ?occupation.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
}
}
GROUP BY ?person ?personLabel ?personDescription
Some careful optimization (or execution in parts) may be required to get this to fully execute. For example, limiting to 1000 (non-deterministic) person-occupation pairs will run without timing out:
SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?occupationLabel;separator=", ") AS ?positions) WHERE {
SELECT ?person ?personLabel ?personDescription ?occupationLabel
WHERE {
hint:Query hint:optimizer "None".
{
?person wdt:P27 wd:Q142.
FILTER NOT EXISTS {?person wdt:P570|wdt:P509|wdt:P20 ?o}
}
?person wdt:P106 ?occupation.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }
}
LIMIT 1000
}
GROUP BY ?person ?personLabel ?personDescription

Wikidata query throwing StackOverflowError after update

Before, my query worked perfectly. However, something changed on their backend, and now I'm getting an error when running this query.
SELECT ?person ?personLabel ?personDescription (GROUP_CONCAT(DISTINCT ?dob ; SEPARATOR = ' | ') AS ?dob) (GROUP_CONCAT(DISTINCT ?gender ; SEPARATOR = ' | ') AS ?gender) (GROUP_CONCAT(DISTINCT ?image ; SEPARATOR = ' | ') AS ?image)
WHERE {
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:api "EntitySearch" .
bd:serviceParam wikibase:endpoint "www.wikidata.org" .
bd:serviceParam mwapi:search "william" .
bd:serviceParam mwapi:language "en" .
?person wikibase:apiOutputItem mwapi:item .
}
?person wdt:P31 wd:Q5 .
OPTIONAL { ?person wdt:P569 ?dob }
OPTIONAL { ?person wdt:P21 ?gender }
OPTIONAL { ?person wdt:P18 ?image }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
GROUP BY ?person ?personLabel ?personDescription

SPARQL - Extracting Director and Starring from DBPedia

Could anyone please help me with this code. It doesn't come out with any output but with no error.
q = """SELECT DISTINCT ?label ?abstract ?director ?starring
WHERE {
<http://dbpedia.org/resource/Seven_Beauties> rdfs:label ?label.
?label dbo:abstract ?abstract.
?label dbo:director ?director.
?label dbo:starring ?starring.
FILTER (lang(?label) = "en")
FILTER (lang(?abstract) = "en")
}"""
from textwrap import wrap
for result in query(q):
print(result['label'],
"\n----Director----\n",result['director'],
"\n----Starring----\n",result['starring'],
"\n----Abstract----\n",
"\n".join(wrap(result['abstract'])))
<http://dbpedia.org/resource/Seven_Beauties> is the entity which has a Label, Abstract, Director, Star, etc.
The Label of that entity is a literal, and it has no Director, Star, Abstract, etc.
Try changing your query to --
SELECT DISTINCT ?label ?abstract ?director ?starring
WHERE
{
<http://dbpedia.org/resource/Seven_Beauties>
rdfs:label ?label ;
dbo:abstract ?abstract ;
dbo:director ?director ;
dbo:starring ?starring
FILTER ( lang(?label) = "en" )
FILTER ( lang(?abstract) = "en" )
}
-- and see results from DBpedia.
DBpedia Live delivers nothing for the query above -- because the descriptions of films vary. Making some things optional will get you more results on DBpedia Live --
SELECT DISTINCT ?label ?abstract ?director ?starring
WHERE
{
<http://dbpedia.org/resource/Seven_Beauties> rdfs:label ?label
OPTIONAL { <http://dbpedia.org/resource/Seven_Beauties> dbo:abstract ?abstract }
OPTIONAL { <http://dbpedia.org/resource/Seven_Beauties> dbo:director ?director }
OPTIONAL { <http://dbpedia.org/resource/Seven_Beauties> dbo:starring ?starring }
FILTER ( lang(?label) = "en" )
FILTER ( lang(?abstract) = "en" )
}

sparql count not existing properties as zero

Here is my query:
PREFIX : <http://example.org/ns#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?id ?name (count(?s) as ?count)
WHERE {
?t a :Tag ;
:hasId ?id ;
:hasName ?name
OPTIONAL { ?s :hasTag ?t ;
rdf:type ?type }
FILTER (?type in (:Client, :Project, :Staff))
} GROUP BY ?id ?name
Tags with no objects are not included in the result. How can I get them also without using union?
Performance is also important
The goal is to gather information about tags (id, name) and number of objects they assign to (if no such objects count must be 0). Tag data sample:
:tag912 :hasId "912"^^xsd:integer
:tag912 :hasName "Phones"
This tag is assigned to 6 objects.
This query works for me:
PREFIX : <http://example.org/ns#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?id ?name ?count
WHERE {
{
SELECT ?id ?name (count(?s) as ?count)
WHERE {
?t a :Tag ;
:hasId ?id ;
:hasName ?name .
?s :hasTag ?t ;
rdf:type ?type
FILTER (?type in (:Client, :Project, :Staff))
} GROUP BY ?id ?name
} UNION {
SELECT ?id ?name (0 as ?count)
WHERE {
?t a :Tag ;
:hasId ?id ;
:hasName ?name
FILTER not exists { ?s :hasTag ?t }
}
}
}
How can I use bindings here? Will it improve performance?
Thank you
Why not use UNION?
Aside from that, your query is focused on ?t tags and their ?id and ?name — so it's not surprising that results don't include ?s objects which lack ?t and hence lack any ?id and ?name…
I think this may get you going in the right direction —
PREFIX : <http://example.org/ns#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?id
?name
(count(?s) as ?count)
WHERE
{ ?s rdf:type ?type
FILTER ( ?type IN ( :Client, :Project, :Staff ) ) .
OPTIONAL { ?s :hasTag ?t .
?t rdf:type :Tag ;
:hasId ?id ;
:hasName ?name }
}
GROUP BY ?id ?name
You can check if ?s is bound in your filter:
PREFIX : <http://example.org/ns#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?id ?name (COUNT(?s) as ?count)
WHERE {
?t a :Tag ;
:hasId ?id ;
:hasName ?name .
OPTIONAL {
?s :hasTag ?t ;
rdf:type ?type .
}
FILTER (!BOUND(?s) || ?type in (:Client, :Project, :Staff))
} GROUP BY ?id ?name
I don't know if this is faster than a union (or a bunch of unions), however.

sparql - get a list of cities in certain country from dbpedia

I want to get triples about cities, which are from certain country. How can I do that?
I tried:
CONSTRUCT { ?c rdfs:label ?name . ?c rdfs:comment ?desc }
WHERE {
?c dbpprop:wikiPageUsesTemplate <http://dbpedia.org/resource/Template:Infobox_settlement> .
?c rdfs:label ?name .
?c rdfs:comment ?desc .
?c <http://dbpedia.org/ontology/country> ?country . ?country a <http://dbpedia.org/resource/CountryName>
FILTER ( lang(?name) = "en" && lang(?desc) = "en" )
}
but no luck :/ how can i do this?
CONSTRUCT { ?c rdfs:label ?name }
WHERE {
?c dbpprop:wikiPageUsesTemplate <http://dbpedia.org/resource/Template:Infobox_settlement> .
?c rdfs:label ?name .
?c dbpedia-owl:country <http://dbpedia.org/resource/Country> .
OPTIONAL { ?c dbpedia-owl:areaCode ?areacode }
FILTER ( lang(?name) = "pl" && ?population > 5000)
}
Hope it will help :)