<jupyter_start><jupyter_text>Wikidata.orgWikidata.org allows you to get information from Wiki sites through an API.Using the API requires you to write the query in [SPARQL](https://en.wikipedia.org/wiki/SPARQL), but there is plenty of example queries to work from at https://query.wikidata.org allowing you to get started without previous knowledge about this SPARQL.   Wikidata and MachinelearningAs Wiki is an internatinal project in +300 languages and has a lot of articles, Wikidata is a good start for harvesting data to use in machinelearning* Example SPARQL queries    * All [Swedish politicians in the Swedish Goverment](http://tinyurl.com/y4jo4lva)        * just those with a [twitter account](http://tinyurl.com/y4qb32q9)* In may started also the [Lexicographical data project](https://www.wikidata.org/wiki/Wikidata:Lexicographical_data/en) with the goal to "describe precisely all words in all languages" in a machinereadable formMore information:* [Wikidata](https://www.wikidata.org/wiki/Wikidata:Main_Page)* [The query editor](https://query.wikidata.org/)<jupyter_code>from IPython.display import YouTubeVideo
YouTubeVideo("AR66WVBViBQ")<jupyter_output><empty_output><jupyter_text>Example: Swedish Prime ministers<jupyter_code># Install a dependency using a terminal command
!pip install --quiet sparqlwrapper

# Allows us to use IPython.display.JSON
import IPython.display
from SPARQLWrapper import SPARQLWrapper, JSON

endpoint_url = "https://query.wikidata.org/sparql"

query = """# Query Find in WikiData people with position held P39 Swedish Prime minister Q687075
#

select  ?person ?personLabel ?personDescription ?replacedbyLabel   ?start ?pic ?end {
{
   ?person wdt:P39 wd:Q687075;
         p:P39 [
           ps:P39 wd:Q687075;
           pq:P580 ?start;
           pq:P582 ?end;
           pq:P1365 ?replace; #Replace
           pq:P1366 ?replacedby  #Replaced by
         ].
     OPTIONAL{ ?person wdt:P18 ?pic .} 					# If we have an illustration

 }   
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en"}
 }
Order by ?start"""


def get_results(endpoint_url, query):
    sparql = SPARQLWrapper(endpoint_url)
    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    return sparql.query().convert()


results = get_results(endpoint_url, query)
IPython.display.JSON(results)
for result in results["results"]["bindings"]:
    print(result["personLabel"]["value"], result["start"]["value"], " - ",  result["end"]["value"])<jupyter_output><NAME> 1880-04-19T00:00:00Z  -  1883-06-13T00:00:00Z
<NAME> 1883-06-13T00:00:00Z  -  1884-05-16T00:00:00Z
<NAME> 1884-05-16T00:00:00Z  -  1888-02-06T00:00:00Z
<NAME> 1888-02-06T00:00:00Z  -  1889-10-12T00:00:00Z
<NAME> 1889-10-12T00:00:00Z  -  1891-07-10T00:00:00Z
<NAME> 1891-07-10T00:00:00Z  -  1900-09-12T00:00:00Z
<NAME> 1900-09-12T00:00:00Z  -  1902-07-05T00:00:00Z
<NAME> 1902-07-05T00:00:00Z  -  1905-04-13T00:00:00Z
<NAME> 1905-04-13T00:00:00Z  -  1905-08-02T00:00:00Z
<NAME> 1905-08-02T00:00:00Z  -  1905-11-07T00:00:00Z
Karl Staaff 1905-11-07T00:00:00Z  -  1906-05-29T00:00:00Z
<NAME> 1906-05-29T00:00:00Z  -  1911-10-07T00:00:00Z
Karl Staaff 1911-10-07T00:00:00Z  -  1914-02-17T00:00:00Z
<NAME> 1914-02-17T00:00:00Z  -  1917-03-30T00:00:00Z
<NAME> 1917-03-30T00:00:00Z  -  1917-10-19T00:00:00Z
<NAME> 1917-10-19T00:00:00Z  -  1920-03-10T00:00:00Z
Hjalmar [...]<jupyter_text>Example: Airports near Berlin<jupyter_code>from SPARQLWrapper import SPARQLWrapper, JSON

endpoint_url = "https://query.wikidata.org/sparql"

query = """SELECT ?place ?placeLabel ?location
WHERE
{
  # Berlin coordinates
  wd:Q64 wdt:P625 ?berlinLoc .
  SERVICE wikibase:around {
    ?place wdt:P625 ?location .
    bd:serviceParam wikibase:center ?berlinLoc .
    bd:serviceParam wikibase:radius "100" .
  } .
  # Is an airport
  FILTER EXISTS { ?place wdt:P31/wdt:P279* wd:Q1248784 } .
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
  } 
}"""


def get_results(endpoint_url, query):
    sparql = SPARQLWrapper(endpoint_url)
    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    return sparql.query().convert()


results = get_results(endpoint_url, query)
IPython.display.JSON(results)
for result in results["results"]["bindings"]:
    print(result["placeLabel"]["value"])<jupyter_output>Berlin Tempelhof Airport
Berlin Schönefeld Airport
Berlin Tegel Airport
Dedelow Airport
Nauen Airport
Segeletz Airport
Lusse Airport
Eisenhuttenstadt Airport
Eisenhuttenstadt Airport
Kyritz Airport
Berlin Brandenburg Airport
Johannisthal Air Field
Kunice
Chojna
{'fraction_non_alphanumeric': 0.11969365426695842, 'fraction_numerical': 0.11466083150984682, 'mean_word_length': 4.581196581196581, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 7, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '494438', 'license': 'MIT', 'n_tokens_mistral': 2057, 'n_tokens_neox': 1728, 'n_words': 369, 'path': 'wikidata-org.ipynb', 'repo_name': 'salgo60/open-data-examples', 'size': '4704'}