Yeah, I found Bob DuCharme's book quite helpful.
For an introduction to SPARQL in a specifically Wikidata context, you might find the following useful, which tries to set out the pieces in a vaguely logical order:
https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries
(or at least I hope so!)
-- James.
On 23/11/2015 22:03, james@j1w.xyz wrote:
As a post-script, the following query that I cobbled together from examples accomplishes the objective stated below. However, I don't yet understand SPARQL well enough to know why it works. Sorry for the noise, and I'll not post any more SPARQL questions until I've thoroughly read Bob DuCharme's "Learning SPARQL" book.
PREFIX wikibase: http://wikiba.se/ontology# 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 ?l WHERE { entity:Q170790 ?p ?q . ?q rdfs:label ?q_label FILTER (LANG(?q_label) = "en") . ?property ?ref ?p . ?property a wikibase:Property . ?property rdfs:label ?l FILTER (lang(?l) = "en") } LIMIT 100
Thanks, James Weaver
On Mon, Nov 23, 2015, at 04:40 PM, 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
Wikidata mailing list Wikidata@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikidata