Help
Difference between revisions of "SPARQL"
Line 6: | Line 6: | ||
* [[DataViz:Records]] | * [[DataViz:Records]] | ||
− | == Fetch data | + | == Fetch SPARQL data == |
− | Data can be queried via various languages such as Javascript | + | Data can be queried via various languages such as Python, Javascript, R and others. On the [https://query.wikidata.org/ Wikidata Query Service page], after running your SPARQL query, click "Code" : a pop up window appears with various call implementations. |
− | On the [Wikidata Query Service page] | ||
'''Javascript:'''<br> | '''Javascript:'''<br> | ||
Line 20: | Line 19: | ||
); | ); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
== ✅ Is Sex or Gender([[Q7]]) → list all possible values == | == ✅ Is Sex or Gender([[Q7]]) → list all possible values == | ||
{| | {| |
Revision as of 12:24, 6 December 2021
Base
- Special:ListProperties
- List of properties with possible values. Ex: Gender (P8) : male (male (Q16)), female (female (Q17)), intersex (intersex (Q18)).
- LinguaLibre:List of languages
- DataViz:Speakers
- DataViz:Records
Fetch SPARQL data
Data can be queried via various languages such as Python, Javascript, R and others. On the Wikidata Query Service page, after running your SPARQL query, click "Code" : a pop up window appears with various call implementations.
Javascript:
At least 3 methods exists (code snippet).
var endpoint = 'https://lingualibre.org/sparql';
var sparql = 'SELECT ?item WHERE { ?item wdt:P2 wd:Q5 } LIMIT 10';
$.getJSON(endpoint,
{ query: sparql, format: 'json' },
function(data){ console.log('JQuery: ',data)}
);
✅ Is Sex or Gender(sex or gender (Q7)) → list all possible values
SELECT ?item ?itemLabel
WHERE {
# P2: instance of; P9: subclass of; Q7: gender or sex.
?item prop:P2 entity:Q7
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
}
}
|
|
✅ Is Speaker (speaker (Q3)) → list all speakers
select ?speaker ?speakerLabel
where {
#P2: instance of; Q3: speaker.
?speaker prop:P2 entity:Q3 .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
}
}
|
|
✅ Speaker name → Speaker Qid
select ?speakerName ?speakerId
where {
VALUES ?speakerName { "Yug" "VIGNERON" } # One or multiple values
BIND ( STRLANG(?speakerName, "en") AS ?speakerLabel )
# P2: instance of; Q3: speaker.
?speakerId prop:P2 entity:Q3 ; rdfs:label ?speakerLabel .
}
|
|
Speaker Qid (0x010C (Q42)) → Speaker data
SELECT ?predicate ?object
WHERE {
entity:Q42 ?predicate ?object .
}
|
|
Speaker Qid → Speaker data → speaker languages
SELECT ?languages
WHERE {
entity:Q42 prop:P4 ?languages .
}
|
|
Speaker Qid + language → list of all associated audios
SELECT ?audio WHERE {
?audio prop:P5 entity:Q42 .
?audio prop:P4 entity:Q21 .
}
|
|
Is Language (speaker (Q3)) → list all languages with number of unique words and speakers
SELECT ?language (COUNT(?audio) AS ?nbAudio) (COUNT(?speaker) AS ?nbSpeaker) WHERE {
?language prop:P2 entity:Q4 .
?audio prop:P4 ?language .
?speaker prop:P4 ?language .
}
GROUP BY ?language
Too large to work :(
Isolang → Language LL Qid
SELECT * WHERE {
?lang prop:P13 ?code .
}
|
|
Isolang → Language WD Qid
SELECT * WHERE {
?lang prop:P12 ?idWikidata .
}
|
|
Language WD Qid → Language data
SELECT * WHERE {
?lang prop:P12 "Q12107" .
?lang ?predicate ?object .
}
|
|
Language LL Qid → Language data
SELECT * WHERE {
entity:Q209 ?predicate ?object .
}
|
|
Langue + speaker + word → Audio's Qid
SELECT ?audio WHERE {
?audio prop:P4 entity:Q209 . #language
?audio prop:P5 entity:Q584098 . #speaker
?audio rdfs:label ?word . #word
FILTER ( STR(?word) = "ni" )
}
|
|
Audio Qid → Audio data
✅ Langue + speaker + word → Audio's Commons url
Tools
- Special:ApiSandbox – API queries generator for Lingualibre wikipage and wikibase contents.