ウィキデータ:SPARQL問合せサービス/ウィキデータ問合せサービスのやさしい入門

Outdated translations are marked like this.

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

ウィキデータクエリサービスのやさしい入門

次のような疑問を持ったことはありますか?

  • 女性が市長を務める、世界で最も大きな都市はどこか?
  • ベルリンから100km以内に位置する空港はどこか?
  • チンギス・カンの子孫として知られている人は誰か?

ウィキデータはこれらの疑問の答えを持っています。

ウィキデータとは何か?

ウィキデータは知識の倉庫(レポジトリ)です。 ウィキデータのアイディアやコンセプトの紹介は、はじめにで読むことができます。

どうやって尋ねるの?

ウィキデータにおいて,前掲のような疑問を尋ねるのに用いられる問合せ言語は,SPARQLと呼ばれます。SPARQLがあまりに技術的で難しすぎると思っても,心配しないで。本入門ではSPARQLを学ぶ必要はありません。代わりに,既存の例及びそれらの編集方法を見ていきます。

問合せとは何か?

問合せとは,計算機系が理解し回答できる,特別な形式の質問を言います。人工知能を相手にしているのでなければ,質問を特定の方法で定式化する手法を学ばなくてはなりません。

このデータは自由に使うことができますか?

はい。ウィキデータのすべてのデータは公有されている (CC0) ので、誰もがあらゆるデータを利用することができます。

今度はなに?

本サービスの使用法を示す為の単純な例から初めます。更に,より詳細な例を組み上げるにつれ,基本的な質問への解答を得られるでしょう。約束します。

ウィキデータの簡単な問合せの使い方

それでは、簡単な例として、世界の有名な猫の一覧を得る方法を見てみましょう。

世界のすべての猫の一覧を取得する

問合せサービスへは次のURLからアクセスしてください: https://query.wikidata.org

  1. 「例」をクリックする。
  2. 新しいウィンドウに現れる一覧から「Cats」を選択する。
  3. コードボックスのすぐ下の実行ボタンをクリックする。

 

これによって、インターネット上で見つかる――少なくともウィキデータが知っている――すべての有名な、あるいは無名な猫の一覧を得ることができました。これは、たとえあなたが猫派であってもそうでなくても、たとえば犬派であっても、すばらしいことでしょう。

犬についてはどうか?

代わりに犬(又は他の何か)の一覧が欲しい場合,問合せを編集する二通りの方法があります:

  1. 項目を猫 (cat) から動物園 (zoo) に変更する為にはQuery Helperを用います。  
  2. 問合せを手動で編集する Wikidata上の各項目は符号を用いて一意に識別されます。「猫」に対してはQ146という符号です。「犬」に対応する符号を見付けるには,Wikidataで調べることができます:
  1. 自動補完を利用する(最後に説明します)
  2. 「猫」の例から始めて,「Q146」を取り除く
  3. 「wd:」の後にカーソルを置く
  4. 「wd:」の後に「犬」と入力する
  5. Ctrl+Spaceを押下する
  6. 上下キーを押下する。Enterを押下すると,Q144に入れ替わります。
    注意: 検索結果は表示言語によって変わります。
  1. https://www.wikidata.org に移動する
  2. 検索欄に「犬」と入力する
  3. 吊り下げメニューの最初の結果をクリックする
  4. 表示されたWikidataページ (Q144) から「犬」の項目番号を書き留める

 

「猫」から「犬」に問合せを変更するには,単にSPARQL問合せエディタにてQ146をQ144で置き換えるだけです。

プログラムを実行すると,著名な犬からそうでないものまで全ての一覧が表示されます。

 

問合せの仕組み

さて,コードを分析して一般化し,任意の単独対象(例: 犬・惑星・国・歌)を検索できるようになりましょう:

#Cats
SELECT ?item ?itemLabel
WHERE
{
	?item wdt:P31 wd:Q146 . 
	SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
  • SELECT 問合せは某かを選択 (SELECT) します。正確には何を選択及び表示するのかについては後述しますが,当面は記入子(又は変数)を指定するだけです。ここでは,指定されたものを?item及び?itemLabelと呼びます。疑問符で始まっていれば,変数であると分かるでしょう。
  • ?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.

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.

文字からSPARQLへの立ち稽古

次の動画で自然言語でトリプルを書いて,それらをSPARQL問合せに変換する方法を見られます。

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.

 

 

ユーザーインターフェースを掘り下げる

結果が表示される言語を変更する方法

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" }
}

結果の表示の並び順を変更する方法

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:

 

UIの言語を変更する方法

Englishという言葉をクリックして,表示される一覧から言語を選択して下さい:

 

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.

自分の問合せを他者に用いてもらう方法

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.

地図上の位置を調べる

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 are more visualizations for you to explore:

Look through the query examples and try them out!

自動補完

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.

行き先案内

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.