rdflib query sparql cause error with group by - sparql

I am trying to run a sparql query on a local rdf dataset. The dataset is parsed successfully. The following query containing the group by clause gives the exception 'Cannot eval thing: None (<class 'NoneType'>)'
select (COUNT(?person) as ?cnt) ?name where {
?person <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://swrc.ontoware.org/ontology#Person> .
?person <http://swrc.ontoware.org/ontology#affiliation> ?aff .
# ?aff a <http://swrc.ontoware.org/ontology#affiliation>.
?aff <http://swrc.ontoware.org/ontology#name> ?name .
} group by(?name)
If I remove the brackets from the variable name in the group by clause like below the query works fine without any errors.
select (COUNT(?person) as ?cnt) ?name where {
?person <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://swrc.ontoware.org/ontology#Person> .
?person <http://swrc.ontoware.org/ontology#affiliation> ?aff .
# ?aff a <http://swrc.ontoware.org/ontology#affiliation>.
?aff <http://swrc.ontoware.org/ontology#name> ?name .
} group by ?name
I cannot see why the brackets would cause an error or how I should use rdflib to work with that format too. Any help would be really appreciated!

Related

i want to get the names of similar types using sparql queries from dbpedia

I need to find the names of similar types from DBpedia so I'm trying to figure out a query which can return me the names of entities which have same subject type in its dct:subject (example I want to find similar types of white house so i want to write a query for same . I'm considering the dct:subject to find them ). If there is any other approach please mention it
Previously I tried it for rdf:type but the result are not so good and some time it shows time out
I have done my problem by the query mentioned below and now i want to consider dct:subject instead of rdf:type
select distinct ?label ?resource count(distinct ?type) as ?score where {
values ?type { dbo:Thing dbo:Organization yago:WikicatIslam-relatedControversies yago:WikicatIslamistGroups yago:WikicatRussianFederalSecurityServiceDesignatedTerroristOrganizations yago:Abstraction100002137 yago:Act100030358 yago:Cabal108241798 yago:Group100031264 yago:Movement108464601 yago:PoliticalMovement108472335
}
?resource rdfs:label ?label ;
foaf:name ?name ;
a ?type .
FILTER (lang(?label) = 'en').
}
ORDER BY DESC(?score)

Why does this SPARQL query give wrong results?

I tried doing some sparql requests on http://dbpedia.org/sparql.
My sparql-request is this:
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?Name ?Todestag ?person
WHERE {
?person dbo:deathPlace :Hamburg .
?person foaf:name ?Name .
?person dbo:deathDate ?Todestag .
FILTER ( ?Todestag > "2016-01-01"^^xsd:date ) .
} ORDER BY ?Todestag
The problem:
Somehow this FILTER doesn’t work. The SPARQL request gives me all people who died on every day since the start of time in DBpedia. However, I just want people who died after 2016. Can anyone spot the mistake in the query or the syntax?
Here is the correct solution to the query, note the casting applied to the FILTER CLAUSE ( i.e. xsd:date(?Todestag ) :
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?Name ?Todestag ?person
WHERE {
?person dbo:deathPlace :Hamburg .
?person foaf:name ?Name .
?person dbo:deathDate ?Todestag .
FILTER ( xsd:date(?Todestag) > "2016-01-01"^^xsd:date ) .
} ORDER BY ?Todestag
Live Query Solution Page (format HTML Table).
Query Definition Page
Okay, I figured it out myself. The problem is definitely the filter. I have now changed the filter and the desired result appears. The filter must be: FILTER (str(?Todestag) >= "2016") .
Completely, it would look like this:
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?Name ?Todestag ?person
WHERE {
?person dbo:deathPlace :Hamburg .
?person foaf:name ?Name .
?person dbo:deathDate ?Todestag .
FILTER (str(?Todestag) >= "2016") .
} ORDER BY ?Todestag

Retrieving data from blank nodes in Wikidata

I am attempting to retrieve data about the lifespans of certain people. This is problematic in cases of people that have lived a while ago. The dataset for e.g. Pythagoras seems to have a so called "blank node" for date of birth (P569). But this blank node references another node earliest date (P1319) which has data I could work with just fine.
But for some reason I am not able to retrieve that node. My first try looked like this, but somehow that results in a completly empty result set:
SELECT DISTINCT ?person ?name ?dateofbirth ?earliestdateofbirth WHERE {
?person wdt:P31 wd:Q5. # This thing is Human
?person rdfs:label ?name. # Name for better conformation
?person wdt:P569 ?dateofbirth. # Birthday may result in a blank node
?dateofbirth wdt:P1319 ?earliestdateofbirth # Problem: Plausbible Birth
}
I then found another Syntax that suggested using ?person wdt:P569/wdt:P1319 ?earliestdateofbirth as some kind of "shortcut"-syntax for the explicit navigation I did above but this also ends with a empty result set.
SELECT DISTINCT ?person ?name ?dateofbirth ?earliestdateofbirth WHERE {
?person wdt:P31 wd:Q5. # Is Human
?person rdfs:label ?name. # Name for better conformation
?person wdt:P569/wdt:P1319 ?earliestdateofbirth.
}
So how do I access a node referenced by a blank node (in my case specifically the earliest birthdate) in Wikidata?
But this blank node references another node…
Things are slightly different. The earliest date property is not a property of _:t550690019, but rather is a property of the statement wd:Q10261 wdt:P569 _:t550690019.
In the Wikidata data model, these annotations are expressed using qualifiers.
Your query should be:
SELECT DISTINCT ?person ?name ?dateofbirth ?earliestdateofbirth WHERE {
VALUES (?person) {(wd:Q10261)}
?person wdt:P31 wd:Q5. # --Is human
?person rdfs:label ?name. # --Name for better conformation
?person p:P569/pq:P1319 ?earliestdateofbirth.
FILTER (lang(?name) = "en")
}
Try it!
By the way, time precision (which is used when date of birth is known) is yet another qualifier:
SELECT ?person ?personLabel ?value ?precisionLabel {
VALUES (?person) {(wd:Q859) (wd:Q9235)}
?person wdt:P31 wd:Q5 ;
p:P569/psv:P569 [ wikibase:timeValue ?value ;
wikibase:timePrecision ?precisionInteger ]
{
SELECT ?precision (xsd:integer(?precisionDecimal) AS ?precisionInteger) {
?precision wdt:P2803 ?precisionDecimal .
}
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
Try it!

SPARQL Query DBpedia Getting Multiple Values

I am new to DBPedia SPARQL Query, I am currently using the http://dbpedia.org/snorql to test queries.
My query looks like this
SELECT ?name ?school ?person
WHERE {
?person dbo:almaMater :Harvard_University .
?person foaf:name ?name .
?person dbo:birthDate ?birth .
?person dbo:country ?country .
?person dbo:almaMater ?school .
FILTER (?birth > "1980-01-01"^^xsd:date) .
} ORDER BY ?name
And below is my result
From the result above, looks like the name "Mingze Xi"#en was repeated thrice. I checked on the link under the person field and it shows that name "Mingze Xi"#en has attended Harvard University, Hangzhou ... and Zhejiang University.
Is there a way for me to query and show that under this name the person has attended these schools? I need this because there is no unique ID that I can use to indicate that this is the same person.

Retrieve data from linkedgeodata.org

I am trying to retrieve data from linkedgeodata.org/sparql
Prefix lgdo: <http://linkedgeodata.org/ontology/>
Select *
From <http://linkedgeodata.org>
{
?s a lgdo:Restaurant .
?s rdfs:label ?l .
?s geo:geometry ?g .
Filter(bif:st_intersects (?g, bif:st_point (48.143889, 17.109722), 5.1)) .
}
But response is empty. I want to retrieve restaurants in Bratislava....5km from coordinates.
I used similar sparql code like in the example, I changed only class to restaurant and coordinates of the city, so I dont know where I am doing mistake.(http://linkedgeodata.org/OnlineAccess/SparqlEndpoints?v=bpg)
Prefix lgdo: <http://linkedgeodata.org/ontology/>
Select *
From <http://linkedgeodata.org>
{
?s a lgdo:Amenity .
?s rdfs:label ?l .
?s geo:geometry ?g .
Filter(bif:st_intersects (?g, bif:st_point (12.372966, 51.310228), 0.1)) .
}
You can see all types of things that fall within those coordinates by running the following query:
Prefix lgdo: <http://linkedgeodata.org/ontology/>
Select ?type, count(?s)
From <http://linkedgeodata.org>
{
?s a ?type .
?s rdfs:label ?l .
?s geo:geometry ?g .
Filter(bif:st_intersects (?g, bif:st_point (48.143889, 17.109722), 5.1)) .
} GROUP BY ?type
This query, using GROUP BY and COUNT, gives you counts for all different types. As you can see there are no restaurants falling in geographical area. Your query is not wrong, the database does not contain any restaurants for the given coordinates.