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

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
}

Related

How can I use SPARQL to find instances where two properties do not share any of the same objects?

In my company's taxonomy, all concepts have a value for skos:prefLabel, and most have a bunch of values for a custom property -- let's call it ex:keyword -- whose values have the datatype rdf:langString. I want to find concepts where the value of skos:prefLabel does NOT exactly match ANY value of ex:keyword, when case and language are ignored.
It is fairly straightforward to write a query for concepts where the values DO match, like this:
SELECT *
WHERE {
?concept skos:prefLabel ?label ;
ex:keyword ?kw
FILTER (lcase(str(?label)) = lcase(str(?kw)))
}
Where I'm getting tripped up is in trying to negate this.
Using != in the FILTER would just return a bunch of cases where ?label and ?kw don't match, which is not what I'm after.
What I would like is to be able to use a FILTER NOT EXISTS, but that's invalid with an expression like (?a = ?b); it only works with something like {?a ?b ?c}.
I suspect that there is a proper way to express FILTER NOT EXISTS (?a = ?b) in SPARQL, but I don't know what it is. Can someone help?
The trick is to put the triple pattern for matching the keyword in the FILTER NOT EXISTS, like so:
SELECT *
WHERE {
?concept skos:prefLabel ?label .
FILTER NOT EXISTS { ?concept ex:keyword ?kw .
FILTER(lcase(str(?label)) = lcase(str(?kw)))
}
}
This query says "I want all concepts with a preflabel such that the concept has no keyword value that matches that prefLabel".

SPARQL How to get the type of an individual

I made this ontology:
SensorOntology:MedicalCabinet-01 rdf:type owl:NamedIndividual ,
SensorOntology:MedicalCabinetSensor ;
SensorOntology:hasId "57"^^xsd:int ;
SensorOntology:hasValue "0"^^xsd:int .
I am trying to write a query to get the type of MedicalCabinet-01 which is MedicalCabinetSensor by given id number. However, I am printing owl:NamedIndividual and SensorOntology:MedicalCabinetSensor. Is it anyway I print just SensorOntology:MedicalCabinetSensor. This is my query:
SELECT DISTINCT ?sensor ?sensorclass
WHERE {?sensor :hasId "100"^^xsd:int.
?sensor rdf:type ?sensorclass}
There are multiple ways to ignore results, so it's not clear which one you want to use - especially since some are quite obvious. Here are two options:
As you tagged it with Jena, use Java and ignore resources whose URI matches owl:NamedIndividual.
Use a FILTER in SPARQL, i.e. add FILTER(?sensorclass != owl:NamedIndividual) to the graph pattern of the SPARQL query.
Indeed, there are multiple ways. Here's another:
SELECT DISTINCT ?sensor ?sensorclass
WHERE {?sensor :hasId "100"^^xsd:int.
?sensor rdf:type ?sensorclass
FILTER ( strstarts(str(?sensorclass), "[sensor ontology URI]") )
}

SPARQL search query

I have some RDF data structured like this:
[ pref:ip_address "127.0.0.1" ;
pref:time "1459630844.482" ;
pref:url "https://google.com" ;
pref:user "johndoe"
] .
I need query that will return all results matching given IP, time frame (between from time and end time), url (even partial match) and user (also even partial match).
What I have now is simple query to get results based on single value, like this:
PREFIX pref: <http://something> SELECT DISTINCT * WHERE { ?u pref:user USER_VALUE . ?u ?p ?o . }
This returns all results with given user but only if given username is full match. Meaning it will return all results for johndoe if USER_VALUE is johndoe but not if it is john.
My knowledge of SPARQL is extremely limited and I appreciate any help. Thank you.
To do anything more than exact match, you need to use a FILTER and use operations like CONTAINS or REGEX.
Example:
{ ?u pref:user ?user .
?u ?p ?o .
FILTER( CONTAINS(?user, "john") )
}
There are a number of FILTER functions that may be useful including REGEX. See the spec for details.

SPARQL query failing to match literal on Pubchem RDF data

I have loaded part of the PubChem RDF data in Virtuoso and am trying to use SPARQL to query it, through iSQL.
While the following query works:
SELECT ?syno ?type ?value
WHERE {
?syno sio:is-attribute-of <http://rdf.ncbi.nlm.nih.gov/pubchem/compound/CID1829049> .
?syno rdf:type ?type .
?syno sio:has-value ?value .
} LIMIT 10;
I am not able to get any results for a query like (the value being taken from one of the above results):
SELECT ?syno
WHERE {?syno sio:has-value "AC1LXI26"};
In the previous case, I am simply trying to match a litteral.
Do I need to build an extra index? Is exact text match not supported in Virtuoso?
I solved my problem by simply adding #en at the end of the query string!
SELECT ?syno
WHERE {?syno sio:has-value "AC1LXI26"#en};

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.