SPARQL Querying Transitive different versions of arq - sparql

Basically I got a SPARQL query below which works with the arq 2.8.8 but doesn't work with arq2.8.4 as it doesnt recognise the + symbol. I really want a query which can work on the arq 2.8.4 version which is similar to the one I posted. The query I posted basically finds all items which are the sameas each other. For eg if a is the sameas b and b is the sameas c, the query returns both b and c for a.
PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT * WHERE { ?x owl:sameas+ ?y }

The feature you are using is SPARQL 1.1 and so was not supported by earlier versions of ARQ. The only way you can write a query that gets close to what you do is to do one of the following.
Union paths of different lengths
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT *
WHERE
{
{ ?x owl:sameAs ?y }
UNION
{ ?s owl:sameAs [ owl:sameAs ?y ] . }
UNION
{ ?s owl:sameAs [ owl:sameAs [ owl:sameAs ?y ] ] . }
# Repeat the above pattern up to whatever limit you want
}
Use client side code
Issue an initial query as follows:
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT * WHERE { ?x owl:sameAs ?y }
Make a list of ?y values, then for each value issue a query of the form:
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT * WHERE { <constant> owl:sameAs ?y }
Where you substitute <constant> for one of the values from the list each time and then add the new values of ?y to the list.
Only thing you need to be careful of with this approach is that you keep track of values for which you've already issued the second query to save you repeating queries.

Related

A DBpedia SPARQL query misses small number of triples when asking for large number of relations

I'm running the following SPARQL query on DBpedia (in fact I'm running a similar CONSTRUCT query via rdflib, see blow in the edited section):
SELECT *
WHERE {
{ ?influencer dbo:influenced ?influencee .}
UNION
{ ?influencee dbo:influencedBy ?influencer .}
?influencer rdf:type dbo:Person .
?influencee rdf:type dbo:Person .
}
The above query almost works, except that some (a small number of) triples is missing.
E.g. the following relation is missing:
<http://dbpedia.org/resource/Plato> --> <http://dbpedia.org/resource/Aristotle>
Yet, we can see the above relation really should be included, e.g. by manually examining the Aristotle entry on DBpedia and looking at the dbo:influencedBy section.
What's wore, is that if I augment the above code with some FILTER() expressions to limit the amount of returned tuples, I do get this missing relation in return:
SELECT *
WHERE {
{?influencer dbo:influenced ?influencee .}
UNION
{?influencee dbo:influencedBy ?influencer .}
?influencer rdf:type dbo:Person .
?influencee rdf:type dbo:Person .
FILTER(regex(?influencer, "Plato"))
FILTER(regex(?influencee, "Aristo"))
}
Edit 2022-07-02: I'm aware of the 10k query result limit imposed by the DBpedia backend, yet I believe this limit is not interfering here (as hinted by TallTed below). This is because, in fact, I'm using the rdflib to run the query and -- in it -- I'm using the CONSTRUCT rather than the SELECT clause:
>>> import rdflib
>>> g = rdflib.Graph()
>>> query = """
... PREFIX schema: <http://schema.org/>
... PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
... PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
... PREFIX dbo: <http://dbpedia.org/ontology/>
... PREFIX dbp: <http://dbpedia.org/property/>
... PREFIX dbr: <http://dbpedia.org/resource/>
...
... CONSTRUCT {
... ?influencer dbo:influenced ?influencee .
... }
... WHERE {
... SERVICE <https://dbpedia.org/sparql/query> {
... {?influencer (dbo:influenced|dbo:influences) ?influencee .}
... UNION
... {?influencee dbo:influencedBy ?influencer .}
...
... ?influencer rdf:type dbo:Person .
... ?influencee rdf:type dbo:Person .
... }
... }"""
>>> qres = g.query(query)
>>> len(qres)
9464
And this query returns less than 10k...
EDIT: 2022-07-02, part2:
Interestingly, running the above code with the following selector:
SELECT ?influencer ?influencee
instead of the CONSTRUCT, returns indeed 10000 results, suggesting that I'm bouncing from the limit.
So the question really is about why my CONSTRUCT clause returns much less results than the SELECT clause?
Thanks!
A quick check of the COUNT(*) on your initial query shows that there are 19,527 solutions.
It is not surprising that you don't get them all when you run your query, as there's a 10,000 solution limit on that public instance -- and it's not "a small number of" triples that's missing!
To get all solutions, you'll need to add a few clauses -- i.e., ORDER BY ?influencer ?influencee , LIMIT 10000, and OFFSET 0 -- and then run the query to get the first 10,000 solutions. Then, change the OFFSET clause to OFFSET 10000 and run the query again, to get the remaining 9,527.
Of course, you'll probably also want to add a DISTINCT to your SELECT, as there's probably no point in repeated statements of influence.

Matching two words together in sparql

How to List the laureate awards (given by their label) for which the description of the contribution (given by nobel:motivation) contains the word "human" together with the word "peace" (i.e., both words must be there).
I have use the bds:search namespace from the the full-text search feature of Blazegraph.
After visiting this link i have composed this query
Free text search in sparql when you have multiword and scaping character
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bds: <http://www.bigdata.com/rdf/search#>
PREFIX nobel: <http://data.nobelprize.org/terms/>
SELECT ?awards ?description
WHERE {
?entity rdfs:label ?awards .
?entity nobel:motivation ?description .
FILTER ( bds:search ( ?description, '"human" AND "peace"' ) )
}
This query is returning me the following error on execution shown in image.
Error Image
How to correct this query and get the desired result?
You may take a look at the specification of this dataset or download an RDF dump of the dataset
Use bds:search to search for "human" category.Then apply filter and contain function to "peace".
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bds: <http://www.bigdata.com/rdf/search#>
PREFIX nobel: <http://data.nobelprize.org/terms/>
PREFIX bif: <http://www.openlinksw.com/schemas/bif#>
SELECT ?awards ?description
WHERE {
?entity rdfs:label ?awards .
?entity nobel:motivation ?description .
?description bds:search "human" .
FILTER (CONTAINS(?description, "peace"))
}

How to query dbpedia.org with sparql

I'm very new in OpenData and try to write a query in SPARQL.
My goal is to get data for following set of criteria:
- Category: Home_automation
- select all items from type "Thing"
- with at least one entry in "is Product of"
- that have a picture-url with a German description
I tried the following:
PREFIX cat: <http://dbpedia.org/resource/Category:>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT *
WHERE {
cat:Home_automation skos:broader ?x
}
But now I don't know how to add the other filters to the where clause.
I tried to user broader:... to get the items, but I think that was the wrong direction.
I tested the queries with: https://dbpedia.org/sparql
The result should be:
| (label) | (url)
|--------------------------|-----------------------------------
|"Kurzzeitwecker"#de | urls to the picture of the device
|"Staubsauger"#de | -||-
|"Waschmaschine"#de | -||-
|"Geschirrspülmaschine"#de | -||-
Does anyone have some tips please?
UPDATE: new query:
PREFIX cat: <http://dbpedia.org/resource/Category:>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?s ?label WHERE {
?s ?p cat:Home_automation .
?s rdf:type owl:Thing .
?s rdfs:label ?label
FILTER (LANG(?label)='de')
}
order by ?p
It is not clear what you want. You must first know what exact related information dbpedia contains and how they are structured.
However, you can try discovering what types of relationships cat:Home_automation is involved in, thus, you may know better what you want.
I suggest starting by generic queries, to specify how cat:Home_automation occurs in dbpedia, then, you might be able to go more specific, and pose further queries.
A query to list triples where cat:Home_automation is an subject:
PREFIX cat: <http://dbpedia.org/resource/Category:>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?p ?o WHERE {
cat:Home_automation ?p ?o
}
order by ?p
A query to list triples where cat:Home_automation is an object:
PREFIX cat: <http://dbpedia.org/resource/Category:>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?s ?p WHERE {
?s ?p cat:Home_automation
}
order by ?p
check the results, see what is interesting for you, and then continue with further queries.

Not able to get Indian cities abstract from Sparql

I am trying to get abstract using Sqarql with dbpedia datasets.
When I am running the following query on Virtuoso,
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?abstract WHERE {
[ rdfs:label ?name
; dbpedia-owl:abstract ?abstract
] .
FILTER langMatches(lang(?abstract),"en")
VALUES ?name { "London"#en }
}
LIMIT 10
I am getting the result, however if I modify the name to say 'Gokarna' which is a south indian tourist spot, I am not getting any data. However I do see the resource page online on dbpedia for Gokarna(http://dbpedia.org/page/Gokarna,_India). What am I doing wrong? I need to get similar data for close to 800 indian places.
When you use values, you'd get only those that exactly match your string. For Gokarna, that would work for #de, #it, #fr, but not for #en, as there the label is different, as you can see also from the previous answer.
I would suggest to use contains, instead of values:
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?abstract WHERE {
[ rdfs:label ?name
; dbpedia-owl:abstract ?abstract
] .
FILTER langMatches(lang(?abstract),"en")
FILTER langMatches(lang(?name),"en")
FILTER CONTAINS (?name, "Gokarna" )
}
LIMIT 10
I am not that experience with Sqarql but as much i can see in your code and checked with dbpedia library...
it is not just Gokarna. it is "Gokarna,_India".
This should work..
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?abstract WHERE {
[ rdfs:label ?name
; dbpedia-owl:abstract ?abstract
] .
FILTER langMatches(lang(?abstract),"en")
VALUES ?name { "Gokarna,_India"#en }
}
LIMIT 10
If you look through the DBpedia page for Gokarna, India that you linked to, you'll notice that its rdfs:label is "Gokarna, India". But its foaf:name is just "Gokarna". This would mean you should modify your query to:
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?abstract WHERE {
[ foaf:name ?name
; dbpedia-owl:abstract ?abstract
] .
FILTER langMatches(lang(?abstract),"en")
VALUES ?name { "Gokarna"#en }
}
LIMIT 10
Though this will return other Gokarnas too: Gokarna, Nepal, Gokarna, Bangladesh and Gokarna (film). If you want to remove these, you will have to figure out another filter (possibly dbo:country dbr:India).

Can't execute a specific SPARQL query on DBPedia :/

On this site, for example, take the first SPARQL query and make something very similar:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX p: <http://dbpedia.org/property/>
SELECT *
WHERE {
?name p:name <http://dbpedia.org/resource/Olivier_Theyskens> .
}
Try to execute it: here
And I get no results. However, modify the query to the following:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX p: <http://dbpedia.org/property/>
SELECT *
WHERE {
?name p:name ?otherthing.
}
And I get results, even though they're not the results I want.
Why doesn't the first query work -- what am I doing wrong? :/
In this case, I think it's because you're ordering your query statement backwards.
The DBpedia resource (<http://dbpedia.org/resource/Olivier_Theyskens>) is the Entity or Subject (?s), the property (p:name) is the Attribute or Predicate (?p), and the value of that property (?name) is the Value or Object (?o).
SPARQL expects all statements to be { ?s ?p ?o }, but yours seems to be written as { ?o ?p ?s }...
To sum up, if you try this query --
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX p: <http://dbpedia.org/property/>
SELECT *
WHERE
{
<http://dbpedia.org/resource/Olivier_Theyskens> p:name ?name .
}
-- you'll get the results I think you want.
The problem with your first query is that p:name links to Literal and you try to match a URI.
If you want your first query to work you have to to use the property http://dbpedia.org/ontology/artist that links to the URI and not the literal:
SELECT *
WHERE {
?s <http://dbpedia.org/ontology/artist> <http://dbpedia.org/resource/The_Velvet_Underground> .
}
Notice the different name space for the property <http://dbpedia.org/ontology/artist> this namespace contains ontology instead of property - ontology is the one used for object properties.