DBPedia: all population fields? - sparql

I am trying to extract all entities in DBPedia that have a population. However, I have found that there are different field names for population depending on the entity. For instance, http://dbpedia.org/page/Boston has the field populationTotal while http://dbpedia.org/page/Alaska has the field 2010pop. Is there a complete list of the population fields that I can query for?

Solution via #AKSW above: query for all properties that start with "pop" and have a specified range.
SELECT ?p ?range {
?p a rdf:Property
FILTER(regex(str(?p), "pop"))
OPTIONAL {?p rdfs:range ?range}
}

Related

How to get all property from an item based on its Q number?

I want to gather all properties of a item from wikidata.
All queries I see so far assume you know properties you are looking for, but in my case, I'm not.
For example, when querying for Q1798740, I would like a returned value that looks like
[{"item": "Q1798740",
"P31": ["Q1146"],
"P17": ["Q70972"],
...
"P2043":"70 metres"}
]
and that contains all statements from the wikidata page
What query should I perform?
You need only to ask for {wd:Q1798740 ?p ?value} but it would be useful also to get the labels of the properties, which is a bit trickier:
SELECT DISTINCT ?p ?property_label ?value
WHERE
{
wd:Q1798740 ?p ?value .
?property wikibase:directClaim ?p ;
rdfs:label ?property_label .
FILTER(LANG(?property_label)="en")
}

Getting a list of available hierarchies from statistics.gov.scot

I'm interested in obtaining a list of available distinct hierarchies from statistics.gov.scot. The best-fit hierarchies, which I would like to list, are as follow:
http://statistics.gov.scot/def/hierarchy/best-fit#community-health-partnership
http://statistics.gov.scot/def/hierarchy/best-fit#council-area
http://statistics.gov.scot/def/hierarchy/best-fit#country
As available through API section of this sample geography.
Desired results
I would like for the desired results to return:
community-health-partnership
council-area
country
How can I construct query that would actually produce that, I can get a list of available all geographies via:
PREFIX sdmx: <http://purl.org/linked-data/sdmx/2009/dimension#>
SELECT DISTINCT ?framework
WHERE {
?a sdmx:refArea ?framework .
} LIMIT 10
I was trying something on the lines:
PREFIX fits: <http://statistics.gov.scot/def/hierarchy/best-fit#>
SELECT DISTINCT ?framework
WHERE {
?a fits ?framework .
} LIMIT 10
but naturally this syntax is not correct.
Starting on their SPARQL endpoint, you could do something like this --
DESCRIBE <http://statistics.gov.scot/def/hierarchy/best-fit#country>
Then, based on those results, you might try something like this, which results aren't exactly what you say you want, but might be better --
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?hierarchy
?label
WHERE
{ ?hierarchy rdfs:subPropertyOf <http://statistics.gov.scot/def/hierarchy/best-fit>
; rdfs:label ?label
}

Querying for date range in SPARQL

I have some data in a semantic database that looks like the following, where the first column is the ID of an object, and the second column is the last modified date, as xsd:dateTime's.
?s ?last_mod_date
http://company.com/custom.xml#obj1, 2016-08-30T08:44:49.000-04:00
http://company.com/custom.xml#obj2, 2016-08-30T17:24:21.000-04:00
http://company.com/custom.xml#obj3, 2016-08-30T09:03:57.000-04:00
http://company.com/custom.xml#obj4, 2016-07-27T03:26:44.000-04:00
http://company.com/custom.xml#obj5, 2016-08-11T03:23:53.000-04:00
http://company.com/custom.xml#obj6, 2016-07-19T03:05:03.000-04:00
I'm trying to filter this list of objects down to one item by date; my query input is unfortunately only precise to the minute, so I'm trying to use a date range to find the object, like this:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix cust: <http://company.com/custom.xml#>
SELECT ?s ?date WHERE
{
?s cust:last_mod_date ?date.
BIND("2016-08-30T09:03:00.000-0400"^^<http://www.w3.org/2001/XMLSchema#dateTime> as ?minDate).
BIND("2016-08-30T09:04:00.000-0400"^^<http://www.w3.org/2001/XMLSchema#dateTime> as ?maxDate).
FILTER(?date > ?minDate && ?date < ?maxDate)
}
The above query should find obj3, but instead it finds nothing. This is with a Sesame semantic database. Any ideas why this would be?
Your datetimes in the SPARQL query are malformed:
BIND("2016-08-30T09:03:00.000-0400"^^<http://www.w3.org/2001/XMLSchema#dateTime> as ?minDate).
BIND("2016-08-30T09:04:00.000-0400"^^<http://www.w3.org/2001/XMLSchema#dateTime> as ?maxDate).
Should be
BIND("2016-08-30T09:03:00.000-04:00"^^<http://www.w3.org/2001/XMLSchema#dateTime> as ?minDate).
BIND("2016-08-30T09:04:00.000-04:00"^^<http://www.w3.org/2001/XMLSchema#dateTime> as ?maxDate).
The timezone modifier is the first BIND statements are missing a colon.

Good SPARQL query to find all triples with a resource as subject or object

I need to find all triples on DBpedia where http://dbpedia.org/resource/Benin is a subject or object. This query gives me the output that I want in a format that works the best for me (just three variables and no blank spaces):
PREFIX : <http://dbpedia.org/resource/>
SELECT * WHERE {
?s ?p ?o
FILTER (?s=:Benin OR ?o=:Benin)
}
I get similar results if I have this query:
PREFIX : <http://dbpedia.org/resource/>
SELECT * WHERE {
{:Benin ?p ?o}
UNION
{?s ?p :Benin}
}
However, the formatting of the latter is off. It first gives me p and o output leaving s blank and then s and p leaving o blank. Also, the first query takes more time to execute. I will be grateful for an explanation of the mechanics of how the two queries work and why there is a difference in the output.
However, the formatting of the latter is off
That's because both queries have different result sets together with SELECT *. The union joins the tuples, but since some tuples are missing parts, you get skewed output.
You can resolve the problem by explicitly listing and selecting the variables:
PREFIX : <http://dbpedia.org/resource/>
SELECT ?s ?p ?o WHERE {
{
?s ?p ?o
FILTER (?s=:Benin)
}
UNION
{
?s ?p ?o .
FILTER (?o=:Benin)
}
}
Note that this is still much faster on dbpedia than the OR filter.
The union will return duplicates when a tuple matches both filter expressions (i.e. :Benin ?p :Benin).
SELECT DISTINCT would remedy that at additional cost and since it looks like the problem is non-existent, I omitted it for improved performance.
Also, the first query takes more time to execute.
That's hard to say without the result of an EXPLAIN(), but my first guess would be that the equality filter is using the index, while the OR filter is using a full table scan. Virtuoso does not seem to generate good query plans for nested filters.
Try this --
PREFIX : <http://dbpedia.org/resource/>
DESCRIBE :Benin
-- or just --
DESCRIBE <http://dbpedia.org/resource/Benin>
You can get the output in various other serializations, including N-triples.

How do i make sparql query only about data type name?

How to display data's property label?
I working for dbpedia ontology,
I want to make a sparql query, below is my sample query. This result is mix up either datatype or object type, I want to datatype property name.
SELECT ?p ?pLabel ?domain ?range
{
?p rdfs:domain http://dbpedia.org/ontology/Person> .
}
ex: Following is data type example, but I cannot select only datatype, I want to display
type name.
"chat"
'chat'#fr with language tag "fr"
"xyz"^^<http://example.org/ns/userDatatype>
"abc"^^appNS:appDataType
'''The librarian said, "Perhaps you would enjoy 'War and Peace'."'''
1, which is the same as "1"^^xsd:integer
1.3, which is the same as "1.3"^^xsd:decimal
1.300, which is the same as "1.300"^^xsd:decimal
1.0e6, which is the same as "1.0e6"^^xsd:double
true, which is the same as "true"^^xsd:boolean
false, which is the same as "false"^^xsd:boolean
expect to result
Expect to result (only data type)
typename <- field name
string <- type name
int
boolean
int
double
boolean
How to make a sparql query?
Use function datatype() for that purpose. For example:
select distinct ?y datatype(?z)
{
?x a <http://dbpedia.org/class/yago/JeskolaBuzzUsers>.
?x ?y ?z.
filter (datatype(?z) != '')
}
PREFIX xsd: http://www.w3.org/2001/XMLSchema#
ASK WHERE
{
?item dm:amount ?amount .
FILTER ((datatype(?amount)) != xsd:integer)
}
The query engine still knew which ?amount values were integers and which were not,
because any unquoted series of digits with no period is treated as an integer.
Most of your work with datatypes in SPARQL will involve the use of functions that are
covered in more detail in the next section. Before we look at any of those, it’s a good
idea to know how representations of typed literals in your queries interact with different
kinds of literals in your dataset.