Wikidata:Serviço de consulta SPARQL/Uma introdução suave ao serviço de consulta Wikidata

Other languages:
Afrikaans • ‎Bahasa Indonesia • ‎Deutsch • ‎English • ‎Türkçe • ‎català • ‎español • ‎français • ‎italiano • ‎polski • ‎português do Brasil • ‎svenska • ‎русский • ‎српски / srpski • ‎українська • ‎العربية • ‎বাংলা • ‎中文 • ‎日本語

Uma introdução suave ao serviço de consulta Wikidata

Já se perguntou coisas como:

  • Quais são as maiores cidades do mundo com uma prefeita mulher?
  • Que aeroportos estão localizados a 100 km de Berlin?
  • Quem foram todos os descendentes conhecidos de Genghis Khan?

Wikidata tem as respostas.

O que é o Wikidata?

Wikidata é um repositório de conhecimento. Você pode ler uma introdução à ideia e aos conceitos por trás do Wikidata na nossa pagina de introdução.

Como faço uma pergunta?

A linguagem de consulta usada para fazer perguntas como aquelas no Wikidata é chamada SPARQL. Se isto soa técnico demais e muito complicado para você, não se preocupe. Para esta introdução leve, você não terá que aprender SPARQL. Em vez disso, vamos ver exemplos de consultas que já existem e como podemos editá-las.

O que é uma consulta?

Uma consulta é uma forma especial de questão que sistemas computacionais podem entender e responder. A menos que você esteja interagindo com uma inteligência artificial, você precisará aprender a formular suas questões de um certo modo.

Posso usar livremente estes dados?

Oh, sim, Wikidata pode ser usada por qualquer um, pois toda a informação está em domínio público (CC0).

E agora?

Iniciaremos com um exemplo simples para lhe mostrar como usar este serviço, e nós continuaremos com exemplos mais elaborados, de tal modo que ao final terá a resposta às tuas perguntas. Prometemos.

Como usar Wikidata para uma consulta simples

Vamos por meio de um exemplo simples demonstrar como obter uma lista de todos os gatos conhecidos do mundo.

Obter uma lista de todos os gatos conhecidos do mundo

Use este URL para acessar o serviço de consulta: https://query.wikidata.org

  1. Clique em “Exemplos”
  2. Selecione “Gatos” na lista que aparece na nova janela
  3. Clique em “Executar” logo abaixo do bloco de código

 

Isto agora nos dá uma uma lista de todos os gatos famosos ou não presentes na internet - ou pelo menos daqueles que o Wikidata tem conhecimento. Isto é ótimo, já que você é aficionado por gatos e não, digamos, aficionado por cães.

E para os cachorros?

Se, ao invés, você deseja listar os cachorros (ou o que quer que seja), existem duas formas de editar sua consulta:

  1. Usar o Query Helper para mudar o item de gato para jardim zoologico:  
  2. Editar manualmente a consulta cada item no Wikidata é identificado univocamente usando um código. Este código é Q146 para “gato”. Para encontrar o código correspondente a “cachorro”, Você pode procurar no Wikidata:
  1. Use autocompletion (will be covered at the end)
  2. Start with "Cat" example, remove "Q146"
  3. Place cursor after "wd:"
  4. Type "Dog" after "wd:"
  5. Press Ctrl+Space
  6. Use up/down arrows; when you hit Enter - it will be substituted with Q144
    Note: search results depend on interface language.
  1. Go to https://www.wikidata.org
  2. Type “dog” in the search box
  3. Click on the first result from the drop-down menu
  4. Take note of the item number for “dog” from the Wikidata page that opens (Q144)

 

To change your query from “cat” to “dog”, simply replace Q146 by Q144 in the SPARQL query editor.

Run the program, and you will be presented with a list of all the famous and infamous dogs on the Internet.

 

Breaking down the query

Now let’s analyse the code to generalise this so we can search for any single object (for example, dogs, planets, countries, songs):

#Cats
SELECT ?item ?itemLabel
WHERE
{
	?item wdt:P31 wd:Q146 . 
	SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
  • SELECT A query SELECTs something. What exactly you want to select and display is described later, but for the time being you just state the placeholders (or variables). Here they are called ?item and ?itemLabel. You can tell that something is a variable if it starts with a question mark.
  • ?item This is the list of items we are looking for. In our case that would be the famous cats. Items on Wikidata work in many different languages, almost 300 of them. Because we don’t think that everything in the world is English and all the other languages are just special cases of English, items are stored in a way that works across languages. Every concept gets a number. Q146 is the concept of a cat, Q64 is Berlin in Germany, Q42 is the writer Douglas Adams and so on. Tip: ?item is just a variable name that we decided to use in our query. It could be anything, for example: ?animal.
  • ?itemLabel Humans can digest names in a human language much better than numbers. A label is the name of an item in a human language, e.g. English (or Esperanto, or Japanese, or Zulu…). We want the labels displayed in our query so that Q1371145 gets displayed as “Socks” (the cat belonging to Bill Clinton and family). Precise details about label service in User manual.
  • WHERE {   This is the WHERE clause of the query. Here we define what goes in the placeholders. A WHERE clause starts with an opening curly bracket { and ends with a closing curly bracket }.
  • wdt:P31 This is the predicate of the subject. If you hover over it, you’ll see that it stands for instance of.

 

  • wd:Q146 . This is the object. If you hover over it, you’ll see that it stands for cat.

 

All this is quite a mouthful. It may help to express the query in natural language:

“Give me a list of items and their labels, where the items are instances of cat. Then generate the labels to the items in English.”

With the code we used in our examples we showed you a list of all the famous and infamous cats on the Internet. This query can easily be changed to show a similar list for dogs, for example.

Text to SPARQL walkthrough

The animations below show how to write triplets in natural language, and convert them into SPARQL queries.

The key is to prefix variables with ?, items with wd: and properties with wdt:, and then using the Ctrl+Space (or Ctrl+Alt+Space or Alt+Enter) keyboard shortcut to activate the auto-completion feature that replaces the latter two with the actual Wikidata identifiers.

 

 

Diving into the User Interface

How to change the language that the results are displayed in?

With this search query tool, you can not only customise and search for simple or aggregated, compound, nested and complex queries but you can also search in ANY language and get results in ANY language too.

  • Change the default language code from “en” (English) to any other language code, say “ja” for Japanese.
#Cats  
SELECT ?item ?itemLabel WHERE
{
     ?item wdt:P31 wd:Q146 .
     SERVICE wikibase:label { bd:serviceParam wikibase:language "ja" }
}

How to change the sorting order of the results displayed?

The order of the items displayed can be easily changed in the interface. Just click on the relevant columns of the search results to customise the sorting order:

 

How to change the language of the UI?

Click the word English and select the language from the list displayed:

 

Notice the UI has changed to the selected language:

But I want pictures of cats! How to search for images?

So far, we’ve seen the output of the queries displayed as a table. Let’s try for some images:

Click the “Examples” button and select the “Even more cats, with pictures” example. Run the query and cat images should pop up at the lower half of the screen.

 

This is what displays the result as images instead of a table. You can manually switch how the data is displayed by using the “Display” menu in the lower right corner of the result. Try switching the menu to “Table” to see the same result in a table view.

Let’s have a look at what’s new in the query, compared to the query in the previous chapter. Written in plain English, the query would read “Give me all items that have something to do with cats. Also give me the image for each item. And by the way, display the result as a grid of images, not as a table.”

  • #defaultView:ImageGrid What looks like a comment, is actually an instruction to display the query result as images instead of a table.
  • ?x1 This means that we query for any predicate, not only for “is an instance of”. Think of predicates like “depicts” (P180) or “is named after” (P138). We are not using ?x1 anywhere else in the query, meaning the placeholder stands for “I don’t care what the predicate is, give me all results that somehow have a cat as the subject”
  • OPTIONAL Inside the curly braces you see another statement that adds optional data to the result. The placeholder ?item is reused and coupled with the predicate P18 (meaning “(has) image” and a new placeholder ?pic. Wrapping the statement with OPTIONAL means that items do not necessarily have to have a P18 predicate to show up in the list and that the ?pic placeholder can be empty.

Try modifying the query:

  • Search for dogs instead of cats. Hint: Change the Object part of the first statement in WHERE.
  • Only show items that “depict” cats (you’ll get paintings). Hint: replace ?x1 with something else.
  • Remove the OPTIONAL

There are other ways to show your data, but not all of them are always applicable, which is why some are greyed out in the menu. “Image map” is only selectable if the result data actually contains image URLs. In one of the following chapters you’ll learn how to display items as points on a map.

How to share your query for others to use?

If you want to share your query with someone else – say, on social media – you can create a short link for your query.

  • Complete your query
  • Click the LINK icon:
  • Copy the URL listed there. This is the URL for the query.

Finding things on maps

There are more ways to visualize the query results. If the query asks for geocoordinates, the results can be displayed on a map.

Look at this example of lighthouses in Norway. When you run the query, you’ll see red dots that mark the location of lighthouses on the Norwegian coast.


Using the Explorer Dialog

Once the results of a query have loaded, you can see a magnifying glass icon on each result. Clicking this icon opens the Explorer Dialog.

 

The explorer dialog by default, displays a single node representing that particular result. Below the node, you can see a toggle that defaults to ‘Outgoing’. This means that clicking on any node will expand all the properties of that node, from the entire Wikidata knowledge base. Toggling this to ‘Incoming’ allows you to see all the incoming links or properties that point to the node. This is a way to explore all the relationships between the various items and their properties using Wikidata.

 

You can open multiple explorer dialogs at once, allowing you to compare the results of more than one query at once. The explorer dialog also has a toolbar at the bottom right corner which is used to switch between multiple views like Graph, Tree, Map, Table etc.

There is even more

There are more visualizations for you to explore:

Look through the query examples and try them out!

Autocompletion

In the editor, you can press Ctrl+Space at any point in the query and get suggestions for code that might be appropriate; select the right suggestion with the up/down arrow keys, and press Enter to select it.

For example, instead of writing out SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } every time, you can just type SERV, hit Ctrl+Space, and the first suggestion will be that complete label service incantation, ready for use! Just hit Enter to accept it. (The formatting will be a bit different, but that doesn’t matter.)

And autocompletion can also search for you. If you type one of the Wikidata prefixes, like wd: or wdt:, and then just write text afterwards, Ctrl+Space will search for that text on Wikidata and suggest results. wd: searches for items, wdt: for properties. For example, instead of looking up the items for Johann Sebastian Bach (Q1339) and father (P22), you can just type wd:Bach and wdt:fath and then just select the right entry from the autocompletion. (This even works with spaces in the text, e. g. wd:Johann Sebastian Bach.)

NB: For ChromeOS and MacOS users: the shortcuts Ctrl+Alt+Space and Alt+Enter should also work.

Where to go from here

You can probably find your way around the Wikidata Query Service now. Exploring the interface and changing values in the queries can be a good way to learn how to ask your own questions on Wikidata.