Help
Difference between revisions of "SPARQL"
Line 1: | Line 1: | ||
== Base == | == Base == | ||
* [[Special:ListProperties]] | * [[Special:ListProperties]] | ||
− | |||
* [[LinguaLibre:List of languages]] | * [[LinguaLibre:List of languages]] | ||
* [[DataViz:Speakers]] | * [[DataViz:Speakers]] | ||
Line 7: | Line 6: | ||
== Fetch SPARQL data == | == Fetch SPARQL data == | ||
− | Data can be | + | Data can be fetched using various coding 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 implementations. |
'''Javascript:'''<br> | '''Javascript:'''<br> | ||
− | At least 3 methods exists ([https://jsfiddle.net/hugolpz/tsg9ewa7/4/ code snippet]) | + | At least 3 methods exists ([https://jsfiddle.net/hugolpz/tsg9ewa7/4/ code snippet]), example: |
+ | {| | ||
+ | |- | ||
+ | ! Query || Result | ||
+ | |- style="vertical-align:top;" | ||
+ | |style="padding: 0 3em;width:60%"| | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
var endpoint = 'https://lingualibre.org/sparql'; | var endpoint = 'https://lingualibre.org/sparql'; | ||
− | var sparql = 'SELECT ?item WHERE { ?item | + | var sparql = 'SELECT ?item WHERE { ?item prop:P2 entity:Q5 } LIMIT 10'; |
$.getJSON(endpoint, | $.getJSON(endpoint, | ||
{ query: sparql, format: 'json' }, | { query: sparql, format: 'json' }, | ||
Line 19: | Line 23: | ||
); | ); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | | | ||
+ | <syntaxhighlight lang="javascript"> | ||
+ | { | ||
+ | "item" : { | ||
+ | "type" : "uri", | ||
+ | "value" : "https://lingualibre.org/entity/Q12" | ||
+ | }, | ||
+ | "itemLabel" : { | ||
+ | "xml:lang" : "en", | ||
+ | "type" : "literal", | ||
+ | "value" : "beginner" | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |} | ||
== ✅ Is Sex or Gender([[Q7]]) → list all possible values == | == ✅ Is Sex or Gender([[Q7]]) → list all possible values == | ||
Line 27: | Line 46: | ||
SELECT ?item ?itemLabel | SELECT ?item ?itemLabel | ||
WHERE { | WHERE { | ||
− | + | ?item prop:P2 entity:Q5 # Condition 1, P2 'instance of' is Q5 'language level'. | |
− | ?item prop:P2 entity: | ||
SERVICE wikibase:label { | SERVICE wikibase:label { | ||
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . | bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . | ||
Line 38: | Line 56: | ||
SELECT ?item ?itemLabel | SELECT ?item ?itemLabel | ||
WHERE { | WHERE { | ||
− | |||
?item prop:P2 entity:Q7 | ?item prop:P2 entity:Q7 | ||
SERVICE wikibase:label { | SERVICE wikibase:label { | ||
Line 52: | Line 69: | ||
|style="padding: 0 3em;width:60%"| | |style="padding: 0 3em;width:60%"| | ||
<syntaxhighlight lang="sparql"> | <syntaxhighlight lang="sparql"> | ||
− | + | SELECT ?speaker ?speakerLabel | |
− | + | WHERE { | |
− | + | ?speaker prop:P2 entity:Q3 . # Condition 1, P2 'instance of' is Q3 'speaker'. | |
− | ?speaker prop:P2 entity:Q3 . | ||
SERVICE wikibase:label { | SERVICE wikibase:label { | ||
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . | bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . | ||
Line 63: | Line 79: | ||
|| | || | ||
<query _pagination="5" speaker="Item" speakerLabel="Speaker" langLabel="Language"> | <query _pagination="5" speaker="Item" speakerLabel="Speaker" langLabel="Language"> | ||
− | + | SELECT ?speaker ?speakerLabel | |
− | + | WHERE { | |
− | |||
?speaker prop:P2 entity:Q3 . | ?speaker prop:P2 entity:Q3 . | ||
SERVICE wikibase:label { | SERVICE wikibase:label { | ||
Line 74: | Line 89: | ||
|} | |} | ||
− | == ✅ Speaker name → Speaker Qid == | + | == ✅ Speaker name(s) → Speaker Qid(s) == |
{| | {| | ||
Line 80: | Line 95: | ||
|style="padding: 0 3em;width:60%"| | |style="padding: 0 3em;width:60%"| | ||
<syntaxhighlight lang="sparql"> | <syntaxhighlight lang="sparql"> | ||
− | + | SELECT ?speakerName ?speakerId | |
− | + | WHERE { | |
VALUES ?speakerName { "Yug" "VIGNERON" } # One or multiple values | VALUES ?speakerName { "Yug" "VIGNERON" } # One or multiple values | ||
BIND ( STRLANG(?speakerName, "en") AS ?speakerLabel ) | BIND ( STRLANG(?speakerName, "en") AS ?speakerLabel ) | ||
Line 91: | Line 106: | ||
<query _pagination="5"> | <query _pagination="5"> | ||
− | + | SELECT ?speakerName ?speakerId | |
− | + | WHERE { | |
VALUES ?speakerName { "Yug" "VIGNERON" } # One or multiple values | VALUES ?speakerName { "Yug" "VIGNERON" } # One or multiple values | ||
BIND ( STRLANG(?speakerName, "en") AS ?speakerLabel ) | BIND ( STRLANG(?speakerName, "en") AS ?speakerLabel ) | ||
Line 101: | Line 116: | ||
|} | |} | ||
− | == Speaker Qid | + | == ✅🇶 Speaker Qid: [[Q42]] → Speaker data == |
+ | <!-- Q: alternative words for "predicate" and "object". "property" and "value" ?--> | ||
{| | {| | ||
|- style="vertical-align:top;" | |- style="vertical-align:top;" | ||
|style="padding: 0 3em;width:60%"| | |style="padding: 0 3em;width:60%"| | ||
<syntaxhighlight lang="sparql"> | <syntaxhighlight lang="sparql"> | ||
− | SELECT ?predicate ?object | + | # Get Q42 (User:0x010C)'s data |
+ | SELECT ?predicate ?predicate ?object ?objectLabel | ||
WHERE { | WHERE { | ||
entity:Q42 ?predicate ?object . | entity:Q42 ?predicate ?object . | ||
+ | SERVICE wikibase:label { | ||
+ | bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . | ||
+ | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|| | || | ||
<query _pagination="10" > | <query _pagination="10" > | ||
− | SELECT ?predicate ?object | + | SELECT ?predicate ?object ?objectLabel |
WHERE { | WHERE { | ||
entity:Q42 ?predicate ?object . | entity:Q42 ?predicate ?object . | ||
+ | SERVICE wikibase:label { | ||
+ | bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . | ||
+ | } | ||
} | } | ||
</query> | </query> | ||
|} | |} | ||
− | == Speaker Qid → Speaker data → | + | == ✅🇶 Speaker Qid: [[Q42]] → Speaker data → Speaker languages [[Property:P4|P4]] == |
− | + | <!-- Q: Add languages iso P:13 --> | |
{| | {| | ||
|- style="vertical-align:top;" | |- style="vertical-align:top;" | ||
|style="padding: 0 3em;width:60%"| | |style="padding: 0 3em;width:60%"| | ||
<syntaxhighlight lang="sparql"> | <syntaxhighlight lang="sparql"> | ||
− | SELECT ?languages | + | SELECT ?languages ?languagesLabel |
WHERE { | WHERE { | ||
entity:Q42 prop:P4 ?languages . | entity:Q42 prop:P4 ?languages . | ||
+ | SERVICE wikibase:label { | ||
+ | bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . | ||
+ | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|| | || | ||
<query _pagination="5" > | <query _pagination="5" > | ||
− | SELECT ? | + | SELECT ?languages ?languagesLabel |
− | |||
WHERE { | WHERE { | ||
− | entity:Q42 prop:P4 ?languages . | + | entity:Q42 prop:P4 ?languages . |
+ | SERVICE wikibase:label { | ||
+ | bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . | ||
+ | } | ||
} | } | ||
</query> | </query> | ||
|} | |} | ||
− | == Speaker Qid + language → list of all associated audios == | + | == ✅ Speaker Qid + language → list of all associated audios == |
− | |||
− | |||
{| | {| | ||
|- style="vertical-align:top;" | |- style="vertical-align:top;" | ||
Line 149: | Line 175: | ||
<syntaxhighlight lang="sparql"> | <syntaxhighlight lang="sparql"> | ||
SELECT ?audio WHERE { | SELECT ?audio WHERE { | ||
− | ?audio prop:P5 entity:Q42 . | + | ?audio prop:P5 entity:Q42 . # Condition 1, P5 Speaker is Q42 User:0x010C |
− | ?audio prop:P4 entity:Q21 . | + | ?audio prop:P4 entity:Q21 . # Condition 2, P4 language is Q21 French |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 156: | Line 182: | ||
<query _pagination="5" > | <query _pagination="5" > | ||
SELECT ?audio WHERE { | SELECT ?audio WHERE { | ||
− | ?audio prop:P5 entity:Q42 . # | + | ?audio prop:P5 entity:Q42 . # Condition 1, P5 Speaker is Q42 User:0x010C |
− | ?audio prop:P4 entity:Q21 . # | + | ?audio prop:P4 entity:Q21 . # Condition 2, P4 language is Q21 French |
} | } | ||
</query> | </query> | ||
|} | |} | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
== Is Language ([[Q3]]) → list all languages with number of unique words and speakers == | == Is Language ([[Q3]]) → list all languages with number of unique words and speakers == | ||
Line 255: | Line 287: | ||
|style="padding: 0 3em;width:60%"| | |style="padding: 0 3em;width:60%"| | ||
<syntaxhighlight lang="sparql"> | <syntaxhighlight lang="sparql"> | ||
− | SELECT ?audio WHERE { | + | SELECT ?audio |
+ | WHERE { | ||
?audio prop:P4 entity:Q209 . #language | ?audio prop:P4 entity:Q209 . #language | ||
?audio prop:P5 entity:Q584098 . #speaker | ?audio prop:P5 entity:Q584098 . #speaker | ||
Line 264: | Line 297: | ||
|| | || | ||
<query _pagination="5" > | <query _pagination="5" > | ||
− | SELECT ?audio WHERE { | + | SELECT ?audio |
+ | WHERE { | ||
?audio prop:P4 entity:Q209 . #language | ?audio prop:P4 entity:Q209 . #language | ||
?audio prop:P5 entity:Q584098 . #speaker | ?audio prop:P5 entity:Q584098 . #speaker |
Revision as of 14:57, 6 December 2021
Base
Fetch SPARQL data
Data can be fetched using various coding 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 implementations.
Javascript:
At least 3 methods exists (code snippet), example:
Query | Result |
---|---|
var endpoint = 'https://lingualibre.org/sparql';
var sparql = 'SELECT ?item WHERE { ?item prop:P2 entity:Q5 } LIMIT 10';
$.getJSON(endpoint,
{ query: sparql, format: 'json' },
function(data){ console.log('JQuery: ',data)}
);
|
{
"item" : {
"type" : "uri",
"value" : "https://lingualibre.org/entity/Q12"
},
"itemLabel" : {
"xml:lang" : "en",
"type" : "literal",
"value" : "beginner"
}
}
|
✅ Is Sex or Gender(sex or gender (Q7)) → list all possible values
SELECT ?item ?itemLabel
WHERE {
?item prop:P2 entity:Q5 # Condition 1, P2 'instance of' is Q5 'language level'.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
}
}
|
|
✅ Is Speaker (speaker (Q3)) → list all speakers
SELECT ?speaker ?speakerLabel
WHERE {
?speaker prop:P2 entity:Q3 . # Condition 1, P2 'instance of' is Q3 'speaker'.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
}
}
|
|
✅ Speaker name(s) → Speaker Qid(s)
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
# Get Q42 (User:0x010C)'s data
SELECT ?predicate ?predicate ?object ?objectLabel
WHERE {
entity:Q42 ?predicate ?object .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
}
}
|
|
✅🇶 Speaker Qid: 0x010C (Q42) → Speaker data → Speaker languages P4
SELECT ?languages ?languagesLabel
WHERE {
entity:Q42 prop:P4 ?languages .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
}
}
|
|
✅ Speaker Qid + language → list of all associated audios
SELECT ?audio WHERE {
?audio prop:P5 entity:Q42 . # Condition 1, P5 Speaker is Q42 User:0x010C
?audio prop:P4 entity:Q21 . # Condition 2, P4 language is Q21 French
}
|
|
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.