Help

Difference between revisions of "SPARQL"

Line 283: Line 283:
 
|}
 
|}
  
== Langue + speaker + word → Audio's Qid ==
+
== ✅ Language + speaker + word → Audio's Qid ==
 
+
Search
 
{|  
 
{|  
 
|- style="vertical-align:top;"
 
|- style="vertical-align:top;"
Line 291: Line 291:
 
SELECT ?audio
 
SELECT ?audio
 
WHERE {
 
WHERE {
   ?audio prop:P4 entity:Q209 . #language
+
   ?audio prop:P4 entity:Q209 .   # P4 'language' is Q209 'Breton'
   ?audio prop:P5 entity:Q584098 . #speaker
+
   ?audio prop:P5 entity:Q584098 . # P5 'speaker' is Q584098 'ThonyVezbe'
 
   ?audio rdfs:label ?word . #word
 
   ?audio rdfs:label ?word . #word
   FILTER ( STR(?word) = "ni" )
+
   FILTER ( STR(?word) = "ni" )   # word = 'ni'
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 301: Line 301:
 
SELECT ?audio
 
SELECT ?audio
 
WHERE {
 
WHERE {
   ?audio prop:P4 entity:Q209 . #language
+
   ?audio prop:P4 entity:Q209 .   # P4 'language' is Q209 'Breton'
   ?audio prop:P5 entity:Q584098 . #speaker
+
   ?audio prop:P5 entity:Q584098 . # P5 'speaker' is Q584098 'ThonyVezbe'
 
   ?audio rdfs:label ?word . #word
 
   ?audio rdfs:label ?word . #word
   FILTER ( STR(?word) = "ni" )
+
   FILTER ( STR(?word) = "ni" )   # word = 'ni'
 
}
 
}
 
</query>
 
</query>

Revision as of 18:32, 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" .
  } 
}
... Loading ...

✅ 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" .
  } 
}
... Loading ...

✅ 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 .
}
... Loading ...

✅🇶 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" .
  } 
}
... Loading ...

✅🇶 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" .
  } 
}
... Loading ...

✅ Speaker Qid + language → list of all associated audios

SELECT ?audio ?audioLabel
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
  # Labels
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
  } 
}
... Loading ...

✅ Is Language (speaker (Q3)) → list all languages with number of unique words and speakers

Too large to run on this page. Please test in Lingualibre Query.

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

Isolang → Language LL Qid

SELECT * WHERE {
  ?lang prop:P13 ?code .
}
... Loading ...

Isolang → Language WD Qid

SELECT * WHERE {
  ?lang prop:P12 ?idWikidata .
}
... Loading ...

Language WD Qid → Language data

SELECT * WHERE {
  ?lang prop:P12 "Q12107" .
  ?lang ?predicate ?object .
}
... Loading ...

Language LL Qid → Language data

SELECT * WHERE {
  entity:Q209 ?predicate ?object .
}
... Loading ...

✅ Language + speaker + word → Audio's Qid

Search

SELECT ?audio
WHERE {
  ?audio prop:P4 entity:Q209 .    # P4 'language' is Q209 'Breton'
  ?audio prop:P5 entity:Q584098 . # P5 'speaker' is Q584098 'ThonyVezbe'
  ?audio rdfs:label ?word . #word
  FILTER ( STR(?word) = "ni" )    # word = 'ni'
}
... Loading ...

Audio Qid → Audio data

✅ Langue + speaker + word → Audio's Commons url

Tools

  • Special:ApiSandbox – API queries generator for Lingualibre wikipage and wikibase contents.