Skip to content
Nintendofan885 edited this page Jul 25, 2020 · 17 revisions

Wikidata/SPARQL Query Service:

  • Wikidata is a free and open knowledge base that can be read and edited by both humans and machines.
  • Wikidata acts as central storage for the structured data of its Wikimedia sister projects including Wikipedia, Wikivoyage, Wiktionary, Wikisource, and others.

Usage of Wikidata/SPARQL Query Service:

  • Creating a dictionary using Wikidata Query Service/SPARQL saves time and energy. Wikidata is a knowledge database and SPARQL is a language to formulate queries using knowledge databases.

  • To start with, open the home page of WDQS/SPARQL : https://www.wikidata.org/wiki/Wikidata:WikiFactMine/Core_SPARQL

  • From the list on the left hand side of the home page, go to Query Service, this takes you to: https : //query.wikidata.org/

  • This opens the SPARQL query page where you can add your query. If you find any trouble in creating a SPARQL Query, you can create it from: https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/Query_Helper#How_to_create_a_Query?

    OR You can create a query without knowing SPARQL, it's too easy. Go to Query Helper on the left side of Query Editor and apply filters to your search, then test it by running the query and so on.

  • Wikidata Query Service provides various tools to build a SPARQL Query. One of them is Wikidata Query Builder :

    https://wd-query-builder.toolforge.org/
    
  • Just do the related search and Click on 'Show Query', this has built a SPARQL Query for you. eg. in my case, the property is Crossref Funder ID (P3153).

  • Set the limit of your results, it is by default 20, you can change it by clicking on to 'Query Options', I changed it to 20000 and got 13442 results.

  • Go to 'Search results': You will get a list of the items which you are searching for.

  • Go to 'link' and then 'SPARQL endpoint' and it's done.

  • The dictionary created is : https://github.com/petermr/openVirus/blob/master/dictionaries/funders/sparql%20(4)

  • Reference TUTORIAL for creating queries : https://www.youtube.com/watch?v=kJph4q0Im98

adding labels

We often need the human-readable labels for an Item. Almost all items have labels. However you have to use a label SERVICE for this:

SELECT ?funder ?funderLabel WHERE {
  ?funder wdt:P3153 ?crossrefid .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Every item has a label defined by adding Label as a suffix. So funder has funderLabel . The label is not a property but has to be generated. This is through a SERVICE

SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }

This is not easy to remember. It will generate labels for all the variables (i.e. ?foo) in the SELECT.

Here's the first part of my output:

funder.         funderLabel
 wd:Q750619	Chugai Pharmaceutical Co.
 wd:Q746879	British Medical Research Council
 wd:Q782600	Linköping University

SPARQL query for creating the Country dictionary

Maintained by: Ambreen H

  • Wikidata Query Service was used to write a query for retrieving all ISO counties and their details:

  • Care is taken to remove flags from the list of synonyms. They appear there because they are two characters from RGI aplhabets

  • Code used with explanations in comments:

## Select Query was used to retrieve specific results (Country name, wiki data number, Synonyms)
SELECT ?wikidata ?wikidataLabel ?code ?wikipedia ?wikidataAltLabel ?alt (?wikidataLabel as ?term) { 

## Forcing particular query execution order
  hint:Query hint:optimizer "None" . 

## all ISO countries 
  ?wikidata wdt:P297 ?code.

## Optional details about the countries like links to wikipaedia pages for each country to be presented in a seperate column
  OPTIONAL { ?wikipedia schema:about ?wikidata; schema:isPartOf <https://en.wikipedia.org/> }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en".

## Selecting the prefered label 
    ?wikidata skos:altLabel ?wikidataAltLabel ; rdfs:label ?wikidataLabel         
  } 

## Making sure the RGI alphabets of the flags are not rendered as flags and they appear as simple alphabets by specifying the acceptable characters. 
  BIND (REPLACE(REPLACE(?wikidataAltLabel, "(, )?[🇦-🇿]{2}", ""), "^, ", "") AS ?alt )
}

QUERY: SPARQL Query

Results: Country Dictionary SPARQL without flags

(PMR comment; The "letters" in the replacement of the flags are NOT the normal characters A-Z. Do not try to type them. Also if you cut and paste make sure that your tools' charset uses "UTF-8" as otherwise they might be corrupted.

Clone this wiki locally