1 of 40

Workshop #3

Introduction to Wikidata Query Service

2 of 40

REFRESHER

Workshops #1 & #2

Workshop #3

3 of 40

REFRESHER

Workshop #1 Introduction to Wikidata

A triple (also called statement in Wikidata) is the basic unit of a knowledge base.

A triple consists of:

Element

Example:

Property

Value

was composed by

Tchaikovsky

The Nutcracker

Introduction to Wikidata Query Service

Workshop #3

4 of 40

REFRESHER

Workshop#2 Contributing to Wikidata

  • Creating an item and avoiding duplicates, part 1
  • An item and its page, part 1
  • Main statement = instance of (P31), part 2
  • Properties & values, part 2
  • Referencing data, part 2
  • Qualifiers, part 2

  • Other Wikimedia projects, part 2
  • Discussion pages, part 2
  • Use Bistro for general questions, part 4
  • Ask specific questions and find answers in Wikidata projects, part 4

Introduction to Wikidata Query Service

Workshop #3

5 of 40

Workshop Scope

Workshop #1: Exploring Wikidata

Workshop #2: Creating a Wikidata item (André Laliberté)

Workshop #3 : How do you use Wikidata? With SparQL queries.

The best ways to use data is by searching and extracting it with SparQL. It’s the tool you use to check that the data you added to Wikidata is producing the results you want.

You don’t need to know everything about SparQL syntax to add data to Wikidata. However, it can help you understand how others use data and help you see your data input from different points of view.

Introduction to Wikidata Query Service

Workshop #3

6 of 40

Introduction to SparQL

Content

Part 1

  • Searching directly in Wikidata
  • Wiki page history

Part 2

  • Wikidata Query Service (WDQS)
  • SparQL langage
  • Comments, variables, syntax

Part 3

  • Extracting the list of "performing artist" subclasses
  • Displaying query results as a bubble graph
  • Displaying query results on a map graph

End of workshop

Introduction to Wikidata Query Service

Workshop #3

7 of 40

Part 1

Search and History in Wikidata

Workshop #3

8 of 40

Searching for Wikidata Entities

Before creating a new item (an entity), make sure you are not creating a duplicate.

  • With a name in another language
  • Under another name
  • Under an artist’s name (or if you search for an artist’s name)

There are several ways to search:

  • Wikidata Search Engine
  • Wikidata Query Service (WDQS)

Introduction to Wikidata Query Service

Workshop #3 Part 1

9 of 40

Searching in Wikidata: Header Field

Begin the search in the header field of the Wikidata page.

A menu appears when you type in this field. Pay attention to the last menu item, the one that will take you to a search page.

Notice how important a good description is in guiding your choice when items have the same label.

Introduction to Wikidata Query Service

Workshop #3 Part 1

10 of 40

Searching in Wikidata: Results Page

This page displays the results of a search.

Each result has three lines of data:

  1. Item label
  2. Description
  3. Number of statements and date �of last revision

Use property and item filters to refine a search.

Introduction to Wikidata Query Service

Workshop #3 Part 1

11 of 40

History of Page Revisions

Every page in a Wiki has a history. In Wikidata, every item has a history page.

A page’s history lets you see who made a revision, when it was done, and the scope of the revision in bytes.

It is also possible to compare different versions.

Introduction to Wikidata Query Service

Workshop #3 Part 1

12 of 40

End of Part 1

Search for the Stars!

Workshop #3

13 of 40

Part 2

SparQL Query Syntax

Workshop #3

14 of 40

SparQL Language

Queries are text-based and follow SparQL syntax.

URL of Wikidata query editor: https://query.wikidata.org/

SparQL language is highly structured and admittedly complex and a bit off-putting. But it is VERY powerful. You can create a SparQL query without having to memorize anything.

To learn how to use SparQL, I recommend this tutorial:�https://www.wikidata.org/wiki/Wikidata:SPARQL_tutorial

Introduction to Wikidata Query Service

Workshop #3 Part 2

15 of 40

A SparQL Query from an example

I recommend that you always start with an example (on button bar).

That way, you don't need to write your request from scratch!

Introduction to Wikidata Query Service

Workshop #3 Part 2

16 of 40

A SparQL Query

Variable

Comment

Service is for label names�Here, the choice of interface language, in French and if there are no results, in English as the default choice

Introduction to Wikidata Query Service

Workshop #3 Part 2

17 of 40

Editor of the Wikidata Query Service

You don't need to remember the identifiers of your elements. You can use the auto-completion feature.

To do so, type the name of the element or property after the namespace identifier (“wd:” for elements or “wdt:” for properties), then press on the keyboard keys [Control-Space].

Introduction to Wikidata Query Service

Workshop #3 Part 2

18 of 40

SparQL Language: Basic Syntax

SELECT: list of variables you want returned

WHERE: search criteria used to restrict

SERVICE: supplemental tools

GROUP BY: group the results according to one or several variables

ORDER BY: sort order of the results

Introduction to Wikidata Query Service

Workshop #3 Part 2

19 of 40

SparQL Language: Comments

Comments are preceded by a # (hash mark)

Some comments can change the display of results

Example: #defaultView:Map to display the results as geographic coordinates on a map

Introduction to Wikidata Query Service

Workshop #3 Part 2

20 of 40

SparQL Language: Variables

Variables are key to extracting data.

  • Variables start with ? (question mark): example "?item"
  • The name of a variable can be a series of upper and lowercase letters.
  • There are no spaces in �variable names.

Introduction to Wikidata Query Service

Workshop #3 Part 2

21 of 40

SparQL Language: Searching with Triples

In this example, we are searching for all items whose value, for the property "instance of (P31)" is "house cat (Q146)".

Since the variable is in the position of the main element (the subject), this request will extract all the elements in triplets where the property and the value are present.

Introduction to Wikidata Query Service

Workshop #3 Part 2

22 of 40

End of Part 2

Workshop #3

23 of 40

Part 3

Viewing Query Results in WDQS

Workshop #3

24 of 40

Subclasses of "performing artist"

SELECT ?vClass ?vLabel

WHERE

{

?vClass wdt:P279* wd:Q713200 .

OPTIONAL {

?vClass rdfs:label ?vLabel

FILTER (LANG(?vLabel) = "en") .

}

}

?vClass = variable containing all the classes that qualify

?vLabel = variable containing the name of the class of the variable ?vClass

P279 = subclass of

Q713200 = performing artist

Note the period.

https://w.wiki/bmh

Introduction to Wikidata Query Service

Workshop #3 Part 3

25 of 40

Subclasses of "performing artist"

Here is the result of the query.

The columns are the variables from the SELECT section.

In the results header, you can see « 185 results in 319 ms ».

You can download a file containing the results to your computer.

Introduction to Wikidata Query Service

Workshop #3 Part 3

26 of 40

WDQS : Bubble Chart Query

  • We are searching for all occupations that are subclasses of "performing artist (Q713200)".
  • Next, we search for all artists whose occupation is one of the occupations found previously.
  • Finally, we limit the search to artists who are Canadian citizens.

Introduction to Wikidata Query Service

Workshop #3 Part 3

27 of 40

WDQS : BubbleChart Query (cont.)

?vOccup wdt:P279 wd:Q713200

[occupations] subclass of performing artist

?vArtist wdt:P106* ?vOccup

[artists] occupation [occupations]

?vArtist wdt:P27 wd:Q16 �[artists] country of citizenship Canada

Variable containing occupations

Variable containing performing artists

Introduction to Wikidata Query Service

Workshop #3 Part 3

28 of 40

WDQS : BubbleChart Query (cont.)

wdt:P279�subclass of

wd:Q713200�performing artist

wdt:P106*�occupation

?vOccup�[occupations]

wdt:P27�country of citizenship

wd:Q16�Canada

?vArtist�[artists]

?vOccup�[occupations]

Introduction to Wikidata Query Service

Workshop #3 Part 3

29 of 40

WDQS: Viewing Results as a Bubble Chart

To display results as a Bubble chart, you need at least two columns of data.

A column with text or entities and a column with numeric values. Each entity is represented as a bubble, and the number sets the size of the bubble.

Introduction to Wikidata Query Service

Workshop #3 Part 3

30 of 40

WDQS: Map View Query

  1. Search for items that are theatres �(the physical space, not the art form).
  2. We extract each theatre’s coordinates (with the variable ?vCoord ).
  3. We limit the results to Canadian theatres.
  4. Note the comment a the end of the query telling it to display the results on a map

Introduction to Wikidata Query Service

Workshop #3 Part 3

31 of 40

WDQS: Map View Query (cont.)

?vTheatre wdt:P31* wd:Q24354

[theatres] instance of item theatre (building)

?vTheatre wdt:P625 ?vCoord

[theatres] geographic coordinates [geo. coordinates]

?vTheatre wdt:P17 wd:Q16

[theatres] country Canada

Variable containing geographic coordinates

Variable containing theatres

Introduction to Wikidata Query Service

Workshop #3 Part 3

32 of 40

WDQS: Viewing Results on a Map

  • You can scroll the map, but in this example, there are no point outside �of Canada.
  • You can add a map created with Wikidata’s map engine in any Web page, including your own websites.

Introduction to Wikidata Query Service

Workshop #3 Part 3

33 of 40

End of Part 3

Workshop #3

34 of 40

Recap

What you need to know

Workshop #3

35 of 40

Part 1 : Things to Remember

  • Use an internal Wikidata search for searches with the name (label) of an item.
  • It is important to avoid recreating items that already exist — duplicates. �Always perform a search before you create a new item.
  • A Wiki page’s history allows us to know the evolution of a page, and thus to know who made what change and when the revision was made.
  • For more complex searches based on an item’s statements, use Wikidata Query Service (WDQS).

Introduction to Wikidata Query Service

Workshop #3

36 of 40

Part 2: Things to Remember

  • Wikidata Query Services uses SparQL syntax.
  • Avoid having to memorize syntax. Use an example as your starting point.
  • Basic elements of SparQL syntax: structure, comments, variables.
  • A SparQL query consists of triples.

Introduction to Wikidata Query Service

Workshop #3

37 of 40

Part 3: Things to Remember

  • You can make very complexe searches with Wikidata Query Service.
  • Query results can be displayed as: tables, maps or charts of various types such as bubble charts, line charts, area charts, tree maps, etc.
  • With WDQS, you can control:
    • The language of the labels in the results of the query
    • The maximum number of results

Introduction to Wikidata Query Service

Workshop #3

38 of 40

Acknowledgments

Main Project Partners

Financial partners

Introduction to Wikidata Query Service

Workshop #3

39 of 40

End of Workshop

Keep on searching!

Workshop #3

40 of 40

Nom de la diapositive

Introduction to Wikidata Query Service

Workshop #3