Hi James,
What is tripping you up here is that each property can occur in multiple representations in the SPARQL triplestore version of Wikidata, each one with a different prefix, for each different role the property can play.
So: * the wdt:... version of properties is used to connect an item with a simple value of the best current rank; * the p:... version of a property is used to connect an item with an entity representing a whole statement * the v:... version of a property is used to connect a statement to a simple value * the q:... version of a property is used to connect a statement to a qualifier value
etc.
Having this different forms makes it easier in searches to specify the kind of relationship one is trying to extract.
The 'master' form of the property lives in the wd:... namespace. There are connector predicates that connect this to each of its other forms; and it is this form that the label is attached to.
So we probably want to look for relations of the wdt:... form; but to get out a label for the property, we need to relate this form of the property back to the wd:... form.
That can be done with a query like the following: http://tinyurl.com/q3qboxv
PREFIX wikibase: http://wikiba.se/ontology# PREFIX wd: http://www.wikidata.org/entity/ PREFIX rdfs: http://www.w3.org/2000/01/rdf-schema#
SELECT ?prop ?prop_label ?q ?q_label WHERE { wd:Q170790 ?p ?q . ?prop wikibase:directClaim ?p . ?q rdfs:label ?q_label FILTER (LANG(?q_label) = "en") . ?prop rdfs:label ?prop_label filter (lang(?prop_label) = "en") . } LIMIT 100
Here the wikibase:directClaim connector forces ?prop to be in the wd:... namespace and ?p to be in the wdt:... namespace, giving the relationship between the two we need, so we can now look up the label for ?prop
Hope this helps,
James.
On 23/11/2015 21:40, james@j1w.xyz wrote:
Thanks for your help, James. I'm now attempting to use a SPARQL query, and am having trouble returning the property labels. Here's the query I'm using:
PREFIX entity: http://www.wikidata.org/entity/ PREFIX p: http://www.wikidata.org/prop/direct/ PREFIX rdfs: http://www.w3.org/2000/01/rdf-schema#
SELECT ?q ?q_label ?p ?p_label WHERE { entity:Q170790 ?p ?q . ?q rdfs:label ?q_label FILTER (LANG(?q_label) = "en") .
OPTIONAL {?p rdfs:label ?p_label filter (lang(?p_label) = "en") .} } LIMIT 100
It is returning the following:
{ "head" : { "vars" : [ "q", "q_label", "p", "p_label" ] }, "results" : { "bindings" : [ { "p" : { "type" : "uri", "value" : "http://www.wikidata.org/prop/direct/P279" }, "q" : { "type" : "uri", "value" : "http://www.wikidata.org/entity/Q901" }, "q_label" : { "xml:lang" : "en", "type" : "literal", "value" : "scientist" } }, { "p" : { "type" : "uri", "value" : "http://www.wikidata.org/prop/direct/P425" }, "q" : { "type" : "uri", "value" : "http://www.wikidata.org/entity/Q395" }, "q_label" : { "xml:lang" : "en", "type" : "literal", "value" : "mathematics" } }, { "p" : { "type" : "uri", "value" : "http://www.wikidata.org/prop/direct/P910" }, "q" : { "type" : "uri", "value" : "http://www.wikidata.org/entity/Q7217485" }, "q_label" : { "xml:lang" : "en", "type" : "literal", "value" : "Category:Mathematicians" } }, { "p" : { "type" : "uri", "value" : "http://www.wikidata.org/prop/direct/P31" }, "q" : { "type" : "uri", "value" : "http://www.wikidata.org/entity/Q28640" }, "q_label" : { "xml:lang" : "en", "type" : "literal", "value" : "profession" } } ] } }
My hope was to return property labels, but I obviously am missing something. I know that this isn't a SPARQL mailing list, but perhaps I'm overlooking something that could easily pointed out.
Thanks, James Weaver
On Sun, Nov 22, 2015, at 12:22 PM, James Heald wrote:
You could use the SPARQL API to extract any or all of the statements on an item in one go, plus the labels in any language you wanted, depending on what you put in the query.
The JSON won't be formatted exactly as below -- you'd get back a structure corresponding to a row for each statement, each row containing the various fields you've asked for -- but the information would be there.
One thing to watch out for in general if you're looking up property labels is that you might need to turn off the normal query optimiser, and instead hand-order the lines in your SPARQL query, because the optimiser often finds that irresistably tempting as a place to start. However, it may not be a problem if you're looking up the statements on a specific item, because there should usually be fewer of those.
But if you do want to get the information in one call, SPARQL may be as good a way as any to do it.
-- James.
On 22/11/2015 14:54, james@j1w.xyz wrote:
I would like to create a REST service in my application that has request/response behavior shown in the following two scenarios:
http://example/claims?id=Q170790&lang=en
{ “id”: “Q170790”, “label”: “mathematician”, “claims”: [ { “property”: { “id”: “P279”, “label”: “subclass of” }, “value”: [ { “id”: “Q816264”, “label”: “formal science” } ] }, { “property”: { “id”: “P425”, “label”: “field of this profession” }, “value”: [ { “id”: “Q395”, “label”: “mathematics” } ] } ] }
http://example/claims?id=Q170790&lang=fr
{ “id”: “Q170790”, “label”: “math\u00e9maticien”, “claims”: [ { “property”: { “id”: “P279”, “label”: “sous-classe de” }, “value”: [ { “id”: “Q816264”, “label”: “science formelle” } ] }, { “property”: { “id”: “P425”, “label”: “domaine d'occupation” }, “value”: [ { “id”: “Q395”, “label”: “math\u00e9matiques” } ] } ] }
I'm trying to identify the best Wikidata API queries to support this functionality, ideally only requiring one invocation. The closest one I've found is something like the following, but it seems that I'd have to make an additional query to get the label for each Q item returned: https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q170790&...
Is there a way to somehow include the desired information in one query?
Regards, James Weaver
Wikidata mailing list Wikidata@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikidata
Wikidata mailing list Wikidata@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikidata
Wikidata mailing list Wikidata@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikidata