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

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.

Related

Dbpedia sparql gives empty result to my query, what is wrong?

I'm doing sparql query in this site.
It gives me an empty result, what is wrong with my query?
prefix foaf: <http://xmlns.com/foaf/0.1/>
select * where {
?s rdf:type foaf:Person.
} LIMIT 100
This query is ok, but when I add the second pattern, I got empty result.
?s foaf:name 'Abraham_Robinson'.
prefix foaf: <http://xmlns.com/foaf/0.1/>
select * where {
?s rdf:type foaf:Person.
?s foaf:name 'Abraham_Robinson'.
} LIMIT 100
How to correct my query so the result includes this record:
http://dbpedia.org/resource/Abraham_Robinson
I guess Kingsley misread this as a freetext search, rather than a simple string comparison.
The query Kingsley posted and linked earlier delivers no solution, for the same reasons as the original query failed, as identified in the comment by AKSW, i.e. --
foaf:name values don't generally replaces spaces with underscores as in the original value; i.e., 'Abraham_Robinson' should have been 'Abraham Robinson'
foaf:name strings are typically langtagged, and it is in this case, so that actually needs to be 'Abraham Robinson'#en
Incorporating AKSW's fixes with this line ?s foaf:name 'Abraham Robinson'#en., the query works.
All that said -- you may prefer an alternative query, which will deliver results whether or not the foaf:name value is langtagged and whether or not the spaces are replaced by underscores. This one is Virtuoso-specific, and produces results faster because the bif:contains function uses its free-text indexes, would be --
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT *
WHERE
{
?s rdf:type foaf:Person ;
foaf:name ?name .
?name bif:contains "'Abraham Robinson'" .
}
LIMIT 100
Generic SPARQL using a REGEX FILTER works against both Virtuoso and other RDF stores, but produces results more slowly because REGEX does not leverage the free-text indexes, as in --
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT *
WHERE
{
?s rdf:type foaf:Person ;
foaf:name ?name .
FILTER ( REGEX ( ?name, 'Abraham Robinson' ) ) .
}
LIMIT 100
The following query should work. Right now it isn't working due to a need to refresh the text index associated with this instance (currently in progress):
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT * where {
?s rdf:type foaf:Person.
?s foaf:name "'Abraham Robinson'".
}
LIMIT 100
Note how the phrase is placed within single-quotes that are within double-quotes.
If the literal content is language-tagged, as is the case in DBpedia the exact-match query would take the form (already clarified in #TallTed's response):
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT * where {
?s rdf:type foaf:Person.
?s foaf:name 'Abraham Robinson'#en.
}
LIMIT 100
This SPARQL Results Page Link should produce a solution when the index update completes.

Openlink Virtuoso SPARQL OFFSET and LIMIT behavior

The following SPARQL query returns 20 results. I was expecting 10 given the OFFSET and LIMIT
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX dbpedia:<http://dbpedia.org/resource/>
PREFIX dbpedia-owl:<http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT ?person_id ?person2_id
WHERE {
{
SELECT DISTINCT ?person_id ?person2_id WHERE {
?person rdf:type dbpedia-owl:Person .
?person2 rdf:type dbpedia-owl:Person .
?person ?link ?person2 .
?person dbpedia-owl:wikiPageID ?person_id .
?person2 dbpedia-owl:wikiPageID ?person2_id .
FILTER (?link = dbpedia-owl:wikiPageWikiLink) .
} ORDER BY ?link
}
} OFFSET 10 LIMIT 10
I execute the code in the SPARQL endpoint of an OpenLink Virtuoso Server.
What is the problem with the query?
The clause that makes the query behave weird is ORDER BY ?link. Replacing it with ORDER BY ?person_id all works as expected. It makes still no sense to me but I am a newbie using SPARQL too.
#jordipala said
The clause that makes the query behave weird is ORDER BY ?link. Replacing it with ORDER BY ?person_id all works as expected. It makes still no sense to me but I am a newbie using SPARQL too.
Part of the issue is that the ?link values aren't string literals, though they may appear to be if you include that variable in the SELECT clauses. (Also note that the ?link values are all the same for these solutions, so you definitely need to put something else into the ORDER BY such that it does the desired job of preventing both duplicate solutions and omitted solutions.)
Also, counterintuitively given the ?link datatype, the numeric-appearing person_id and person2_id are not typed as numbers -- they're strings, unless you force their datatype.
If you simply change ?link to str(?link) in the ORDER BY, the query will deliver the desired 10 rows -- and you may note that all the ?link values are identical! -- and if you include person_id and person2_id in the ORDER BY (done in my following links by using the ordinals of the SELECT variables, because of where and how I've coerced the datatypes), you'll get more useful output, as in this query and these results

SPARQL Querying Transitive different versions of arq

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.

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.

SPARQL queries for Pizza ontology

i have to use ROWLKit
http://www.dis.uniroma1.it/quonto/?q=node/30
(1) can anybody suggest two sparql queries for the Pizza.owl ?
(2) is this query valid ?
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT *
WHERE { ?p rdf:type pizza:Pizza;
pizza:hasTopping ?t.
?t rdf:type pizza:TomatoTopping }
(3) if it is a valid query then: is the response an empty result?
SELECT DISTINCT *
WHERE {
?NombrePizza ?Relacion pizza:MushroomTopping .
?Relacion owl:inverseOf pizza:isToppingOf .
OPTIONAL {
?NombrePizza2 ?Relacion2 pizza:HamTopping .
?Relacion2 owl:inverseOf pizza:isToppingOf .
}
FILTER(?NombrePizza2 = ?NombrePizza)
}
(1) can anybody suggest two sparql queries for the Pizza.owl ?
Here are two examples:
SELECT * WHERE { ?s ?p ?o }
and:
SELECT ?class WHERE { ?class a owl:Class }
(2) is this query valid ?
Yes.
(3) if it is a valid query then: is the response an empty result?
I assume that you mean "if I query the RDF document that serialises the pizza ontology, is the response an empty result?". The answer is yes.
(2) appears to be a valid query
I don't understand part (3) of your question. (2) cannot be compared to a boolean since it returns a Result Set, if you want a boolean result then you need to use an ASK query. If an ASK query returns true then it means that there are solutions to the query in the data you are querying so it would not be an empty result.