Why the results are different in Virtuoso SPARQL Query Editor and SPARQLWrapper? - sparql

My query in Virtuoso SPARQL Query Editor is as follows which resuted it 74
SELECT (COUNT (*) AS ?count) WHERE {?s ?p <http://dbpedia.org/resource/Machine_learning> .}
I used the same query in SPARQLWrapper as follows which gave me the count as 1195.
from SPARQLWrapper import SPARQLWrapper, JSON
sparqlw = SPARQLWrapper("http://dbpedia.org/sparql")
dbpedia_uri = "http://dbpedia.org/resource/Machine_learning"
sparqlw.setQuery(f"SELECT (COUNT (*) AS ?count) WHERE {{?s ?p <{dbpedia_uri}> .}}")
sparqlw.setReturnFormat(JSON)
results = sparqlw.query().convert()
results_df = pd.io.json.json_normalize(results['results']['bindings'])
print(results_df)
I re-checked the following details.
both are using http://dbpedia.org/ version
my f-string is equivalent to the query
Therefore, I am very confused why this big change of the results happen.
I am happy to provide more details if needed.

Related

Sparql filter on prefix does not work on dbpedia

I'm new to SPARQL hope someone could help me.
The problem is that if I run the following query on dbpedia sparql:
SELECT DISTINCT ?class WHERE {
?s a ?class.
}
it returns:
result query
I would like to remove the results that has this two prefix: "http://www.w3.org/2002/07/owl" and "http://www.w3.org/2000/01/rdf-schema", now the query is:
SELECT DISTINCT ?class WHERE {
?s a ?class.
FILTER ( !strstarts(str(?class), "http://www.w3.org/2002/07/owl") ).
FILTER ( !strstarts(str(?class), "http://www.w3.org/2000/01/rdf-schema") ).}
but it returns only one result:
http://www.w3.org/1999/02/22-rdf-syntax-ns#Property
If I execute the same query on istat sparql these queries work fine.
So the question is, why on dbpedia my queries does not work as expected?
Thanks

How to filter the simple Subject in a SPARQL Query

I guess I am stuck at the basics with SPARQL. Can someone help ?
I simply wnat to filter all subjects containing "Mountain" of an RDS database.
Prefix lgdr:<http://linkedgeodata.org/triplify/> Prefix lgdo:<http://linkedgeodata.org/ontology/>
Select * where {
?s ?p ?o .
filter (contains(?s, "Mountain"))
} Limit 1000
The query leads to an error:
Virtuoso 22023 Error SL001: The SPARQL 1.1 function CONTAINS() needs a string value as first argument
You can get it to "work" using:
Prefix lgdr:<http://linkedgeodata.org/triplify/> Prefix lgdo:<http://linkedgeodata.org/ontology/>
Select * where {
?s ?p ?o .
filter (contains(str(?s), "Mountain"))
} Limit 1000
Note the additional str in the query.
However, that results in
Virtuoso S1T00 Error SR171: Transaction timed out
and I am not sure how to deal with that.
But in principle in works: When you use
Limit 1
you get
s p o
http://linkedgeodata.org/ontology/MountainRescue http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2002/07/owl#Class

Sparql- how to get number of triples?

I'm doing a small exercise on sparql. Using Dbpedia Endpoint, I need to count number of triples.
This is my query
// Get the number of triples //
SELECT (COUNT(*) as ?Triples) WHERE { ?s ?p ?o}
-------------------------------------------------------
OUTPUT:
( ?Triples = 1625382483 )
Just wondering are the query and the result right?
Is this how you get the number of triples?
You can sanity check many things by executing queries directly on a SPARQL endpoint, rather than going through Jena or other intermediary clients. For instance, your query on the DBpedia form, and its results, which shows all triples in that triplestore (currently 1,625,382,483).
If you want the count of triples only within the DBpedia named graph (currently 438,336,517), you'll need to specify that, either in the SPARQL form Default Data Set Name (Graph IRI), or directly in the query, as in --
SELECT (COUNT(*) as ?Triples)
WHERE
{ GRAPH <http://dbpedia.org>
{ ?s ?p ?o }
}
-- or --
SELECT (COUNT(*) as ?Triples)
FROM <http://dbpedia.org>
WHERE { ?s ?p ?o }

SPARQL Query to Identify Predicates from One of Many Graphs [duplicate]

This question already has an answer here:
SPARQL - Restricting Result Resource to Certain Namespace(s)
(1 answer)
Closed 4 years ago.
I have created an ontology by grouping together many ontologies.
I want to use SPARQL to identify all middle terms (relationships) from one ontology from the group ontology.
The following approach only produces a pyparsing error.
g = rdflib.Graph()
result = g.parse("without-bfo.owl")
qres = g.query(
""" PREFIX sudo: <http://purl.url/sudo/ontology#>
SELECT ?v
WHERE {
?s sudo:?v ?o.
}""")
If I remove the sudo: prefix, this query returns all triples.
You can check if the relation starts with your namespace with CONTAINS
SELECT ?v
WHERE {
?s ?v ?o.
FILTER CONTAINS(?v, "http://purl.url/sudo/ontology#")
}
You can also try STRSTARTS
see w3 documentation
#Arcturus was close.
The following worked for me. One has to declare ?v as a string using STR. This SO post suggested the syntax.
qres = g.query(
""" SELECT DISTINCT ?v
WHERE {
?s ?v ?o .
FILTER CONTAINS(STR(?v), "sudo")
}""")
for row in qres:
print row

SPARQL Query problem -> wrong answer

I want to select a triple using SPARQL. To do it, i'm using following query:
SELECT count (*)
WHERE {?s ?p ?o}
FILTER (?s=http://kjkhlsa.net && ?p=http://lkasdjlkjas.com && ?o=Test)
As answer i get fully wrong triple :( subject ist not equal to "http://kjkhlsa.net", predicate is not equal to "http://lkasdjlkjas.com" and object ist also not equal to "Test". Can someone explain me, what I'm doing wrong :(
edit1:
I have put the query into php file:
$inst_query = 'SELECT * { <http://kjkhlsa.net> <http://lkasdjlkjas.com> "Test"}';
echo $inst_query;
The answer from the echo was "SELECT * { "Test"}". Then i tried it with WHERE:
$inst_query = 'SELECT * WHERE { <http://kjkhlsa.net> <http://lkasdjlkjas.com> "Test"}';
echo $inst_query;
Here was the answer "SELECT * WHERE { "Test"}"...so, i'm missing the URIs, but this seems for me as php issue and not sparql problem.
edit2:
I've put the query into SPARQL Query editor and i get the response "no result"....but I'm sure, that i have this triple.
In its current form the question is not very clear (see my comment above).
Since you are essentially trying to get triples matching a pattern, it is more efficient to use a graph pattern instead of FILTER. Many SPARQL implementations first match candidate triples by graph patterns and only then apply the FILTER expression. In essence, with a ?s ?p ?o graph pattern, you're doing a linear scan over all your triples.
So, here's something that should work, using graph patterns instead of FILTER.
SELECT * { <http://kjkhlsa.net> <http://lkasdjlkjas.com> "Test" }
Notes: I didn't include COUNT(*) which is not standard SPARQL. <> around URIs. "" around literal.
Try to use this :
SELECT count (*) as ?count
WHERE {
?s ?p ?o
FILTER (?s=<http://kjkhlsa.net> && ?p=<http://lkasdjlkjas.com> && ?o=Test)
}
The following query uses the count function to count the number of distinct URI(s) returned to the ?s variable.
SELECT ?s (COUNT (DISTINCT ?s) as ?count)
WHERE {?s ?p ?o}
FILTER (?o="Test")