import pywikibot
from pywikibot.data import api
import re
lang_info ={'en': {'name':u'English',     'localname':u'English',  'weight': 1.0},
            'de': {'name':u'German',      'localname':u'Deutsch',  'weight':1.0},
            'fr': {'name':u'French',      'localname':u'Français', 'weight':1.0},
            'pl': {'name':u'Polish',      'localname':u'Polski',   'weight':1.1},
            'ja': {'name':u'Japanese',    'localname':u'日本語',    'weight':1.9},
            'it': {'name':u'Italian',     'localname':u'Italiano', 'weight':1.1},
            'nl': {'name':u'Dutch',       'localname':u'Nederlands', 'weight':0.9},
            'pt': {'name':u'Portuguese',  'localname':u'Português', 'weight':1.1},
            'es': {'name':u'Spanish',     'localname':u'Español',  'weight':1.1},
            'sv': {'name':u'Swedish',     'localname':u'Svenska',  'weight':1.1},
            'ru': {'name':u'Russian',     'localname':u'Русский',  'weight':1.4},
            'zh': {'name':u'Chinese',     'localname':u'中文',      'weight':3.7},
            'no': {'name':u'Norwegian (Bokmål)','localname':u'Norsk (Bokmål)', 'weight':1.2},
            'fi': {'name':u'Finnish',     'localname':u'Suomi', 'weight':1.1},
            'vo': {'name':u'Volapük',     'localname':u'Volapük'},
            'ca': {'name':u'Catalan',     'localname':u'Català', 'weight':1.1},
            'ro': {'name':u'Romanian',    'localname':u'Română', 'weight':1.1},
            'tr': {'name':u'Turkish',     'localname':u'Türkçe', 'weight':1.3},
            'uk': {'name':u'Ukrainian',   'localname':u'Українська', 'weight':1.3},
            'eo': {'name':u'Esperanto',   'localname':u'Esperanto', 'weight':1.1},
            'cs': {'name':u'Czech',       'localname':u'Čeština', 'weight':1.3},
            'hu': {'name':u'Hungarian',   'localname':u'Magyar', 'weight':1.1},
            'sk': {'name':u'Slovak',      'localname':u'Slovenčina', 'weight':1.3},
            'da': {'name':u'Danish',      'localname':u'Dansk', 'weight':1.2},
            'id': {'name':u'Indonesian',  'localname':u'Bahasa Indonesia', 'weight':0.9},
            'he': {'name':u'Hebrew',      'localname':u'עברית', 'weight':1.2},
            'lt': {'name':u'Lithuanian',  'localname':u'Lietuvių', 'weight':1.2},
            'sr': {'name':u'Serbian',     'localname':u'Српски / Srpski', 'weight':1.4},
            'sl': {'name':u'Slovenian',   'localname':u'Slovenščina', 'weight':1.2},
            'ko': {'name':u'Korean',      'localname':u'한국어', 'weight':2.5},
            'ar': {'name':u'Arabic',      'localname':u'العربية', 'weight':1.0},
            'bg': {'name':u'Bulgarian',   'localname':u'Български', 'weight':1.1},
            'et': {'name':u'Estonian',    'localname':u'Eesti', 'weight':1.2},
            'hr': {'name':u'Croatian',    'localname':u'Hrvatski', 'weight':1.3},
            'new':{'name':u'Newar / Nepal Bhasa','localname':u'नेपाल भाषा'},
            'te': {'name':u'Telugu',      'localname':u'తెలుగు'},
            'vi': {'name':u'Vietnamese',  'localname':u'Tiếng Việt', 'weight':1.1},
            'th': {'name':u'Thai',        'localname':u'ไทย', 'weight':1.0},
            'gl': {'name':u'Galician',    'localname':u'Galego', 'weight':1.2},
            'fa': {'name':u'Persian',     'localname':u'فارسی', 'weight':1.2},
            'nn': {'name':u'Norwegian (Nynorsk)','localname':u'Nynorsk', 'similar_lang':'no'},
            'ceb':{'name':u'Cebuano',     'localname':u'Sinugboanong Binisaya', 'weight':0.8},
            'el': {'name':u'Greek',       'localname':u'Ελληνικά', 'weight':1.1},
            'ms': {'name':u'Malay',       'localname':u'Bahasa Melayu', 'weight':1.0},
            'simple':{'name':u'Simple English','localname':u'Simple English'},
            'eu': {'name':u'Basque',      'localname':u'Euskara', 'weight':1.1},
            'bpy':{'name':u'Bishnupriya Manipuri','localname':u'ইমার ঠার/বিষ্ণুপ্রিয়া মণিপুরী'},
            'bs': {'name':u'Bosnian',     'localname':u'Bosanski', 'similar_lang':'hr'},
            'lb': {'name':u'Luxembourgish','localname':u'Lëtzebuergesch'},
            'is': {'name':u'Icelandic',   'localname':u'Íslenska', 'weight':1.1},
            'ka': {'name':u'Georgian',    'localname':u'ქართული'},
            'sq': {'name':u'Albanian',    'localname':u'Shqip'},
            'la': {'name':u'Latin',       'localname':u'Latina', 'weight':1.1},
            'br': {'name':u'Breton',      'localname':u'Brezhoneg'},
            'az': {'name':u'Azeri',       'localname':u'Azərbaycan', 'weight':1.2},
            'hi': {'name':u'Hindi',       'localname':u'हिन्दी', 'weight':1.0},
            'bn': {'name':u'Bengali',     'localname':u'বাংলা'},
            'ht': {'name':u'Haitian',     'localname':u'Krèyol ayisyen'},
            'mk': {'name':u'Macedonian',  'localname':u'Македонски', 'weight':1.3},
            'mr': {'name':u'Marathi',     'localname':u'मराठी'},
            'sh': {'name':u'Serbo-Croatian','localname':u'Srpskohrvatski / Српскохрватски', 'similar_lang':'hr'},
            'tl': {'name':u'Tagalog',     'localname':u'Tagalog'},
            'io': {'name':u'Ido',         'localname':u'Ido'},
            'cy': {'name':u'Welsh',       'localname':u'Cymraeg', 'weight':1.2},
            'pms':{'name':u'Piedmontese', 'localname':u'Piemontèis'},
            'lv': {'name':u'Latvian',     'localname':u'Latviešu', 'weight':1.1},
            'su': {'name':u'Sundanese',   'localname':u'Basa Sunda'},
            'ta': {'name':u'Tamil',       'localname':u'தமிழ்', 'weight':0.9},
            'jv': {'name':u'Javanese',    'localname':u'Basa Jawa'},
            'nap':{'name':u'Neapolitan',  'localname':u'Nnapulitano'},
            'oc': {'name':u'Occitan',     'localname':u'Occitan'},
            'nds':{'name':u'Low Saxon',   'localname':u'Plattdüütsch'},
            'scn':{'name':u'Sicilian',    'localname':u'Sicilianu'},
            'ast':{'name':u'Asturian',    'localname':u'Asturianu'},
            'ku': {'name':u'Kurdish',     'localname':u'Kurdî / كوردی'},
            'be': {'name':u'Belarusian',  'localname':u'Беларуская', 'similar_lang':'be-x-old'},
            'wa': {'name':u'Walloon',     'localname':u'Walon'},
            'af': {'name':u'Afrikaans',   'localname':u'Afrikaans', 'weight':1.0},
            'be-x-old':{'name':u'Belarusian (Taraškievica)','localname':u'Беларуская (тарашкевіца)', 'weight':1.4},
            'tg': {'name':u'Tajik',       'localname':u'Тоҷикӣ'},
            'an': {'name':u'Aragonese',   'localname':u'Aragonés', 'weight':1.1},
            'fy': {'name':u'West Frisian','localname':u'Frysk'},
            'vec':{'name':u'Venetian',    'localname':u'Vèneto'},
            'roa-tara':{'name':u'Tarantino',   'localname':u'Tarandíne'},
            'cv': {'name':u'Chuvash',     'localname':u'Чăваш'},
            'zh-yue':{'name':u'Cantonese',   'localname':u'粵語', 'similar_lang':'zh'},
            'ur': {'name':u'Urdu',        'localname':u'اردو'},
            'ksh':{'name':u'Ripuarian',   'localname':u'Ripoarisch'},
            'sw': {'name':u'Swahili',     'localname':u'Kiswahili'},
            'qu': {'name':u'Quechua',     'localname':u'Runa Simi'},
            'uz': {'name':u'Uzbek',       'localname':u'O‘zbek'},
            'mi': {'name':u'Maori',       'localname':u'Māori'},
            'ga': {'name':u'Irish',       'localname':u'Gaeilge'},
            'bat-smg':{'name':u'Samogitian',  'localname':u'Žemaitėška'},
            'ml': {'name':u'Malayalam',   'localname':u'മലയാളം', 'weight':1.1},
            'gd': {'name':u'Scottish Gaelic','localname':u'Gàidhlig'},
            'yo': {'name':u'Yoruba',      'localname':u'Yorùbá'},
            'co': {'name':u'Corsican',    'localname':u'Corsu'},
            'kn': {'name':u'Kannada',     'localname':u'ಕನ್ನಡ', 'weight':0.9},
            'pam':{'name':u'Kapampangan', 'localname':u'Kapampangan'},
            'yi': {'name':u'Yiddish',     'localname':u'ייִדיש'},
            'hsb':{'name':u'Upper Sorbian','localname':u'Hornjoserbsce'},
            'nah':{'name':u'Nahuatl',     'localname':u'Nāhuatl'},
            'ia': {'name':u'Interlingua', 'localname':u'Interlingua', 'weight':1.0},
            'li': {'name':u'Limburgian',  'localname':u'Limburgs'},
            'sa': {'name':u'Sanskrit',    'localname':u'संस्कृतम्'},
            'hy': {'name':u'Armenian',    'localname':u'Հայերեն', 'weight':1.2},
            'tt': {'name':u'Tatar',       'localname':u'Tatarça / Татарча'},
            'als':{'name':u'Alemannic',   'localname':u'Alemannisch', 'weight':1.1},
            'roa-rup':{'name':u'Aromanian',   'localname':u'Armãneashce'},
            'lmo':{'name':u'Lombard',     'localname':u'Lumbaart'},
            'map-bms':{'name':u'Banyumasan',  'localname':u'Basa Banyumasan'},
            'am': {'name':u'Amharic',     'localname':u'አማርኛ'},
            'nrm':{'name':u'Norman',      'localname':u'Nouormand/Normaund'},
            'zh-min-nan':{'name':u'Min Nan',     'localname':u'Bân-lâm-gú', 'weight':1.2},
            'pag':{'name':u'Pangasinan',  'localname':u'Pangasinan'},
            'wuu':{'name':u'Wu',          'localname':u'吴语', 'similar_lang':'zh'},
            'fo': {'name':u'Faroese',     'localname':u'Føroyskt'},
            'vls':{'name':u'West Flemish','localname':u'West-Vlams'},
            'nds-nl':{'name':u'Dutch Low Saxon','localname':u'Nedersaksisch'},
            'se': {'name':u'Northern Sami','localname':u'Sámegiella'},
            'rm': {'name':u'Romansh',     'localname':u'Rumantsch'},
            'ne': {'name':u'Nepali',      'localname':u'नेपाली'},
            'war':{'name':u'Waray-Waray', 'localname':u'Winaray'},
            'fur':{'name':u'Friulian',    'localname':u'Furlan'},
            'lij':{'name':u'Ligurian',    'localname':u'Líguru'},
            'nov':{'name':u'Novial',      'localname':u'Novial'},
            'bh': {'name':u'Bihari',      'localname':u'भोजपुरी'},
            'sco':{'name':u'Scots',       'localname':u'Scots'},
            'dv': {'name':u'Divehi',      'localname':u'ދިވެހިބަސް'},
            'pi': {'name':u'Pali',        'localname':u'पाऴि'},
            'diq':{'name':u'Zazaki',      'localname':u'Zazaki'},
            'ilo':{'name':u'Ilokano',     'localname':u'Ilokano'},
            'kk': {'name':u'Kazakh',      'localname':u'Қазақша', 'weight':1.3},
            'os': {'name':u'Ossetian',    'localname':u'Иронау'},
            'frp':{'name':u'Franco-Provençal/Arpitan','localname':u'Arpitan'},
            'zh-classical':{'name':u'Classical Chinese','localname':u'古文 / 文言文', 'similar_lang':'zh'},
            'mt': {'name':u'Maltese',     'localname':u'Malti'},
            'lad':{'name':u'Ladino',      'localname':u'Dzhudezmo'},
            'fiu-vro':{'name':u'Võro',        'localname':u'Võro'},
            'pdc':{'name':u'Pennsylvania German','localname':u'Deitsch'},
            'csb':{'name':u'Kashubian',   'localname':u'Kaszëbsczi'},
            'kw': {'name':u'Cornish',     'localname':u'Kernewek'},
            'bar':{'name':u'Bavarian',    'localname':u'Boarisch'},
            'to': {'name':u'Tongan',      'localname':u'faka Tonga'},
            'haw':{'name':u'Hawaiian',    'localname':u'Hawai`i'},
            'mn': {'name':u'Mongolian',   'localname':u'Монгол'},
            'ps': {'name':u'Pashto',      'localname':u'پښتو'},
            'ang':{'name':u'Anglo-Saxon', 'localname':u'Englisc'},
            'km': {'name':u'Khmer',       'localname':u'ភាសាខ្មែរ'},
            'gv': {'name':u'Manx',        'localname':u'Gaelg'},
            'tk': {'name':u'Turkmen',     'localname':u'تركمن / Туркмен'},
            'ln': {'name':u'Lingala',     'localname':u'Lingala'},
            'ie': {'name':u'Interlingue', 'localname':u'Interlingue'},
            'tpi':{'name':u'Tok Pisin',   'localname':u'Tok Pisin'},
            'crh':{'name':u'Crimean Tatar','localname':u'Qırımtatarca'},
            'jbo':{'name':u'Lojban',      'localname':u'Lojban', 'weight':1.2},
            'wo': {'name':u'Wolof',       'localname':u'Wolof'},
            'ay': {'name':u'Aymara',      'localname':u'Aymar'},
            'zea':{'name':u'Zealandic',   'localname':u'Zeêuws'},
            'eml':{'name':u'Emilian-Romagnol','localname':u'Emiliàn e rumagnòl'},
            'si': {'name':u'Sinhalese',   'localname':u'සිංහල'},
            'sc': {'name':u'Sardinian',   'localname':u'Sardu'},
            'or': {'name':u'Oriya',       'localname':u'ଓଡ଼ିଆ'},
            'ig': {'name':u'Igbo',        'localname':u'Igbo'},
            'mg': {'name':u'Malagasy',    'localname':u'Malagasy'},
            'cbk-zam':{'name':u'Zamboanga Chavacano','localname':u'Chavacano de Zamboanga'},
            'gu': {'name':u'Gujarati',    'localname':u'ગુજરાતી'},
            'ky': {'name':u'Kirghiz',     'localname':u'Кыргызча'},
            'kg': {'name':u'Kongo',       'localname':u'KiKongo'},
            'ty': {'name':u'Tahitian',    'localname':u'Reo Mā`ohi'},
            'glk':{'name':u'Gilaki',      'localname':u'گیلکی'},
            'arc':{'name':u'Assyrian Neo-Aramaic','localname':u'ܐܪܡܝܐ'},
            'gn': {'name':u'Guarani',     'localname':u'Avañe\'ẽ'},
            'kab':{'name':u'Kabyle',      'localname':u'Taqbaylit'},
            'so': {'name':u'Somali',      'localname':u'Soomaaliga'},
            'ks': {'name':u'Kashmiri',    'localname':u'कश्मीरी / كشميري'},
            'stq':{'name':u'Saterland Frisian','localname':u'Seeltersk'},
            'mzn':{'name':u'Mazandarani', 'localname':u'مَزِروني'},
            'cu': {'name':u'Old Church Slavonic','localname':u'Словѣньскъ'},
            'ce': {'name':u'Chechen',     'localname':u'Нохчийн'},
            'udm':{'name':u'Udmurt',      'localname':u'Удмурт кыл'},
            'tet':{'name':u'Tetum',       'localname':u'Tetun'},
            'sd': {'name':u'Sindhi',      'localname':u'سنڌي، سندھی ، सिन्ध'},
            'pap':{'name':u'Papiamentu',  'localname':u'Papiamentu'},
            'ba': {'name':u'Bashkir',     'localname':u'Башҡорт', 'weight':1.4},
            'pa': {'name':u'Punjabi',     'localname':u'ਪੰਜਾਬੀ'},
            'rmy':{'name':u'Romani',      'localname':u'romani - रोमानी'},
            'lo': {'name':u'Lao',         'localname':u'ລາວ'},
            'na': {'name':u'Nauruan',     'localname':u'dorerin Naoero'},
            'bcl':{'name':u'Central Bicolano','localname':u'Bikol'},
            'kaa':{'name':u'Karakalpak',  'localname':u'Qaraqalpaq tili'},
            'gan':{'name':u'Gan',         'localname':u'贛語', 'similar_lang':'zh'},
            'iu': {'name':u'Inuktitut',   'localname':u'ᐃᓄᒃᑎᑐᑦ'},
            'myv':{'name':u'Erzya',       'localname':u'Эрзянь (Erzjanj Kelj)'},
            'szl':{'name':u'Silesian',    'localname':u'Ślůnski'},
            'sah':{'name':u'Sakha',       'localname':u'Саха тыла (Saxa Tyla)'},
            'my': {'name':u'Burmese',     'localname':u'Burmese'},
            'ext':{'name':u'Extremaduran','localname':u'Estremeñu'},
            'hif':{'name':u'Fiji Hindi',  'localname':u'Fiji Hindi'},
            'bo': {'name':u'Tibetan',     'localname':u'བོད་སྐད་'},
            'srn':{'name':u'Sranan',      'localname':u'Sranantongo'},
            'got':{'name':u'Gothic',      'localname':u'ðミフᄇðミフ﾿ðミヘトðミフᄚðミヘツðミフᄚðミフᄊðミフᄈðミフᄚ'},
            'dsb':{'name':u'Lower Sorbian','localname':u'Dolnoserbšćina'},
            'bm': {'name':u'Bambara',     'localname':u'Bamanankan'},
            'sm': {'name':u'Samoan',      'localname':u'Gagana Samoa'},
            'cdo':{'name':u'Min Dong',    'localname':u'Mìng-dĕ̤ng-ngṳ̄'},
            'chr':{'name':u'Cherokee',    'localname':u'ᏣᎳᎩ ᎧᏬᏂᎯᏍᏗ'},
            'mdf':{'name':u'Moksha',      'localname':u'Мокшень (Mokshanj Kälj)'},
            'om': {'name':u'Oromo',       'localname':u'Oromoo'},
            'ee': {'name':u'Ewe',         'localname':u'Eʋegbe'},
            'as': {'name':u'Assamese',    'localname':u'অসমীয়া ভাষা আৰু লিপি'},
            'ti': {'name':u'Tigrinya',    'localname':u'ትግርኛ_ፊደል'},
            'ug': {'name':u'Uyghur',      'localname':u'Oyghurque'},
            'kv': {'name':u'Komi',        'localname':u'Коми'},
            'zu': {'name':u'Zulu',        'localname':u'IsiZulu'},
            'av': {'name':u'Avar',        'localname':u'Авар'},
            'nv': {'name':u'Navajo',      'localname':u'Diné bizaad'},
            'ss': {'name':u'Swati',       'localname':u'SiSwati'},
            'pih':{'name':u'Norfolk',     'localname':u'Norfuk'},
            'ts': {'name':u'Tsonga',      'localname':u'Xitsonga'},
            'cr': {'name':u'Cree',        'localname':u'Nehiyaw'},
            've': {'name':u'Venda',       'localname':u'TshiVenda'},
            'ch': {'name':u'Chamorro',    'localname':u'Chamoru'},
            'bi': {'name':u'Bislama',     'localname':u'Bislama'},
            'xh': {'name':u'Xhosa',       'localname':u'IsiXhosa'},
            'rw': {'name':u'Kinyarwanda', 'localname':u'Kinyarwanda'},
            'dz': {'name':u'Dzongkha',    'localname':u'རྫོང་ཁ་'},
            'tn': {'name':u'Tswana',      'localname':u'Setswana'},
            'kl': {'name':u'Greenlandic', 'localname':u'Kalaallisut'},
            'bug':{'name':u'Buginese',    'localname':u'Basa Ugi'},
            'ik': {'name':u'Inupiak',     'localname':u'Iñupiak uqautchit'},
            'bxr':{'name':u'Buryat (Russia)','localname':u'Буряад'},
            'st': {'name':u'Sesotho',     'localname':u'Sesotho'},
            'xal':{'name':u'Kalmyk',      'localname':u'Хальмг келн'},
            'ny': {'name':u'Chichewa',    'localname':u'Chicheŵa'},
            'ak': {'name':u'Akan',        'localname':u'Akana'},
            'ab': {'name':u'Abkhazian',   'localname':u'Аҧсуа бызшәа'},
            'fj': {'name':u'Fijian',      'localname':u'Na Vosa Vakaviti'},
            'lg': {'name':u'Luganda',     'localname':u'Luganda'},
            'tw': {'name':u'Twi',         'localname':u'Twi'},
            'ha': {'name':u'Hausa',       'localname':u'هَوُسَ'},
            'za': {'name':u'Zhuang',      'localname':u'Sawcuengh'},
            'ff': {'name':u'Fula',        'localname':u'Fulfulde'},
            'lbe':{'name':u'Lak',         'localname':u'Лакку маз'},
            'ki': {'name':u'Kikuyu',      'localname':u'Gĩgĩkũyũ'},
            'sn': {'name':u'Shona',       'localname':u'ChiShona'},
            'tum':{'name':u'Tumbuka',     'localname':u'ChiTumbuka'},
            'sg': {'name':u'Sango',       'localname':u'Sängö'},
            'chy':{'name':u'Cheyenne',    'localname':u'Tsetsêhestâhese'},
            'rn': {'name':u'Kirundi',     'localname':u'Kirundi'},
            'arz':{'name':u'Egyptian Arabic', 'localname':u'مصرى (Maṣrī)', 'similar_lang':'ar'},
            'pnt':{'name':u'Pontic',          'localname':u'Ποντιακά', 'similar_lang':'el'},
            'mhr':{'name':u'Meadow Mari',     'localname':u'Олык Марий'},
            'ace':{'name':u'Acehnese',        'localname':u'Acèh'},
            'ckb':{'name':u'Soranî',          'localname':u'Soranî / کوردی'},
            'mwl':{'name':u'Mirandese',       'localname':u'Mirandés'},
            'pnb':{'name':u'Western Panjabi', 'localname':u'پنجابی'},
            'pcd':{'name':u'Picard',          'localname':u'Picard'},
            'krc':{'name':u'Karachay-Balkar', 'localname':u'Къарачай-Малкъар'},
            'frr':{'name':u'North Frisian',   'localname':u'Nordfriisk'},
            'bjn':{'name':u'Banjar',          'localname':u'Bahasa Banjar'},
            'mrj':{'name':u'Hill Mari',       'localname':u'Кырык Мары (Kyryk Mary)'},
            'koi':{'name':u'Komi-Permyak',    'localname':u'Перем Коми (Perem Komi)'},
            'gag':{'name':u'Gagauz',           'localname':u'Gagauz'},
            'pfl':{'name':u'Palatinate German','localname':u'Pfälzisch'},
            'rue':{'name':u'Rusyn',            'localname':u'русиньскый язык'},
            'ltg':{'name':u'Latgalian',        'localname':u'Latgaļu volūda'},
            'kbd':{'name':u'Kabardian',        'localname':u'Aдыгэбзэ'},
            'xmf':{'name':u'Mingrelian',       'localname':u'მარგალური'},
            'nso':{'name':u'Northern Sotho',   'localname':u'Sesotho sa Leboa'},
            'vep':{'name':u'Veps',             'localname':u'Vepsän kel\''},
            'lez':{'name':u'Lezgi',            'localname':u'Лезги'},
            'min':{'name':u'Minangkabau',      'localname':u'Minangkabau'},
            'tyv':{'name':u'Tuva',             'localname':u'Тыва дыл'},            
            'hak':{'name':u'Hakka',            'localname':u'Hak-kâ-fa / 客家話'},
            'mai':{'name':u'Maithili',         'localname':u'मैथिली'}}
repo = pywikibot.Site("wikidata", "wikidata").data_repository()
phys = "List of articles every Wikipedia should have/Expanded/Physical sciences"
site = pywikibot.Site('meta', 'meta')
page = pywikibot.Page(site, phys)
page.exists()
phys_text = page.text
def GetInterwikiLength(article):
    
    #calculate len of all interwiki links
    interwiki_len   = 0
    interwiki_last  = 0
    interwiki_colon = 0
    interwiki_nl    = 0
    interwiki_first = article.find('[[', interwiki_last)
    while interwiki_first > -1:    
        interwiki_last  = article.find(']]', interwiki_first)
        interwiki_colon = article.find(':',  interwiki_first)
        if interwiki_colon > -1 and interwiki_colon < interwiki_last:
            curlang = article[interwiki_first+2:interwiki_colon]
            if curlang in lang_info:
                interwiki_nl = article.find('\n', interwiki_last)
                if interwiki_nl > -1:
                    interwiki_len += (interwiki_nl - interwiki_first) + 1
                else:
                    interwiki_len += (interwiki_last - interwiki_first) + 2
        interwiki_first = article.find('[[', interwiki_last)

    return interwiki_len


def GetCommentLength(article):

    #calculate len of all comments
    comment_len   = 0
    comment_last  = 0
    comment_first = article.find(u'<!--', comment_last)
    while comment_first > -1:    
        comment_last = article.find(u'-->', comment_first)
        if comment_last == -1:
            comment_last = comment_first + 4
            
        comment_len += (comment_last - comment_first) - 4
        comment_first = article.find(u'<!--', comment_last)

    return comment_len
def get_links(text, repo, langs = ('uk', 'ru', 'en')):
    p = re.compile('\[\[d:(.+)\|.+\]\]')
    links = p.findall(text)
    link_str = "[[:{}:{}|{}]]"
    rows = []
    for link in links:
        item = pywikibot.ItemPage(repo, link)
        item.get()
        wikidata_name = list(filter(lambda x: x != "", [item.labels.get(i + 'wiki', '') for i in langs]))
        articles = {lang: item.sitelinks.get(lang + 'wiki', "") for lang in langs}
        print(articles)
        
        uk_link = "[[{}]]".format(articles[langs[0]]) if articles[langs[0]] else link_str.format("d", link, 
                                                                                                 wikidata_name)
        out_links = [(link_str.format(langs[i], articles[langs[i]], langs[i]) if 
                      articles[langs[i]] else "0,0") for i in range(1, len(langs))]
        rows.append([uk_link] + out_links)
    return rows


def make_table(links_list):
    table = ''
    row_text = "|- style='background:#e0ffe0'\n"
    for links in links_list:
        table += row_text
        table += "|" + " || ".join(links) + "\n"
    return table
        

def partitionate(text, level):
    parts_re = re.compile('[^=]{0}([^=]*){0}[^=]'.format('=' * level))
    parts = re.findall(parts_re, text)
    if not len(parts):
        return text
    parts = [(text.find(i), i) for i in parts] + [(len(text)-1, "")]
    parts = {(i, parts[i][1]): text[parts[i][0] + len(parts[i][1]): parts[i + 1][0]] for i in range(len(parts) - 1)}
    for part in parts:
        parts[part] = partitionate(parts[part], level + 1)
    return parts
parts = partitionate(phys_text, 3)
# for key in sorted(parts.keys()):
#     print(key)
#     if not isinstance(parts[key], str):
#         for key2 in sorted(parts[key].keys()):
#             print("    " + str(key2))
print(parts[(0, ' Science, 10 ')])
science = make_table(get_links(parts[(0, ' Science, 10 ')], repo))
print(science)
===
{{Div col|cols=4}}
# '''[[d:Q336|Science]]'''
# [[d:Q101965|Experiment]]
# [[d:Q483242|Laboratory]]
## [[d:Q834028|Laboratory equipment]]
# '''[[d:Q7860|Nature]]'''
# [[d:Q193181|Observation]]
# [[d:Q42240|Research]]
# [[d:Q46857|Scientific method]]
# [[d:Q3239681|Scientific theory]]
# [[d:Q901|Scientist]]
{{Div col end}}

===
{'uk': 'Наука', 'ru': 'Наука', 'en': 'Science'}
{'uk': 'Експеримент', 'ru': 'Эксперимент', 'en': 'Experiment'}
{'uk': 'Лабораторія', 'ru': 'Лаборатория', 'en': 'Laboratory'}
{'uk': 'Лабораторне обладнання', 'ru': 'Лабораторное оборудование', 'en': ''}
{'uk': 'Природа', 'ru': 'Природа', 'en': 'Nature'}
{'uk': 'Спостереження', 'ru': 'Наблюдение (психология)', 'en': 'Observation'}
{'uk': 'Дослідження', 'ru': 'Исследование', 'en': 'Research'}
{'uk': 'Науковий метод', 'ru': 'Научный метод', 'en': 'Scientific method'}
{'uk': 'Наукова теорія', 'ru': 'Научная теория', 'en': 'Scientific theory'}
{'uk': 'Науковець', 'ru': 'Учёный', 'en': 'Scientist'}
|- style='background:#e0ffe0'
|[[Наука]] || [[:ru:Наука|ru]] || [[:en:Science|en]]
|- style='background:#e0ffe0'
|[[Експеримент]] || [[:ru:Эксперимент|ru]] || [[:en:Experiment|en]]
|- style='background:#e0ffe0'
|[[Лабораторія]] || [[:ru:Лаборатория|ru]] || [[:en:Laboratory|en]]
|- style='background:#e0ffe0'
|[[Лабораторне обладнання]] || [[:ru:Лабораторное оборудование|ru]] || 0,0
|- style='background:#e0ffe0'
|[[Природа]] || [[:ru:Природа|ru]] || [[:en:Nature|en]]
|- style='background:#e0ffe0'
|[[Спостереження]] || [[:ru:Наблюдение (психология)|ru]] || [[:en:Observation|en]]
|- style='background:#e0ffe0'
|[[Дослідження]] || [[:ru:Исследование|ru]] || [[:en:Research|en]]
|- style='background:#e0ffe0'
|[[Науковий метод]] || [[:ru:Научный метод|ru]] || [[:en:Scientific method|en]]
|- style='background:#e0ffe0'
|[[Наукова теорія]] || [[:ru:Научная теория|ru]] || [[:en:Scientific theory|en]]
|- style='background:#e0ffe0'
|[[Науковець]] || [[:ru:Учёный|ru]] || [[:en:Scientist|en]]

def get_articles(text, lang):
    p = re.compile('\[\[:{0}:(.+)\|{0}\]\]'.format(lang))
    articles = p.findall(text)
    art_str = "|".join(articles)
    wiki_site = pywikibot.Site(lang, 'wikipedia')
    pages = api.Request(action="query", 
                        prop="revisions", 
                        redirects=True, 
                        titles=art_str, 
                        rvprop="content", 
                        site=wiki_site)
    pages = pages.submit()['query']['pages']
    for k, v in pages.items():
        print(v['title'])
        print(v['title'] in articles)
        art = v['revisions'][0]['*']
        print(len(art) - GetInterwikiLength(art) - GetCommentLength(art)) 
        

get_articles(science, "en")
Research
True
45397
Experiment
True
30223
Scientific method
True
145735
Laboratory
True
10953
Observation
True
16247
Scientist
True
31266
Science
True
105050
Scientific theory
True
49590
Nature
True
68484
WARNING: /srv/paws/lib/python3.4/site-packages/ipykernel/__main__.py:11: DeprecationWarning: Instead of using kwargs from Request.__init__, parameters for the request to the API should be added via the "parameters" parameter.