sparql: how to avoid visited subject - sparql

and I am trying to write SPARQL query to find distinct object.
here is the dataset:
<https://permid.org/1-36436064275> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://permid.org/1-34414203048> .
<https://permid.org/1-34414203048> <http://permid.org/ontology/person/hasTenureInOrganization> <https://permid.org/1-36436064275> .
my sparql query is like this:
select distinct ?n where {
<https://permid.org/1-36436064275> ?a ?b .
?b ?c ?d .
?d ?e ?n .
}
From the dataset, "?d" is https://permid.org/1-36436064275, which is the visited subject. I want to skip visited subject, so that "?d" is empty, thus "?n" is also empty.

There's a couple of things to address here:
From the dataset, "?d" is https://permid.org/1-36436064275, which is the visited subject.
This happens because your RDF graph is a cycle. The first bit of your SPARQL query is:
<https://permid.org/1-36436064275> ?a ?b .
This binds ?a to rdf:type, and ?b to 1-34414203048. The second part of your query pattern is:
?b ?c ?d .
Since ?b is already bound to 1-34414203048 by the previous pattern, there's only one option for ?c (hasTenureInOrganization) and ?d (1-36436064275). Then the third bit of your query is:
?d ?e ?n .
?d is already bound by the previous pattern, so there's one option for ?e (rdf:type again) and ?n (1-36436064275 again).
I want to skip visited subject, so that "?d" is empty, thus "?n" is
also empty.
That's not how SPARQL works. SPARQL only returns a query result if the entire pattern has a result. If ?n has no value, the query will return an empty result.
Having said that, if you want to ensure that ?d is never equal to the subject that you started your query with, you could simply add a FILTER condition:
FILTER (?d != <https://permid.org/1-36436064275>)
But, like I said in the comments as well, I think you may need to rethink your data model a little bit, and consider the purpose of your query as well.

Related

DBpedia Sparql by page template

I am trying to run a query on dbpedia using SPARQL syntax, to look for all pages of a certain template. Doesn't seem to be work, I am looking for all pages with dbpprop:wikiPageUsesTemplate. Does anyone know how to correct this to properly look for templates?
SELECT ?name ?member_Of ?country ?lat ?lng ?link
WHERE {
?x dbpprop:wikiPageUsesTemplate "dbpedia:Template:Infobox_settlement" .
?x a <http://dbpedia.org/ontology/Settlement> .
?x foaf:name ?name .
?x dbpedia-owl:isPartOf ?member_Of.
?x dbpedia-owl:country ?country.
?x geo:lat ?lat .
?x geo:long ?lng .
?x foaf:isPrimaryTopicOf ?link .
}
LIMIT 2500 OFFSET 0
I've also attempted to run it just by the dbprop to no avail.
SELECT * WHERE { ?page dbpprop:wikiPageUsesTemplate <http://dbpedia.org/resource/Template:Infobox_settlement> . ?page dbpedia:name ?name .}
If anyone is trying to do a similar thing, it is also possible via the wiki api, where you can pagananate over all results. http://en.wikipedia.org/w/api.php?action=query&list=embeddedin&eititle=Template:Infobox_settlement
There are at least two problems: (i) you need to use IRIs in places, and not strings; and (ii) you need to use properties that DBpedia uses.
Use IRIs
In
?x a <http://dbpedia.org/ontology/Settlement> .
and
?x foaf:isPrimaryTopicOf ?link .
you've demonstrated that you know that URIs need to be written in full with < and >, or abbreviated with a prefix. However,
?x dbpprop:wikiPageUsesTemplate "dbpedia:Template:Infobox_settlement" .
certainly isn't going to work. It's legal SPARQL, because a string can be the object of a triple, but you almost certainly want an IRI.
Use DBpedia's vocabulary
A query with dbpprop:wikiPageUsesTemplate like this returns no results:
select distinct ?template where {
[] dbpprop:wikiPageUsesTemplate ?template
}
SPARQL results
That's easy enough to check, and quickly confirms that there's no data that can possibly match your query. Where did you find this property? Have you seen it used somewhere? I'm not confident that you can query DBpedia based on infobox templates. DBpedia is not the same as Wikipedia, and even if the Wikipedia API supports it, it doesn't mean that DBpedia has a counterpart. There is a note on DBpedia Data Set Properties that says:
http://xx.dbpedia.org/property/wikiPageUsesTemplate (will be changed to http://dbpedia.org/ontology/wikiPageUsesTemplate)
but that latter property doesn't seem to be in use on the endpoints either. See my answer to Syntax for Sparql query for pages with specific infobox for more details.

Extract Chemical Data from DBpedia via SPARQL

I'd like to know how to submit a SPARQL query to DDBpedia and be given back a table that includes the information found in the Wikipedia "chembox" info box template, such as molecular weight or formula.
So, the first step was just to make a query whose results should be a list of chemical substances that had the formula and molecularWeight properties included. But the following returns no results:
SELECT * WHERE {
?y rdf:type dbpedia-owl:ChemicalSubstance.
?y rdfs:label ?Name .
?y dbpedia:molecularWeight ?molecularWeight .
?y dbpedia:formula ?formula .
OPTIONAL {?y dbpedia-owl:iupacName ?iupacname} .
FILTER (langMatches(lang(?Name),"en"))
}
LIMIT 50
SPARQL Explorer at dbpedia.org
And so I'm stuck. Is something wrong with this query or does DBPedia really not collect that information from the Wikipedia chemboxes?
You caught the wrong namespace for both dbpedia:molecularWeight and dbpedia:formula. The correct namespace here would be dbpedia2.
Furthermore, there seem rarely any entries having a dbpedia-owl:iupacName, dbpedia2:molecularWeight and dbpedia2:formula.
SELECT * WHERE {
?y rdf:type dbpedia-owl:ChemicalSubstance.
?y rdfs:label ?Name .
OPTIONAL {?y dbpedia2:formula ?formula }.
OPTIONAL {?y dbpedia2:molecularWeight ?molecularWeight}.
OPTIONAL {?y dbpedia-owl:iupacName ?iupacname} .
FILTER (langMatches(lang(?Name),"en"))
}
LIMIT 50
SPARQL Explorer #dbpedia.org
To get the correct namespaces, you could either look at one example like this or get a list of all used properties for type dbpedia-owl:ChemicalSubstance using
SELECT DISTINCT ?rel WHERE {
?y rdf:type dbpedia-owl:ChemicalSubstance.
?y ?rel ?x
}
SPARQL Explorer #dbpedia.org

Retrieving properties of redirected resource

How can I retrieve all the properties of http://dbpedia.org/resource/Milano? I tried with this query but I have a few results and I don't understand the reason:
select ?prop ?c
where {<http://dbpedia.org/resource/Milano> ?prop ?c.}
SPARQL results
The question isn't entirely clear, but expect that the problem you're asking about is why you're getting triples about dbpedia:Milano, but not dbpedia:Milan. This query, as you can see in the results, only returns ten rows:
select ?prop ?c
where {
<http://dbpedia.org/resource/Milano> ?prop ?c.
}
SPARQL results
One of those rows, however, is
prop c
http://dbpedia.org/ontology/wikiPageRedirects http://dbpedia.org/resource/Milan
So, the simple answer is "query for Milan" with a query like this:
select ?prop ?c
where {
<http://dbpedia.org/resource/Milan> ?prop ?c. # you can use dbpedia:Milan, too
}
SPARQL results
A more sophisticated answer would return the triples for dbpedia:Milano and any triples of anything that it redirects to (and, I suppose, anything that any of those redirect to, and so on, though I think that Wikipedia limits redirects to be one level deep). You can do this with a property path query in SPARQL:
select ?prop ?c
where {
dbpedia:Milano dbpedia-owl:wikiPageRedirects* ?subject .
?subject ?prop ?c.
}
SPARQL results
In that query ?subject will be anything related by a path of length zero or more (so, given the data that we've seen, ?subject will be bound to at least dbpedia:Milano and dbpedia:Milan. If you want to preserve information about the subject of the various triples that you're using, you might want to add ?subject to the select line, so as to have select ?subject ?prop ?c.
If you don't care about the particular value of ?subject, then you actually don't need to bind ?subject at all, and could use a blank node in the query:
select ?prop ?c
where {
dbpedia:Milano dbpedia-owl:wikiPageRedirects* [ ?prop ?c ] .
}
SPARQL results
Caveat
Unfortunately, although this last query is legal SPARQL, Virtuoso says it's an error. Fortunately, this last refinement is entirely optional; it's not vital to the solution. If you were querying against a different endpoint, you'd be able to use it. The error that Virtuoso gives is:
Virtuoso 37000 Error SP031: SPARQL compiler: Object of transitive triple pattern should be variable or QName or literal, not blank node
SPARQL query:
define sql:big-data-const 0
#output-format:text/html
define sql:signal-void-variables 1 define input:default-graph-uri <http://dbpedia.org> select ?prop ?c
where {
dbpedia:Milano dbpedia-owl:wikiPageRedirects* [ ?prop ?c ] .
}
I contacted the Virtuoso mailing list and they confirmed that it's a Virtuoso bug, and that they'll fix it. I don't know how long it will take for the fix to get to the DBpedia endpoint, though.

dbpedia SPARQL query for finding artist properties

I'm trying to get details about an artist via DBPedia and the SPARQL query language, however, it seems almost impossible (with my understanding) of how to get certain pieces of information.
I'm trying to get an Artist and pull information such as their Hometown. I'm guessing the query should be something similar to that of:
SELECT ?c WHERE {
?b <http://dbpedia.org/property/Artist> <http://dbpedia.org/resource/Arctic_Monkeys>.
?b <http://www.w3.org/2002/07/owl#ObjectProperty> <http://dbpedia.org/ontology/hometown>.
?b rdfs:label ?c.
}
If anyone could enlighten me to how it should be done, that would be amazing.
I've been trying out the queries at:
http://dbpedia.org/sparql
If you want to find the label of their hometown, try this:
SELECT ?hometownLabel WHERE {
<http://dbpedia.org/resource/Arctic_Monkeys> <http://dbpedia.org/ontology/hometown> ?hometown .
?hometown <http://www.w3.org/2000/01/rdf-schema#label> ?hometownLabel .
}
Maybe you don't have a good understanding of SPARQL syntax. Unlike SQL, SPARQL search results by writing some triples with unknow variables in the WHERE clause.
you can try:
prefix dbpedia-owl:<http://dbpedia.org/ontology/>
SELECT ?c
WHERE {
<http://dbpedia.org/resource/Arctic_Monkeys> dbpedia-owl:hometown ?c.
}
With this search, you will get Arctic_Monkeys' hometown.
SELECT ?hometown
WHERE {
dbr:Arctic_Monkeys dbo:hometown ?label.
?label rdfs:label ?hometown.
FILTER(langMatches(lang(?hometown), "en"))
}

How to match exact string literals in SPARQL?

I have this query. It matches anything which has "South" in its name. But I only want the one whose foaf:name is exactly "South".
SELECT Distinct ?TypeLabel
WHERE
{
?a foaf:name "South" .
?a rdf:type ?Type .
?Type rdfs:label ?TypeLabel .
}
a bit late but anyway... I think this is what your looking for:
SELECT Distinct ?TypeLabel Where {
?a foaf:name ?name .
?a rdf:type ?Type .
?Type rdfs:label ?TypeLabel .
FILTER (?name="South"^^xsd:string)
}
you can use FILTER with the xsd types in order to restrict the result.
hope this helps...
cheers!
(Breaking out of comments for this)
Data issues
The issue is the data, not your query. If use the following query:
SELECT DISTINCT ?a
WHERE {
?a foaf:name "Imran Khan" .
}
You find (as you say) "Imran Khan Niazy".
But looking at the dbpedia entry for Imran Khan, you'll see both:
foaf:name "Imran Khan Niazy"
foaf:name "Imran Khan"
This is because RDF allows repeated use of properties.
Cause
"South" had the same issue (album, artist, and oddly 'South Luton'). These are cases where there are both familiar names ("Imran Khan", "South"), and more precise names ("Imran Khan Niazy", "South (album)") for the purposes of correctness or disambiguation.
Resolution
If you want a more precise match try adding a type (e.g. http://dbpedia.org/ontology/MusicalWork for the album).
Beware
Be aware that DBpedia derives from Wikipedia, and the extraction process isn't perfect. This is an area alive with wonky data, so don't assume your query has gone wrong.
That query should match exactly the literal South and not literals merely containing South as a substring. For partial matches you'd go to FILTER with e.g. REGEX(). Your query engine is broken in this sense - which query engine you are working with?