Wikidata

Wikidata is one of the newer families added to the wikimedia projects. It acts as a central storage for structured data for all the wikimedia projects. It solves 2 major problems that wikimedia projects used to face:

  • Copied information (If the information changes it has to be manually edited in each website)
  • Unstructured data (A better method of storing data which can be queried using SQL or similar)
import pywikibot
wikidata = pywikibot.Site('wikidata', 'wikidata')
wikidata
DataSite("wikidata", "wikidata")
testwikidata = pywikibot.Site('test', 'wikidata')
testwikidata
DataSite("test", "wikidata")

1. Items and Properties

In wikidata, every page is either an item or a property.

Items are used to represent all the things in human knowledge: including topics, concepts, and objects. For example; color (Q1075), Albert Einstein (Q937), Earth (Q2), and cat (Q146) are all considered as items in Wikidata.

Properties are the things that describe and define a item. Each data bit related to an item is a type of property. Properties are different for different types of items. Examples of properties for Python (Q28865) are: license (P275), bug tracking system (P1401), official website (P856), Stack exchange tag (P1482).

The wikidata API helps to query data form wikidata using SparkQL to filter properties. So, for example you can find all countries in the world which have a population between 10 million to 300 million with just 1 query. In the earlier category interface, there would have to be a category for this information or you would have to parse evry country's page to find the population using Natural language processing!

Exercise - Contribute to wikidata

Find an item on wikidata and edit it to add some additional information. Some tips on finding an item:

  • Your favourite book or author
  • Your city or state, or a popular place near your hometown
  • A software, tool, or programming language that you like using
  • If you can type in a native language, try translating the label/title of an item

2. Examples of bots with wikidata

The wikidata game

The wikidata game is an example of a bot which helps users contribute better to wikidata. You can check out the wikidata game at https://tools.wmflabs.org/wikidata-game

The wikidata game finds possible pages which do not have a certain type of information using the structured queries (For example human items which have no gender) and shows the wikipedia page related to that item. Then the user is expected to identify a specific property of the item (For example male or female for the property gender).

The wikidata resonator

Wikidata resonator is a script which pulls data from wikidata and joins all the property data of an item to form a descriptive paragraph about the item. You can check it out at https://tools.wmflabs.org/reasonator/

Other than forming a descriptive paragraph of the item, it also groups similar properites like "Relative" group, "External sources" etc. based on some simple conditions. It also creates a timeline of the item if possible based on the properties that would have a datetime data type. It also generates a QR Code to use for the related wikipedia page and shows related images pulled from the related commons.wikimedia page!

3. Fetching data from wikidata using pywikibot

The first thing we're going to do is figure out how to get data from wikidata using pywikibot. Here, it's not like a generic mediawiki website, where the text of the page is pulled. Here, the data is structured. We use a ItemPage class in pywikibot which can handle these items in a better way:

itempage = pywikibot.ItemPage(wikidata, "Q48")  # Q42 is Douglas Adams
itempage
ItemPage('Q48')

In wikidata, the page.text won't work like other mediawiki websites where it gives a string of the whole content in the page. The data and properties are stored in Python dictionary structure:

itempage.get()
{'aliases': {'bho': ['एशिया'],
  'ca': ['Continent asiàtic', 'Continent Asiàtic'],
  'ko': ['아세아'],
  'nan': ['À-chiu'],
  'pt': ['continente asiático'],
  'ta': ['ஆசிய கண்டம்', 'ஆசியக் கண்டம்'],
  'th': ['เอเชีย', 'เอเซีย', 'ทวีปเอเซีย', 'Asia'],
  'vi': ['Á châu', 'Á'],
  'zh': ['亚细亚'],
  'zh-cn': ['亚细亚'],
  'zh-hans': ['亚细亚'],
  'zh-hant': ['亞細亞'],
  'zh-hk': ['亞細亞'],
  'zh-mo': ['亞細亞'],
  'zh-my': ['亚细亚'],
  'zh-sg': ['亚细亚'],
  'zh-tw': ['亞細亞']},
 'claims': {'P1036': [<pywikibot.page.Claim at 0x7f6c9eead4a8>],
  'P1151': [<pywikibot.page.Claim at 0x7f6c9eea7208>],
  'P1245': [<pywikibot.page.Claim at 0x7f6c9eea77f0>],
  'P1296': [<pywikibot.page.Claim at 0x7f6c9ee9ee80>],
  'P1343': [<pywikibot.page.Claim at 0x7f6c9eea2518>,
   <pywikibot.page.Claim at 0x7f6c9eea25c0>],
  'P1464': [<pywikibot.page.Claim at 0x7f6c9eea7438>],
  'P1465': [<pywikibot.page.Claim at 0x7f6c9eea2f98>],
  'P1549': [<pywikibot.page.Claim at 0x7f6c9eea2278>,
   <pywikibot.page.Claim at 0x7f6c9eea2320>],
  'P1566': [<pywikibot.page.Claim at 0x7f6c9eea2240>],
  'P1740': [<pywikibot.page.Claim at 0x7f6c9eea23c8>],
  'P1791': [<pywikibot.page.Claim at 0x7f6c9eea2d68>],
  'P18': [<pywikibot.page.Claim at 0x7f6c9eead588>],
  'P2082': [<pywikibot.page.Claim at 0x7f6c9eead518>],
  'P214': [<pywikibot.page.Claim at 0x7f6c9eea70f0>],
  'P2163': [<pywikibot.page.Claim at 0x7f6c9eea7c18>],
  'P227': [<pywikibot.page.Claim at 0x7f6c9eea77b8>],
  'P242': [<pywikibot.page.Claim at 0x7f6c9eead160>,
   <pywikibot.page.Claim at 0x7f6c9eead3c8>],
  'P244': [<pywikibot.page.Claim at 0x7f6c9eea7a90>],
  'P2612': [<pywikibot.page.Claim at 0x7f6c9eea7198>],
  'P2633': [<pywikibot.page.Claim at 0x7f6c9eea76a0>],
  'P2959': [<pywikibot.page.Claim at 0x7f6c9eea7860>],
  'P31': [<pywikibot.page.Claim at 0x7f6c9eead6d8>],
  'P3134': [<pywikibot.page.Claim at 0x7f6c9eea24e0>],
  'P349': [<pywikibot.page.Claim at 0x7f6c9eea7588>],
  'P361': [<pywikibot.page.Claim at 0x7f6c9ee8a748>,
   <pywikibot.page.Claim at 0x7f6c9ee8a7f0>,
   <pywikibot.page.Claim at 0x7f6c9ee8a940>,
   <pywikibot.page.Claim at 0x7f6c9ee8aa90>],
  'P373': [<pywikibot.page.Claim at 0x7f6c9ee9ed68>],
  'P376': [<pywikibot.page.Claim at 0x7f6c9eea7518>],
  'P443': [<pywikibot.page.Claim at 0x7f6c9ee8ac50>,
   <pywikibot.page.Claim at 0x7f6c9ee96b38>,
   <pywikibot.page.Claim at 0x7f6c9ee9a0f0>,
   <pywikibot.page.Claim at 0x7f6c9ee9a2e8>,
   <pywikibot.page.Claim at 0x7f6c9ee9a518>,
   <pywikibot.page.Claim at 0x7f6c9ee9a748>,
   <pywikibot.page.Claim at 0x7f6c9ee9a978>,
   <pywikibot.page.Claim at 0x7f6c9ee9aba8>,
   <pywikibot.page.Claim at 0x7f6c9ee9add8>,
   <pywikibot.page.Claim at 0x7f6c9ee9e048>,
   <pywikibot.page.Claim at 0x7f6c9ee9e278>,
   <pywikibot.page.Claim at 0x7f6c9ee9e4a8>,
   <pywikibot.page.Claim at 0x7f6c9ee9e6d8>,
   <pywikibot.page.Claim at 0x7f6c9ee9e940>,
   <pywikibot.page.Claim at 0x7f6c9ee9eb38>],
  'P47': [<pywikibot.page.Claim at 0x7f6c9eea2a90>,
   <pywikibot.page.Claim at 0x7f6c9eea2b00>],
  'P625': [<pywikibot.page.Claim at 0x7f6c9eea7e10>],
  'P646': [<pywikibot.page.Claim at 0x7f6c9eea2c50>],
  'P910': [<pywikibot.page.Claim at 0x7f6c9eea27f0>],
  'P935': [<pywikibot.page.Claim at 0x7f6c9eead668>],
  'P948': [<pywikibot.page.Claim at 0x7f6c9ee9ef98>],
  'P949': [<pywikibot.page.Claim at 0x7f6c9eea7b00>]},
 'descriptions': {'ca': 'continent',
  'cs': 'kontinent',
  'cy': 'cyfandir yn Hemisffêr y Dwyrain',
  'de': 'Kontinent der Erde',
  'el': 'ήπειρος',
  'en': "continent in Earth's Eastern Hemisphere",
  'en-gb': 'A continent',
  'eo': 'kontinento',
  'es': 'continente',
  'fa': 'بزرگترین قارۀ جهان',
  'fi': 'maapallon laajin maanosa',
  'fr': 'continent',
  'gl': 'continente',
  'gu': 'ખંડ',
  'he': 'יבשת',
  'hu': 'kontinens',
  'id': 'benua di dunia',
  'ilo': 'kontinente',
  'it': 'continente',
  'ja': '大陸、大州',
  'jv': 'bawana ing donya',
  'ko': '대륙',
  'la': 'continens',
  'min': 'benua',
  'nb': 'verdensdel som dekker størsteparten av kontinentet Eurasia',
  'ne': 'पृथ्वीको पूर्वी गोलार्द्धमा रहेको महाद्वीप',
  'nl': 'continent in het oostelijk halfrond van de aarde',
  'nn': 'verdsdel som dekker størsteparten av kontinentet Eurasia',
  'pa': 'ਮਹਾਦੀਪ',
  'pih': 'kontenent',
  'pl': 'kontynent',
  'pt': 'continente',
  'pt-br': 'continente',
  'ro': 'continent',
  'ru': 'часть света',
  'scn': 'cuntinenti',
  'sco': "continent in Yird's Eastren Hemisphere",
  'sd': 'ڌرتيءَ جي اوڀارين اڌ گول ۾ کنڊ',
  'sv': 'kontinent',
  'ta': 'உலகின் மிகப்பெரியதும், அதிக மக்கள்தொகை கொண்டதுமான ஒரு கண்டம்.',
  'te': 'ఖండం',
  'th': 'ทวีปในซีกโลกตะวันออก',
  'uk': 'частина світу, розташована головним чином у Східній півкулі',
  'vi': 'châu lục',
  'zh': '七大洲之一',
  'zh-cn': '七大洲之一',
  'zh-hans': '七大洲之一',
  'zh-hant': '七大洲之一',
  'zh-hk': '七大洲之一',
  'zh-mo': '七大洲之一',
  'zh-my': '七大洲之一',
  'zh-sg': '七大洲之一',
  'zh-tw': '七大洲之一'},
 'labels': {'ace': 'Asia',
  'ady': 'Азие',
  'af': 'Asië',
  'ak': 'Ehyia',
  'am': 'እስያ',
  'an': 'Asia',
  'ang': 'Asia',
  'ar': 'آسيا',
  'arc': 'ܐܣܝܐ',
  'arz': 'اسيا',
  'as': 'এছিয়া',
  'ast': 'Asia',
  'av': 'Азия',
  'ay': 'Asya',
  'az': 'Asiya',
  'azb': 'آسیا',
  'ba': 'Азия',
  'bar': 'Asien',
  'bcl': 'Asya',
  'be': 'Азія',
  'be-tarask': 'Азія',
  'bg': 'Азия',
  'bho': 'एशिया महादीप',
  'bi': 'Asia',
  'bjn': 'Asia',
  'bm': 'Asia',
  'bn': 'এশিয়া',
  'bo': 'ཨེ་ཤེ་ཡ།',
  'bpy': 'এশিয়া',
  'br': 'Azia',
  'brh': 'Eşíá',
  'bs': 'Azija',
  'bxr': 'Аази',
  'ca': 'Àsia',
  'cbk-zam': 'Asia',
  'cdo': 'Ā-ciŭ',
  'ce': 'Ази',
  'ceb': 'Asya',
  'chr': 'ᎡᏏᏱ',
  'chy': 'Asia',
  'ckb': 'ئاسیا',
  'co': 'Asia',
  'crh-latn': 'Asiya',
  'cs': 'Asie',
  'csb': 'Azëjô',
  'cu': 'Асїꙗ',
  'cv': 'Ази',
  'cy': 'Asia',
  'da': 'Asien',
  'de': 'Asien',
  'de-ch': 'Asien',
  'diq': 'Asya',
  'dsb': 'Azija',
  'el': 'Ασία',
  'en': 'Asia',
  'en-ca': 'Asia',
  'en-gb': 'Asia',
  'eo': 'Azio',
  'es': 'Asia',
  'et': 'Aasia',
  'eu': 'Asia',
  'ext': 'Ásia',
  'fa': 'آسیا',
  'ff': 'Aasiya',
  'fi': 'Aasia',
  'fo': 'Asia',
  'fr': 'Asie',
  'frp': 'Asia',
  'frr': 'Aasien',
  'fur': 'Asie',
  'fy': 'Aazje',
  'ga': 'An Áise',
  'gag': 'Aziya',
  'gan': '亞洲',
  'gd': 'Àisia',
  'gl': 'Asia',
  'glk': 'آسیا',
  'gn': 'Asia',
  'got': '𐌰𐍃𐌹𐌰',
  'grc': 'Ασία',
  'gsw': 'Asie',
  'gu': 'એશિયા',
  'gv': 'Yn Aishey',
  'ha': 'Asiya',
  'hak': 'Â-chû',
  'haw': '‘Ākia',
  'he': 'אסיה',
  'hi': 'एशिया',
  'hif': 'Asia',
  'hr': 'Azija',
  'hsb': 'Azija',
  'ht': 'Azi',
  'hu': 'Ázsia',
  'hy': 'Ասիա',
  'ia': 'Asia',
  'id': 'Asia',
  'ie': 'Asia',
  'ig': 'Asia',
  'ilo': 'Asia',
  'io': 'Azia',
  'is': 'Asía',
  'it': 'Asia',
  'iu': 'ᐊᓰᐊ',
  'ja': 'アジア',
  'jam': 'Ieja',
  'jbo': "zdotu'a",
  'jv': 'Asia',
  'ka': 'აზია',
  'kaa': 'Aziya',
  'kab': 'Asya',
  'kbd': 'Азиэ',
  'kg': 'Azia',
  'kk': 'Азия',
  'kl': 'Asia',
  'km': 'អាស៊ី',
  'kn': 'ಏಷ್ಯಾ',
  'ko': '아시아',
  'koi': 'Азия',
  'krc': 'Азия',
  'ks': 'ایشیا',
  'ku': 'Asya',
  'kv': 'Азия',
  'kw': 'Asi',
  'ky': 'Азия',
  'la': 'Asia',
  'lad': 'Asya',
  'lb': 'Asien',
  'lez': 'Азия',
  'lfn': 'Asia',
  'li': 'Azië',
  'lij': 'Asia',
  'lmo': 'Asia',
  'ln': 'Azía',
  'lo': 'ອາຊີ',
  'lrc': 'آسیا',
  'lt': 'Azija',
  'ltg': 'Azeja',
  'lv': 'Āzija',
  'lzh': '亞細亞洲',
  'mai': 'एशिया',
  'map-bms': 'Asia',
  'mdf': 'Азия',
  'mg': 'Azia',
  'mhr': 'Азий',
  'mi': 'Āhia',
  'min': 'Asia',
  'mk': 'Азија',
  'ml': 'ഏഷ്യ',
  'mn': 'Ази',
  'mo': 'Асия',
  'mr': 'आशिया',
  'mrj': 'Ази',
  'ms': 'Asia',
  'mt': 'Asja',
  'mwl': 'Ásia',
  'my': 'အာရှ',
  'mzn': 'آسیا',
  'na': 'Eija',
  'nah': 'Asia',
  'nan': 'A-chiu',
  'nap': 'Asia',
  'nb': 'Asia',
  'nds': 'Asien',
  'nds-nl': 'Azie',
  'ne': 'एसिया',
  'new': 'एसिया',
  'ng': 'Asia',
  'nl': 'Azië',
  'nn': 'Asia',
  'nov': 'Asia',
  'nrm': 'Âsie',
  'nso': 'Asia',
  'nv': '(*"asia")',
  'ny': 'Asia',
  'oc': 'Asia',
  'om': 'Eshiyaa',
  'or': 'ଏସିଆ',
  'os': 'Ази',
  'pa': 'ਏਸ਼ੀਆ',
  'pag': 'Asia',
  'pam': 'Asia',
  'pap': 'Asia',
  'pcd': 'Asie',
  'pdc': 'Asie',
  'pfl': 'Asie',
  'pih': 'Asya',
  'pl': 'Azja',
  'pms': 'Asia',
  'pnb': 'ایشیاء',
  'pnt': 'Ασίαν',
  'ps': 'آسيا',
  'pt': 'Ásia',
  'pt-br': 'Ásia',
  'qu': 'Asya',
  'rm': 'Asia',
  'rmy': 'Asia',
  'ro': 'Asia',
  'roa-tara': 'Asie',
  'ru': 'Азия',
  'rue': 'Азія',
  'rup': 'Asia',
  'rw': 'Aziya',
  'sa': 'जम्बूद्वीपः',
  'sah': 'Азия',
  'sc': 'Asia',
  'scn': 'Asia',
  'sco': 'Asie',
  'sd': 'ايشيا',
  'se': 'Ásia',
  'sgs': 'Azėjė',
  'sh': 'Azija',
  'si': 'ආසියාව',
  'sk': 'Ázia',
  'sl': 'Azija',
  'sm': 'Asia',
  'sn': 'Asia',
  'so': 'Aasiya',
  'sq': 'Azia',
  'sr': 'Азија',
  'srn': 'Asi',
  'st': 'Asia',
  'stq': 'Asien',
  'su': 'Asia',
  'sv': 'Asien',
  'sw': 'Asia',
  'szl': 'Azyjo',
  'ta': 'ஆசியா',
  'te': 'ఆసియా',
  'tet': 'Ázia',
  'tg': 'Осиё',
  'th': 'ทวีปเอเชีย',
  'tk': 'Aziýa',
  'tl': 'Asya',
  'tokipona': 'ma Asija',
  'tpi': 'Esia',
  'tr': 'Asya',
  'ts': 'Asia',
  'tt': 'Aziə',
  'tum': 'Asia',
  'ty': '’Ātia',
  'udm': 'Азия',
  'ug': 'ئاسىيا',
  'uk': 'Азія',
  'ur': 'ایشیاء',
  'uz': 'Osiyo',
  'vec': 'Axia',
  'vep': 'Azii',
  'vi': 'châu Á',
  'vls': 'Azië',
  'vo': 'Siyop',
  'vro': 'Aasia',
  'wa': 'Azeye',
  'war': 'Asya',
  'wo': 'Asi',
  'wuu': '亚洲',
  'xal': 'Азия',
  'xh': 'I-Eshiya',
  'xmf': 'აზია',
  'yi': 'אזיע',
  'yo': 'Ásíà',
  'yue': '亞洲',
  'za': 'Yacouh',
  'zea': 'Azië',
  'zh': '亚洲',
  'zh-cn': '亚洲',
  'zh-hans': '亚洲',
  'zh-hant': '亞洲',
  'zh-hk': '亞洲',
  'zh-mo': '亞洲',
  'zh-my': '亚洲',
  'zh-sg': '亚洲',
  'zh-tw': '亞洲',
  'zu': 'I-Eshiya'},
 'sitelinks': {'acewiki': 'Asia',
  'adywiki': 'Азие',
  'afwiki': 'Asië',
  'akwiki': 'Ehyia',
  'alswiki': 'Asien',
  'amwiki': 'እስያ',
  'angwiki': 'Asia',
  'anwiki': 'Asia',
  'arcwiki': 'ܐܣܝܐ (ܝܒܫܬܐ)',
  'arwiki': 'آسيا',
  'arwikinews': 'تصنيف:آسيا',
  'arzwiki': 'اسيا',
  'astwiki': 'Asia',
  'aswiki': 'এছিয়া',
  'avwiki': 'Азия',
  'aywiki': 'Asya',
  'azbwiki': 'آسیا',
  'azwiki': 'Asiya',
  'barwiki': 'Asien',
  'bat_smgwiki': 'Azėjė',
  'bawiki': 'Азия',
  'bclwiki': 'Asya',
  'be_x_oldwiki': 'Азія',
  'bewiki': 'Азія',
  'bgwiki': 'Азия',
  'bgwikinews': 'Категория:Азия',
  'bhwiki': 'एशिया महादीप',
  'biwiki': 'Esia',
  'bjnwiki': 'Asia',
  'bmwiki': 'Asia',
  'bnwiki': 'এশিয়া',
  'bowiki': 'ཨེ་ཤེ་ཡ།',
  'bpywiki': 'এশিয়া',
  'brwiki': 'Azia',
  'bswiki': 'Azija',
  'bswikinews': 'Kategorija:Azija',
  'bxrwiki': 'Ази',
  'cawiki': 'Àsia',
  'cawikinews': 'Categoria:Àsia',
  'cbk_zamwiki': 'Asia',
  'cdowiki': 'Ā-ciŭ',
  'cebwiki': 'Asya',
  'cewiki': 'Ази',
  'chrwiki': 'ᏓᎶᏂᎦᏍᏛ',
  'chywiki': 'Asia',
  'ckbwiki': 'ئاسیا',
  'commonswiki': 'Asia',
  'cowiki': 'Asia',
  'crhwiki': 'Asiya',
  'csbwiki': 'Azëjô',
  'cswiki': 'Asie',
  'cswikinews': 'Kategorie:Asie',
  'cuwiki': 'Асїꙗ',
  'cvwiki': 'Ази',
  'cywiki': 'Asia',
  'dawiki': 'Asien',
  'dewiki': 'Asien',
  'dewikinews': 'Kategorie:Asien',
  'dewikiquote': 'Asien',
  'dewikivoyage': 'Asien',
  'diqwiki': 'Asya',
  'dsbwiki': 'Azija',
  'elwiki': 'Ασία',
  'elwikinews': 'Κατηγορία:Ασία',
  'elwikivoyage': 'Ασία',
  'enwiki': 'Asia',
  'enwikibooks': 'Pinyin/Asia',
  'enwikinews': 'Category:Asia',
  'enwikiquote': 'Asia',
  'enwikivoyage': 'Asia',
  'eowiki': 'Azio',
  'eowikinews': 'Kategorio:Azio',
  'eswiki': 'Asia',
  'eswikinews': 'Categoría:Asia',
  'eswikiquote': 'Asia',
  'eswikivoyage': 'Asia',
  'etwiki': 'Aasia',
  'euwiki': 'Asia',
  'extwiki': 'Ásia',
  'fawiki': 'آسیا',
  'fawikinews': 'رده:آسیا',
  'fawikivoyage': 'آسیا',
  'ffwiki': 'Aasiya',
  'fiu_vrowiki': 'Aasia',
  'fiwiki': 'Aasia',
  'fiwikinews': 'Luokka:Aasia',
  'fowiki': 'Asia',
  'frpwiki': 'Asia',
  'frrwiki': 'Aasien',
  'frwiki': 'Asie',
  'frwikinews': 'Catégorie:Asie',
  'frwikivoyage': 'Asie',
  'furwiki': 'Asie',
  'fywiki': 'Aazje',
  'gagwiki': 'Aziya',
  'ganwiki': '亞洲',
  'gawiki': 'An Áise',
  'gdwiki': 'Àisia',
  'glkwiki': 'آسیا',
  'glwiki': 'Asia',
  'gnwiki': 'Ásia',
  'gotwiki': '𐌰𐍃𐌹𐌰',
  'guwiki': 'એશિયા',
  'gvwiki': 'Yn Aishey',
  'hakwiki': 'Â-chû',
  'hawiki': 'Asiya',
  'hawwiki': '‘Ākia',
  'hewiki': 'אסיה',
  'hewikinews': 'קטגוריה:אסיה',
  'hewikivoyage': 'אסיה',
  'hifwiki': 'Asia',
  'hiwiki': 'एशिया',
  'hrwiki': 'Azija',
  'hsbwiki': 'Azija',
  'htwiki': 'Azi',
  'huwiki': 'Ázsia',
  'hywiki': 'Ասիա',
  'iawiki': 'Asia',
  'idwiki': 'Asia',
  'iewiki': 'Asia',
  'igwiki': 'Asia',
  'ilowiki': 'Asia',
  'iowiki': 'Azia',
  'iswiki': 'Asía',
  'itwiki': 'Asia',
  'itwikinews': 'Categoria:Asia',
  'itwikiquote': 'Asia',
  'itwikivoyage': 'Asia',
  'iuwiki': 'ᐊᓰᐊ',
  'jamwiki': 'Ieja',
  'jawiki': 'アジア',
  'jawikinews': 'カテゴリ:アジア',
  'jbowiki': "zdotu'a",
  'jvwiki': 'Asia',
  'kaawiki': 'Aziya',
  'kabwiki': 'Asya',
  'kawiki': 'აზია',
  'kbdwiki': 'Азиэ',
  'kgwiki': 'Azia',
  'kkwiki': 'Азия',
  'klwiki': 'Asia',
  'kmwiki': 'អាស៊ី',
  'knwiki': 'ಏಷ್ಯಾ',
  'koiwiki': 'Азия',
  'kowiki': '아시아',
  'kowikinews': '분류:아시아',
  'krcwiki': 'Азия',
  'kswiki': 'ایشیا',
  'kuwiki': 'Asya',
  'kvwiki': 'Азия',
  'kwwiki': 'Asi',
  'kywiki': 'Азия',
  'ladwiki': 'Asya',
  'lawiki': 'Asia',
  'lbwiki': 'Asien',
  'lezwiki': 'Азия',
  'lgwiki': 'Asia',
  'lijwiki': 'Asia',
  'liwiki': 'Azië',
  'lmowiki': 'Asia',
  'lnwiki': 'Azía',
  'lowiki': 'ອາຊີ',
  'lrcwiki': 'آسیا',
  'ltgwiki': 'Azeja',
  'ltwiki': 'Azija',
  'lvwiki': 'Āzija',
  'maiwiki': 'एसिया',
  'map_bmswiki': 'Asia',
  'mdfwiki': 'Азия',
  'mgwiki': 'Azia',
  'mhrwiki': 'Азий',
  'minwiki': 'Asia',
  'miwiki': 'Āhia',
  'mkwiki': 'Азија',
  'mlwiki': 'ഏഷ്യ',
  'mnwiki': 'Ази',
  'mowiki': 'Асия',
  'mrjwiki': 'Ази',
  'mrwiki': 'आशिया',
  'mswiki': 'Asia',
  'mtwiki': 'Asja',
  'mwlwiki': 'Ásia',
  'mywiki': 'အာရှ',
  'mznwiki': 'آسیا',
  'nahwiki': 'Asia',
  'napwiki': 'Asia',
  'nawiki': 'Eija',
  'nds_nlwiki': 'Azie',
  'ndswiki': 'Asien',
  'newiki': 'एसिया',
  'newwiki': 'एसिया',
  'ngwiki': 'Asia',
  'nlwiki': 'Azië',
  'nlwikivoyage': 'Azië',
  'nnwiki': 'Asia',
  'novwiki': 'Asia',
  'nowiki': 'Asia',
  'nowikinews': 'Kategori:Asia',
  'nrmwiki': 'Âsie',
  'nsowiki': 'Asia',
  'nvwiki': '(*"asia")',
  'nywiki': 'Asia',
  'ocwiki': 'Asia',
  'omwiki': 'Eeshiyaa',
  'orwiki': 'ଏସିଆ',
  'oswiki': 'Ази',
  'pagwiki': 'Asia',
  'pamwiki': 'Asia',
  'papwiki': 'Asia',
  'pawiki': 'ਏਸ਼ੀਆ',
  'pcdwiki': 'Asie',
  'pdcwiki': 'Asie',
  'pflwiki': 'Asie',
  'pihwiki': 'Asya',
  'plwiki': 'Azja',
  'plwikinews': 'Kategoria:Azja',
  'plwikiquote': 'Azja',
  'plwikivoyage': 'Azja',
  'pmswiki': 'Asia',
  'pnbwiki': 'ایشیاء',
  'pntwiki': 'Ασίαν',
  'pswiki': 'آسيا',
  'ptwiki': 'Ásia',
  'ptwikinews': 'Categoria:Ásia',
  'ptwikivoyage': 'Ásia',
  'quwiki': 'Asya',
  'rmwiki': 'Asia',
  'rmywiki': 'Asia',
  'roa_rupwiki': 'Asia',
  'roa_tarawiki': 'Asie',
  'rowiki': 'Asia',
  'rowikinews': 'Categorie:Asia',
  'rowikivoyage': 'Asia',
  'ruewiki': 'Азія',
  'ruwiki': 'Азия',
  'ruwikinews': 'Категория:Азия',
  'ruwikiquote': 'Азия',
  'ruwikivoyage': 'Азия',
  'rwwiki': 'Aziya',
  'sahwiki': 'Азия',
  'sawiki': 'जम्बूद्वीपः',
  'scnwiki': 'Asia',
  'scowiki': 'Asie',
  'scwiki': 'Asia',
  'sdwiki': 'ايشيا',
  'sewiki': 'Ásia',
  'shwiki': 'Azija',
  'simplewiki': 'Asia',
  'siwiki': 'ආසියාව',
  'skwiki': 'Ázia',
  'slwiki': 'Azija',
  'smwiki': 'Asia',
  'snwiki': 'Asia',
  'sowiki': 'Aasiya',
  'sqwiki': 'Azia',
  'sqwikinews': 'Kategoria:Azi',
  'srnwiki': 'Asi (doti)',
  'srwiki': 'Азија',
  'srwikinews': 'Категорија:Азија',
  'stqwiki': 'Asien',
  'stwiki': 'Asia',
  'suwiki': 'Asia',
  'svwiki': 'Asien',
  'svwikinews': 'Kategori:Asien',
  'svwikivoyage': 'Asien',
  'swwiki': 'Asia',
  'szlwiki': 'Azyjo',
  'tawiki': 'ஆசியா',
  'tawikinews': 'பகுப்பு:ஆசியா',
  'tcywiki': 'ಏಷ್ಯಾ',
  'tetwiki': 'Ázia',
  'tewiki': 'ఆసియా',
  'tgwiki': 'Осиё',
  'thwiki': 'ทวีปเอเชีย',
  'tkwiki': 'Aziýa',
  'tlwiki': 'Asya',
  'towiki': 'ʻĒsia',
  'tpiwiki': 'Esia',
  'trwiki': 'Asya',
  'trwikinews': 'Kategori:Asya',
  'tswiki': 'Axiya',
  'ttwiki': 'Азия',
  'tumwiki': 'Asia',
  'tywiki': '’Ātia',
  'udmwiki': 'Азия',
  'ugwiki': 'ئاسىيا',
  'ukwiki': 'Азія',
  'ukwikinews': 'Категорія:Азія',
  'ukwikiquote': 'Азія',
  'ukwikivoyage': 'Азія',
  'urwiki': 'ایشیا',
  'uzwiki': 'Osiyo',
  'vecwiki': 'Axia',
  'vepwiki': 'Azii',
  'viwiki': 'Châu Á',
  'viwikivoyage': 'Châu Á',
  'vlswiki': 'Azië',
  'vowiki': 'Siyop',
  'warwiki': 'Asya',
  'wawiki': 'Azeye',
  'wowiki': 'Asi',
  'wuuwiki': '亚洲',
  'xalwiki': 'Азия',
  'xhwiki': 'I-Eshiya',
  'xmfwiki': 'აზია',
  'yiwiki': 'אזיע',
  'yowiki': 'Ásíà',
  'zawiki': 'Yacouh',
  'zeawiki': 'Azië',
  'zh_classicalwiki': '亞細亞洲',
  'zh_min_nanwiki': 'A-chiu',
  'zh_yuewiki': '亞洲',
  'zhwiki': '亚洲',
  'zhwikinews': 'Category:亚洲',
  'zhwikivoyage': '亚洲',
  'zuwiki': 'I-Eshiya'}}

If you want to get the data using the title of the page rather than the item ID, we can get the wikidata article associated to a wikipedia page using:

itempage = pywikibot.ItemPage.fromPage(pywikibot.Page(pywikibot.Site('en', 'wikipedia'), 'Douglas Adams'))
print(itempage)
[[wikidata:Q42]]

Let's take a closer look at the data items given by an item page. It is a dictionary with the following keys:

itemdata = itempage.get()
itemdata.keys()
dict_keys(['sitelinks', 'labels', 'descriptions', 'claims', 'aliases'])

Labels, Descriptions and Aliases

Labels are the name or title of the wikidata item.

Aliases are alternate labels for the same item in the same lanugage. For example, "Python (Q28865)" The programming language has the alias "Python language", "Python programming language", "/usr/bin/python", etc.

Descriptions are useful statements which can help distinguish items with similar labels. Wikidata items are unique only by their item ID (Qxx) hence the description helps differentiate behind "Python (Q271218)" the genus of reptiles, and "Python (Q28865)" the programming language, and "Python (Q15728)" the family of missiles!

As wikidata does not have a specific code/language (the code we use is "wikidata") it has data for all languages. Hence, the same item can have a different label in Engligh, Arabic, or French. So, these fields in the data are dictionaries with the key as the language code and the value as the label in that language.

itemdata['descriptions']
{'af': 'Engelse skrywer en humoris',
 'ar': 'كاتب إنجليزي فكاهي',
 'bar': 'a englischer Science-Fiction-Schriftsteller',
 'bg': 'английски писател и хуморист',
 'br': 'skrivagner saoznek',
 'ca': 'escriptor anglès',
 'cs': 'anglický spisovatel, humorista a dramatik',
 'cy': 'awdur a dychanwr Seisnig',
 'da': 'engelsk forfatter',
 'de': 'britischer Schriftsteller',
 'de-at': 'britischer Schriftsteller',
 'de-ch': 'britischer Schriftsteller',
 'el': 'Άγγλος συγγραφέας',
 'en': 'English writer and humorist',
 'en-gb': 'English writer and humourist',
 'eo': 'angla aŭtoro de sciencfikcio-romanoj kaj humoristo',
 'es': 'escritor y humorista británico',
 'fa': 'فیلمنامه\u200cنویس و نویسنده بریتانیایی',
 'fi': 'englantilainen kirjailija ja humoristi',
 'fr': 'écrivain anglais de science-fiction',
 'gsw': 'britischer Schriftsteller',
 'gu': 'અંગ્રેજી લેખક અને હાસ્યકાર',
 'he': 'סופר והומוריסטן בריטי',
 'hu': 'angol író',
 'hy': 'անգլիացի գրող, դրամատուրգ, սցենարիստ, «Ավտոստոպով զբոսաշրջիկի միջգալակտիկական ուղեցույց» վեպերի շարք',
 'it': 'scrittore inglese',
 'ja': 'イングランドの作家',
 'kn': 'ಇಂಗ್ಲಿಷ್ ಭಾಷೆಯ ಬರಹಗಾರ ಹಾಗೂ ಹಾಸ್ಯ ಲೇಖಕ',
 'lv': 'angļu zinātniskās fantastikas rakstnieks un humorists',
 'mk': 'англиски писател и хуморист',
 'nb': 'engelsk science fiction-forfatter og humorist',
 'nds': 'englischer Schriftsteller',
 'ne': 'अङ्ग्रेजी लेखक र व्यङ्ग्यकार',
 'nl': 'Engelse schrijver',
 'nn': 'engelsk sciencefictionforfattar og humorist',
 'pa': 'ਅੰਗਰੇਜ਼ੀ ਲੇਖਕ',
 'pl': 'brytyjski pisarz',
 'pt': 'escritor e comediante britânico',
 'pt-br': 'escritor e humorista inglês',
 'ro': 'scriitor, dramaturg englez',
 'ru': 'английский писатель, драматург и сценарист, автор серии книг «Автостопом по галактике»',
 'si': 'ඉංග්\u200dරීසි කවියෙක්',
 'sl': 'angleški pisatelj, humorist in dramatik',
 'sr': 'енглески писац научне фантастике и хумориста',
 'sr-ec': 'енглески писац научне фантастике и хумориста',
 'sr-el': 'engleski pisac naučne fantastike i humorista',
 'sv': 'brittisk författare och humorist',
 'ta': 'ஆங்கில எழுத்தாளர் மற்றும் நகைச்சுவையாளர்',
 'tr': 'İngiliz bilim kurgu ve mizah yazarı',
 'uk': 'британський комічний радіодраматург, письменник',
 'vi': 'Nhà văn và nhà soạn hài kịch người Anh',
 'zh': '英国作家',
 'zh-cn': '英国作家',
 'zh-hans': '英国作家',
 'zh-hant': '英國作家',
 'zh-hk': '英國作家',
 'zh-mo': '英國作家',
 'zh-my': '英国作家',
 'zh-sg': '英国作家',
 'zh-tw': '英國作家'}

For convenience, after the itempage.get() is called, the data is stored in the page variable also:

itemdata['labels'] == itempage.labels
True

Exercise - Check whether a given Item has a label in your native language

Find the language code for your native language and write a function to check if a given item has a label in that language, and what other aliases it has in that language.

Claims

Claims Are other wikidtaa pages that are linked to the given item using properties. The 'claims' are stored as another dictionary with the keys as property IDs (P1003, P1005, P1006, etc.) and the value is a list of objects pywikibot.page.Claim.

Hence, the claim is the value of all the properties that have been set for the given item.

itemdata['claims']
{'P1003': [<pywikibot.page.Claim at 0x7f3c31c29780>],
 'P1005': [<pywikibot.page.Claim at 0x7f3c31c29470>],
 'P1006': [<pywikibot.page.Claim at 0x7f3c31be1b38>,
  <pywikibot.page.Claim at 0x7f3c31be1f60>],
 'P1015': [<pywikibot.page.Claim at 0x7f3c31c94860>],
 'P103': [<pywikibot.page.Claim at 0x7f3c31befc50>,
  <pywikibot.page.Claim at 0x7f3c31befe48>],
 'P106': [<pywikibot.page.Claim at 0x7f3c31bff940>,
  <pywikibot.page.Claim at 0x7f3c31c04198>,
  <pywikibot.page.Claim at 0x7f3c31c04320>,
  <pywikibot.page.Claim at 0x7f3c31c04470>,
  <pywikibot.page.Claim at 0x7f3c31c048d0>,
  <pywikibot.page.Claim at 0x7f3c31c049e8>,
  <pywikibot.page.Claim at 0x7f3c31c04a90>,
  <pywikibot.page.Claim at 0x7f3c31c04d68>],
 'P108': [<pywikibot.page.Claim at 0x7f3c31c0e0b8>],
 'P119': [<pywikibot.page.Claim at 0x7f3c31bfb9b0>],
 'P1196': [<pywikibot.page.Claim at 0x7f3c31bde588>],
 'P1207': [<pywikibot.page.Claim at 0x7f3c31c0af60>],
 'P1233': [<pywikibot.page.Claim at 0x7f3c31c88dd8>],
 'P1258': [<pywikibot.page.Claim at 0x7f3c31c91b70>],
 'P1263': [<pywikibot.page.Claim at 0x7f3c31c294e0>],
 'P1266': [<pywikibot.page.Claim at 0x7f3c31bef358>],
 'P1273': [<pywikibot.page.Claim at 0x7f3c31c147b8>],
 'P1284': [<pywikibot.page.Claim at 0x7f3c31be1ac8>],
 'P1315': [<pywikibot.page.Claim at 0x7f3c31c0a978>],
 'P136': [<pywikibot.page.Claim at 0x7f3c31beb080>],
 'P1368': [<pywikibot.page.Claim at 0x7f3c31be1828>],
 'P1375': [<pywikibot.page.Claim at 0x7f3c31c141d0>],
 'P140': [<pywikibot.page.Claim at 0x7f3c31beb710>],
 'P1411': [<pywikibot.page.Claim at 0x7f3c31c29b00>,
  <pywikibot.page.Claim at 0x7f3c31c29fd0>],
 'P1412': [<pywikibot.page.Claim at 0x7f3c31c0ac18>],
 'P1415': [<pywikibot.page.Claim at 0x7f3c31bde0b8>],
 'P1417': [<pywikibot.page.Claim at 0x7f3c31c91c88>],
 'P1442': [<pywikibot.page.Claim at 0x7f3c31c292e8>],
 'P1477': [<pywikibot.page.Claim at 0x7f3c31bdbda0>],
 'P1559': [<pywikibot.page.Claim at 0x7f3c31d2ec50>],
 'P1670': [<pywikibot.page.Claim at 0x7f3c31be13c8>],
 'P1695': [<pywikibot.page.Claim at 0x7f3c31c142e8>],
 'P172': [<pywikibot.page.Claim at 0x7f3c31c8df60>],
 'P18': [<pywikibot.page.Claim at 0x7f3c31c0e5f8>],
 'P1816': [<pywikibot.page.Claim at 0x7f3c31d2ee48>],
 'P19': [<pywikibot.page.Claim at 0x7f3c31bf6f60>],
 'P1953': [<pywikibot.page.Claim at 0x7f3c31c0aba8>],
 'P20': [<pywikibot.page.Claim at 0x7f3c31bf6eb8>],
 'P2019': [<pywikibot.page.Claim at 0x7f3c31be1438>],
 'P21': [<pywikibot.page.Claim at 0x7f3c31c4b278>],
 'P213': [<pywikibot.page.Claim at 0x7f3c31c0a128>],
 'P214': [<pywikibot.page.Claim at 0x7f3c31c29710>],
 'P2163': [<pywikibot.page.Claim at 0x7f3c31bde2e8>],
 'P2168': [<pywikibot.page.Claim at 0x7f3c31bdeac8>],
 'P2188': [<pywikibot.page.Claim at 0x7f3c31bde048>],
 'P2191': [<pywikibot.page.Claim at 0x7f3c31bde518>],
 'P22': [<pywikibot.page.Claim at 0x7f3c31be1898>],
 'P227': [<pywikibot.page.Claim at 0x7f3c31c91be0>],
 'P2387': [<pywikibot.page.Claim at 0x7f3c31bef240>],
 'P2435': [<pywikibot.page.Claim at 0x7f3c31bef1d0>],
 'P244': [<pywikibot.page.Claim at 0x7f3c31c0ab38>],
 'P25': [<pywikibot.page.Claim at 0x7f3c31c29940>],
 'P26': [<pywikibot.page.Claim at 0x7f3c31bef470>],
 'P2604': [<pywikibot.page.Claim at 0x7f3c31befbe0>],
 'P2605': [<pywikibot.page.Claim at 0x7f3c31beb860>],
 'P2611': [<pywikibot.page.Claim at 0x7f3c31c91668>],
 'P2626': [<pywikibot.page.Claim at 0x7f3c31c14160>],
 'P268': [<pywikibot.page.Claim at 0x7f3c31c8d438>],
 'P269': [<pywikibot.page.Claim at 0x7f3c31bef400>],
 'P27': [<pywikibot.page.Claim at 0x7f3c31c88d30>],
 'P271': [<pywikibot.page.Claim at 0x7f3c31c14518>],
 'P2963': [<pywikibot.page.Claim at 0x7f3c31be1630>],
 'P31': [<pywikibot.page.Claim at 0x7f3c31c8d7b8>],
 'P345': [<pywikibot.page.Claim at 0x7f3c31c91780>],
 'P349': [<pywikibot.page.Claim at 0x7f3c31bdebe0>],
 'P373': [<pywikibot.page.Claim at 0x7f3c31bdbf28>],
 'P396': [<pywikibot.page.Claim at 0x7f3c31bde278>],
 'P40': [<pywikibot.page.Claim at 0x7f3c31c949e8>],
 'P409': [<pywikibot.page.Claim at 0x7f3c31c0a5c0>],
 'P434': [<pywikibot.page.Claim at 0x7f3c31c0a7b8>],
 'P509': [<pywikibot.page.Claim at 0x7f3c31bdea20>],
 'P535': [<pywikibot.page.Claim at 0x7f3c31bde358>],
 'P551': [<pywikibot.page.Claim at 0x7f3c31c88fd0>,
  <pywikibot.page.Claim at 0x7f3c31c88b38>,
  <pywikibot.page.Claim at 0x7f3c31c88c50>],
 'P569': [<pywikibot.page.Claim at 0x7f3c31c144a8>],
 'P570': [<pywikibot.page.Claim at 0x7f3c31c0e438>],
 'P646': [<pywikibot.page.Claim at 0x7f3c31be1128>],
 'P648': [<pywikibot.page.Claim at 0x7f3c31c14240>],
 'P69': [<pywikibot.page.Claim at 0x7f3c31bf6208>,
  <pywikibot.page.Claim at 0x7f3c31bf6390>],
 'P691': [<pywikibot.page.Claim at 0x7f3c31c0a198>],
 'P734': [<pywikibot.page.Claim at 0x7f3c31c916a0>],
 'P735': [<pywikibot.page.Claim at 0x7f3c31c8d470>,
  <pywikibot.page.Claim at 0x7f3c31c8d748>],
 'P800': [<pywikibot.page.Claim at 0x7f3c31c8d0b8>,
  <pywikibot.page.Claim at 0x7f3c31c910b8>,
  <pywikibot.page.Claim at 0x7f3c31c91c18>,
  <pywikibot.page.Claim at 0x7f3c31c91978>,
  <pywikibot.page.Claim at 0x7f3c31c91a20>],
 'P856': [<pywikibot.page.Claim at 0x7f3c31c91d30>],
 'P866': [<pywikibot.page.Claim at 0x7f3c31c91518>],
 'P9': [<pywikibot.page.Claim at 0x7f3c31c04ef0>],
 'P906': [<pywikibot.page.Claim at 0x7f3c31bef6a0>],
 'P910': [<pywikibot.page.Claim at 0x7f3c31c94cc0>],
 'P947': [<pywikibot.page.Claim at 0x7f3c31beb978>],
 'P949': [<pywikibot.page.Claim at 0x7f3c31bf6d68>],
 'P950': [<pywikibot.page.Claim at 0x7f3c31c29550>],
 'P998': [<pywikibot.page.Claim at 0x7f3c31d3af60>,
  <pywikibot.page.Claim at 0x7f3c31c94160>,
  <pywikibot.page.Claim at 0x7f3c31c942b0>,
  <pywikibot.page.Claim at 0x7f3c31c94470>,
  <pywikibot.page.Claim at 0x7f3c31c94630>,
  <pywikibot.page.Claim at 0x7f3c31c947f0>]}
# Similarly, this is available in the page object using:
itemdata['claims'] == itempage.claims
True

Let's take a look at the P800 (notable work) for the item Q42 (Douglas Adams):

itempage.claims['P800']
[<pywikibot.page.Claim at 0x7f3c31c8d0b8>,
 <pywikibot.page.Claim at 0x7f3c31c910b8>,
 <pywikibot.page.Claim at 0x7f3c31c91c18>,
 <pywikibot.page.Claim at 0x7f3c31c91978>,
 <pywikibot.page.Claim at 0x7f3c31c91a20>]

There are multiple claims for the property "P800" and we can ask pywikibot to resolve the claim and fetch the data about the claim:

itempage.claims['P800'][0].getTarget()
ItemPage('Q25169')

So, we notice that the claim for the first "notable work (P800)" of "Douglas Adams (Q42)" is the item Q25169. As this is another ItemPage we can fetch the english label for this item by doing:

p800_claim_target = itempage.claims['P800'][0].getTarget()
p800_claim_target.get()
p800_claim_target.labels['en']
"The Hitchhiker's Guide to the Galaxy pentalogy"

So, finally we were able to find one of the most notable work of Douglas Adams using the wikidata API exposed by pywikibot. Imagine doing the same in the english wikipedia !

Thought exercise: How would you figure out the most notable work of the author using the chunk of text given by an English wikipedia Page ?

Exercise - Check whether item is in India

Given a item ID, check whether the Item is in India by checking the value of the "country" property of the item. Write a function that checks this.

Hence, when the function is run on Q987 (New Delhi), it should give True but on Q62 (San Francisco) it should give False.

6. Property Pages

Sometimes, it is important to be able to fetch data about the property we find itself. For example, if we want to list the english label of the property and the value in a tabular form.

To do this, we use a PropertyPage object to deal with properties:

propertypage = pywikibot.PropertyPage(wikidata, 'P512')
propertypage
PropertyPage('Property:P512')

In the PropertyPage, we again can access the data similar to how it was accessed in the ItemPage:

propertypage.get()
propertypage.labels['en']

4. Wikidata data types

On Wikidata, we've already seen ItemPages and PropertyPages. But sometimes, a Claim's value need not be another Itempage, and can be some other data type like text, number, datetime, etc. Pywikibot provides a class for each of these data-types for easier accesibility to the value and resolve the claim.

The Data types available in wikidata can be seen at: https://www.wikidata.org/wiki/Special:ListDatatypes

The wikidata datatypes provided and the corresponding name of the wikidata data-type are:

  • Item - pywikibot.page.ItemPage - Link to other items at the project.
  • Property - pywikibot.page.PropertyPage - Link to properties at the project.
  • Global Coordinate - pywikibot.Coordinate - Literal data for a geographical position given as a latitude-longitude pair in gms or decimal degrees.
  • Time - pywikibot.WbTime - Literal data field for a point in time.
  • Quantity - pywikibot.WbQuantity - Literal data field for a quantity that relates to some kind of well-defined unit.
  • Monolingual Text - pywikibot.MonoLingualText - Literal data field for a string that is not translated into other languages.

Data types mapping to str

Some types of wikidata have are made specially to show them using a different method for example, showing it as a link, etc. But they all map to the python str. They are:

  • String - str - Literal data field for a string of glyphs. Generally do not depend on language of reader.
  • URL - str - Literal data field for a URL.
  • External Identifier - str - Literal data field for an external identifier. External identifiers may automatically be linked to an authoritative resource for display.
  • Mathematical formula - str - Literal data field for mathematical expressions, formula, equations and such, expressed in a variant of LaTeX.
# Item
item = pywikibot.ItemPage(wikidata, "Q42").get()['claims']['P31'][0].getTarget()
print("Type:", type(item))
print("Instance of Douglas Adams:", item, '(', item.get()['labels']['en'], ')')
# Property
_property = pywikibot.PropertyPage(wikidata, "Property:P31")
_property.get()
print("Type:", type(_property))
print("Property 'instance of':", _property, '(', _property.labels['en'], ')')
# Global Coordinate
coord = pywikibot.ItemPage(wikidata, "Q668").get()['claims']['P625'][0].getTarget()
print("Type:", type(coord))
print("Coordinate location of India:", coord)
# Time
_time = pywikibot.ItemPage(wikidata, "Q28865").get()['claims']['P571'][0].getTarget()
print("Type:", type(_time))
print("Inception of Python (programming language):", _time)
# Quantity
qty = pywikibot.ItemPage(wikidata, "Q668").get()['claims']['P1082'][0].getTarget()
print("Type:", type(qty))
print("Population in India:", qty)
# Monolingual text
monolingual_text = pywikibot.ItemPage(wikidata, "Q42").get()['claims']['P1477'][0].getTarget()
print("Type:", type(monolingual_text))
print("Birth name of Douglas Adams:", monolingual_text)
# String
_string = pywikibot.ItemPage(wikidata, "Q28865").get()['claims']['P348'][0].getTarget()
print("Type:", type(_string))
print("Version of Python:", _string)
# Mathematical Formula
formula = pywikibot.ItemPage(wikidata, "Q11518").get()['claims']['P2534'][0].getTarget()
print("Type:", type(formula))
print("Formula of Pythagorean theorem:", formula)

Exercise - Find URL and External identifier

Using the API, find the type and value of the Official website (P856) and the Freebase identifier (P646) of Python (Q28865).

5. Adding more meaning to Properties

Frequently, a property value may require additional data. For example, consider the property educated at (P69) in the earlier data fetched from Douglas Adams (Q42). It can be seen on WikiData that "St John's College" is mentioned as one of his education schools from "start time" of 1971 to "end time" of 1974. And it also says his "academic major" is English literature and "academic degree" was Bachelor of Arts.

Qualifiers

All this information would not be found if Wikidata was restricted to a (property, value) storage structure. Hence, Wikidata also allows Qualifiers. Qualifiers expand on the dta provided by a (property, value) pair by giving it context. Qualifiers also consist of a (property, value) !

In the above example, the properties which are being used as qualifiers are:

itempage.claims['P69']
itempage.claims['P69'][0].getTarget().get()
itempage.claims['P69'][0].getTarget().labels['en']
itempage.claims['P69'][0].qualifiers

The qualifiers are again claims, as they are similar to the (property, value) pair for item pages. Let us see what the value of the qualifier is by resolving the claim:

# Fetch the label of the P512 (academic degree) property
claim = itempage.claims['P69'][0]
claim.qualifiers['P512'][0].getTarget().get()
claim.qualifiers['P512'][0].getTarget().labels['en']

Some qualifiers may have a value which is not another item, for example the "start date" qualifier. In such cases, we need to check the type of the item:

claim = itempage.claims['P69'][0]
claim.qualifiers['P580'][0].getTarget()

Here, WBTime is a pywikibot class which handles the format of WikiBase Time. Wiki base is the underlying technology that powers the structured editing and so on of Wikidata.

Other functions to modify qualifiers are:

  • claim.removeQualifier()
  • claim.addQualifier()
  • claim.has_qualifier()

Exercise - Find time studied at school

In the case of Douglas Adams as we saw, there are 2 schools mentioned. Using the start time and end time, find the number of years that he studdied in any school and print it out in the format:

<school name>: <start year> to <end year> => <n> years

Reference (Sources)

Other than the qualifier, we would also want the source of the data. Hence, the reference or source field helps in adding, removing, editing these:

itempage.claims['P69'][0].getSources()

Again, the source is a (property, value) where the property describes what type of source it is. Some popular properties are:

A source is again a list of (property, value) tuples. It can have additional properties like: "original language of work", "publisher", "author", "title", "retrieved", etc. if necessary.

Let us take a look at a source here:

source = itempage.claims['P69'][0].getSources()[0]
source
# Get the value of the first tuple in the source:
source['P248']
source['P248'][0].getTarget().get()['labels']['en']

Exercise - Check number of values that have a source as English Wikipedia

A large number of data in wikidata was pulled from the "English Wikipedia". Go through all (property, value) pairs and check how many of them were taken from the English Wikipedia (Q328).

7. Search wikidata using python

The Search on wikidata can be triggered using the python api too. To do this, we use the pywikibot.data.api.Request class which provides with helper functions to query the wikidata website and fetch results:

india_search = pywikibot.data.api.Request(
    site=wikidata,
    parameters={"action": "wbsearchentities",
                "format": "json",
                "type": "item",
                "language": "en",
                "search": "India"})

india_search.submit()

As you can see, this function simply returns the dictionary search results directly. This needs to be parsed to be more useful. By modifying the parameters we can also search for the item using other languages and types.

Exercise: Create an ItemPage for every search result

The search result given by the API is a python dictionary. But it has a lot of data which may not be very useful to us.

  1. Loop over every search item and create a ItemPage object and store these in a list.
  2. Finally, loop over the ItemPage list and print the english label for each item in the search using the ItemPage class.

8. Further Reading

To read more ways of using pywikibot to access wikidata, go to https://www.wikidata.org/wiki/Wikidata:Pywikibot_-_Python_3_Tutorial

SparQL queries are SQL like queries that can be run on Wikidata to fetch data from it. To try out SparkQL queries and visualize the data using nice plots, you can use https://query.wikidata.org. It has a lot of example SparQL queries which can be useful to learn SparQL.