0

I'm trying to get external identifiers for an entity in Wikidata. Using the following query, I can get the literal values (_value) and optionally formatted URLs (value) for Q2409 on the Wikidata Query Service site.

Load in Wikidata Query Service

SELECT ?property ?_value ?value
WHERE {
  ?property wikibase:propertyType wikibase:ExternalId .
  ?property wikibase:directClaim ?propertyclaim .
  OPTIONAL { ?property wdt:P1630 ?formatterURL . }
  wd:Q2409 ?propertyclaim ?_value .
  BIND(IF(BOUND(?formatterURL), IRI(REPLACE(?formatterURL, "\\$", ?_value)) , ?_value) AS ?value)
}

Using RDFLib, I'm writing the same query, but with a federated service.

from rdflib import Graph
from rdflib.plugins.sparql import prepareQuery

g = Graph()
q = prepareQuery(r"""
  PREFIX wd: <http://www.wikidata.org/entity/>
  PREFIX wdt: <http://www.wikidata.org/prop/direct/>
  PREFIX wikibase: <http://wikiba.se/ontology#>

  SELECT ?property ?_value ?value
  WHERE {
    SERVICE <https://query.wikidata.org/sparql> {
      ?property wikibase:propertyType wikibase:ExternalId .
      ?property wikibase:directClaim ?propertyclaim .
      OPTIONAL { ?property wdt:P1630 ?formatterURL . }
      wd:Q2409 ?propertyclaim ?_value .
      BIND(IF(BOUND(?formatterURL), IRI(REPLACE(?formatterURL, "\\$", ?_value)) , ?_value) AS ?value)
    }
  }
""")

for row in g.query(q, DEBUG=True):
  print(row)

With this, I'm getting the URLs as URIRef objects. But, instead of Literal for the literal values, I'm getting None.

First 6 lines of output:

(rdflib.term.URIRef('http://www.wikidata.org/entity/P232'), None, None)
(rdflib.term.URIRef('http://www.wikidata.org/entity/P657'), None, None)
(rdflib.term.URIRef('http://www.wikidata.org/entity/P6366'), None, None)
(rdflib.term.URIRef('http://www.wikidata.org/entity/P1296'), None, rdflib.term.URIRef('https://www.enciclopedia.cat/EC-GEC-01407541.xml'))
(rdflib.term.URIRef('http://www.wikidata.org/entity/P486'), None, rdflib.term.URIRef('https://id.nlm.nih.gov/mesh/D0068511.html'))
(rdflib.term.URIRef('http://www.wikidata.org/entity/P7033'), None, rdflib.term.URIRef('http://vocabulary.curriculum.edu.au/scot/5001.html'))

What am I missing for the literal values? I'm having trouble figuring out why I'm getting None instead of the values.

logi-kal
  • 7,107
  • 6
  • 31
  • 43
King Chung Huang
  • 5,026
  • 28
  • 24

1 Answers1

0

I'm not sure if all of the features of SERVICE calls are fully implemented in RDFLib.

I would get this working with a 'normal' call the Wikidata SPARQL endpoint using either RDFLib's SPARQLWrapper library or the general-purpose web request Python libraries requests or httpx first. If that all works, you could then try again with the SERVICE request but you likely won't need it.

Nicholas Car
  • 1,164
  • 4
  • 7