ویکی داده: سرویس پرس و جوی SPARQL/مقدمهای کلی درباره سرویس کوئری ویکیداده
مقدمهای کلی درباره سرویس کوئری ویکیداده
آیا تاکنون به پرسشهایی از این دست فکر کردهاید؟
- بزرگترین شهرهای دنیا که شهردار زن دارند کدامند؟
- چه فرودگاههایی در 100 کیلومتری شهر برلین قرار دارند؟
- همه نوادگان شناخته شده چنگیز خان چه کسانی بودند؟
ویکی داده پاسخ این پرسشها را به شما میدهد.
ویکی داده چیست؟
ویکیداده مخزنی از دانش است. شما میتوانید مقدمهای بر مفاهیم و ایده ویکیداده را در صفحه مقدمه بخوانید.
چگونه پرسشم را مطرح کنم؟
زبان پرس و جویی که برای پرسیدن چنین پرسشهایی در ویکیداده استفاده می شود اسپارکل نام دارد. اگر این موضوع برای شما بسیار فنی و پیچیده به نظر می رسد، نگران نباشید. برای این مقدمه روان، نیازی به یادگیری SPARQL نخواهید داشت. در عوض، ما به نمونه پرس و جوهای موجود و نحوه ویرایش آنها نگاهی خواهیم انداخت.
یک کوئری چیست؟
پرس و جو یا کوئری شکل خاصی از پرسش است که سیستمهای رایانهای می توانند آن را تفسیر کرده و به آن پاسخ دهند. باید یاد بگیرید که چگونه پرسشهای خود را به روشی خاص فرموله کنید، مگر اینکه از هوش مصنوعی استفاده کنید.
آیا می توانم به طور آزادانه از این دادهها استفاده کنم؟
بله البته. ویکیداده می تواند توسط هر کسی برای هر چیزی استفاده شود زیرا همه دادهها در دامنه عمومی (CC0) هستند.
حالا چی؟
ما با یک مثال ساده شروع میکنیم تا نحوه استفاده از این سرویس را به شما نشان دهیم و همانطور که مثالهای با جزئیات بیشتری را ایجاد میکنیم، پاسخ این سؤال اساسی را خواهید گرفت. قول میدهیم.
نحوه استفاده از ویکیداده برای یک پرس و جوی ساده
بیایید یک مثال ساده را مرور کنیم که نشان می دهد چگونه فهرستی از همه گربههای شناخته شده در جهان را به دست آوریم.
دریافت فهرستی از همه گربههای جهان
از این نشانی اینترنتی برای دسترسی به سرویس پرس و جو استفاده کنید: https://query.wikidata.org
- روی Examples کلیک کنید
- Cats را از لیستی که در پنجره جدید ظاهر می شود انتخاب کنید
- روی دکمه Run درست زیر کادر کد کلیک کنید
اکنون فهرستی از تمام گربههای معروف و بدنام در اینترنت به ما ارائه شده است - یا حداقل گربههایی که ویکیداده درباره آنها میداند. این عالی است، به شرطی که شما یک گربه باشید و نه مثلاً یک سگ.
برای سگ ها چطور؟
اگر میخواهید سگها را به جای گربهها فهرست کنید (یا هر چیز دیگری)، دو راه برای ویرایش درخواست خود وجود دارد:
- از ""Query Helper"" برای تغییر مفهوم از گربه به باغ وحش استفاده کنید:
- ""پرس و جو را به شکل دستی ویرایش کنید"" هر مفهوم در ویکیداده با استفاده از یک کد منحصر به فرد شناسایی می شود. این کد برای "گربه" Q146 است. برای یافتن کد مربوطه برای «سگ»، میتوانید آن را در ویکیداده جستجو کنید:
- استفاده از تکمیل خودکار (در پایان گفته خواهد شد)
- با مثال "Cat" شروع کنید، "Q146" را حذف کنید
- مکاننما را بعد از "wd:" قرار دهید
- "Dog" را بعد از "wd:" تایپ کنید
- Ctrl+Space را فشار دهید
- از فلشهای بالا/پایین استفاده کنید. وقتی Enter را فشار دهید - با Q144 جایگزین می شود
- "توجه: نتایج جستجو به زبان رابط بستگی دارد."
- به https://www.wikidata.org بروید
- "Dog" را در کادر جستجو تایپ کنید
- روی اولین نتیجه از منوی کشویی کلیک کنید
- شماره مفهوم «Dog» را از صفحه ویکی داده ای که باز می شود یادداشت کنید (Q144)
برای تغییر پرس و جوی خود از «cat» به «dog»، به سادگی Q146 را با Q144 در ویرایشگر پرس و جو SPARQL جایگزین کنید.
برنامه را اجرا کنید و لیستی از تمام سگ های معروف و بدنام در اینترنت به شما نمایش داده می شود.
خاصتر کردن پرس و جو
حالا بیایید کد را برای تعمیم آن تجزیه و تحلیل کنیم تا بتوانیم هر شیء واحدی را جستجو کنیم (به عنوان مثال، سگ ها، سیارات، کشورها، آهنگ ها):
#Cats
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P31 wd:Q146 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
SELECT
یک پرس و جو چیزی را انتخاب می کند. اينكه دقیقاً چه چیزی را میخواهید انتخاب و نمایش دهید بعداً توضیح ميدهيد، اما فعلاً فقط جانماها (یا متغیرها) را بیان میکنید. آنها در اینجا?item
و?itemLabel
نامیده می شوند. می توانید بگویید که اگر چیزی با علامت سوال شروع شود یک متغیر است.?item
این فهرست مواردی است که ما به دنبال آن هستیم. در مثال ما این گربههای معروف هستند. مفاهيم موجود در ویکیداده به زبانهای مختلف کار میکنند، تقریباً 300 زبان. از آنجا که همه چیز در جهان به زبان انگلیسی نيست، مفاهيم به گونه ای ذخیره می شوند که در بین زبانها کار کند. براي هر مفهومی یک شناسه عددی اختصاص مییابد. Q146 مفهوم گربه است، Q64 شهر برلین در آلمان است، Q42 شناسه نویسنده داگلاس آدامز و مانند آن است. نکته:?item
یک متغیر است که ما تصمیم گرفتیم در پرس و جوی خود استفاده کنیم. متغیر هر چیزی میتواند باشد، برای مثال: ?animal?itemLabel
انسان ها خیلی بهتر می توانند اسامی را به زبان انسانی هضم کنند تا اعداد شناسه آنها. یک برچسب نام یک مفهوم در زبان انسانی است، به عنوان مثال. انگلیسی (یا اسپرانتو، یا ژاپنی، یا زولو…). ما میخواهیم برچسبها در جستار ما نمایش داده شود، در واقع Q1371145 به صورت "Socks" نمایش داده شود (گربه متعلق به بیل کلینتون و خانواده). جزئیات دقیق خدمات برچسب در راهنمای کاربر.WHERE {
این شرط WHERE در پرس و جو است. در این قسمت مشخص می کنیم که چه چیزی در متغیرها قرار میگیرد. یک شرط WHERE با یک آکولاد باز{
شروع می شود و با یک آکولاد بسته}
به پایان میرسد.wdt:P31
این گزاره (predicate) نهاد (Subject) است. اگر ماوس را روی آن نگه دارید، خواهید دید که نشان میدهد مفهوم شما نمونهای ( instance of) از چیست.
wd:Q146 .
این موضوع (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.
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.