0

I have following problem which I explain on an example:

I want to retrieve the object Berlin from the triplet Germany - capital - object.

I must use labels, because those are inputs in my program.

Following query gives me back the propertyLabel capital:

prefix wdt: <http://www.wikidata.org/prop/direct/>
prefix wikibase: <http://wikiba.se/ontology#>
prefix bd: <http://www.bigdata.com/rdf#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT DISTINCT ?propertyLabel WHERE {
    ?property a wikibase:Property .
    ?property rdfs:label "capital"@en
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .} 
}

Following query with the label Germany and URI P36 (capital) gives me back the desired information Berlin :

prefix wdt: <http://www.wikidata.org/prop/direct/>
prefix wikibase: <http://wikiba.se/ontology#>
prefix bd: <http://www.bigdata.com/rdf#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT DISTINCT ?objectLabel WHERE {
    ?subject wdt:P36 ?object . 
    ?subject rdfs:label "Germany"@en .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .} 
}

But I want to use P36 as a label. I tried various ways with two Selects or a Union, but i get thousands of results or none. The query should look like this (although this one doesn't work):

prefix wdt: <http://www.wikidata.org/prop/direct/>
prefix wikibase: <http://wikiba.se/ontology#>
prefix bd: <http://www.bigdata.com/rdf#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT DISTINCT ?objectLabel WHERE {
    ?subject ?property ?object . 
    ?subject rdfs:label "Germany"@en .
    ?property a wikibase:Property .
    ?property rdfs:label "capital"@en
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .} 
}

The query as already mentioned above has to return Berlin and nothing else. Thanks in advance.

grajkowski
  • 349
  • 1
  • 3
  • 14

1 Answers1

1

the problem is that your property lookup for the label "capital" returns http://www.wikidata.org/entity/P36 but the instance data uses http://www.wikidata.org/prop/direct/P36. A workaround might be:

PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT DISTINCT ?objectLabel WHERE {
  ?subject ?property ?object ;
           rdfs:label "Germany"@en .
  ?p a wikibase:Property ;
     rdfs:label "capital"@en
  BIND(STRAFTER(STR(?p), STR(wd:)) as ?p_localname)
  BIND(IRI(CONCAT(STR(wdt:), ?p_localname)) as ?property)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .} 
}
UninformedUser
  • 8,397
  • 1
  • 14
  • 23
  • Sneaky workaround, but as long as it works... I didn't know you can easily manipulate the string. Thank you for the fast answer. Edit: The syntax rules for SPARQL are stricter in Apache Jena, so you need to place BIND statements further up in the query. The error message states that you can no longer first bind a variable through a basic graph pattern and then use it in a BIND. – grajkowski May 18 '17 at 11:45