0
0

More than 3 years have passed since last update.

言語処理100本ノック 2020をやってみる 第三章:正規表現 後編

Posted at

目的

下記のウェブサイトの言語処理100本ノックを行う.

第一章

第三章 前編

第3章:正規表現

プログラミング言語はPythonを用いる.
jupyter notebookを使用しているので,出力にprint文が必要な場合は適宜追加する.

25. テンプレートの抽出

テンプレートは,タイトル名称と内容が'='で結ばれており,'|'で区切られている.
その正規表現を作り'='の前後をタプル形式で出力.
辞書型に変更するためOrderedDict関数を使用.

from collections import OrderedDict
OrderedDict(re.findall(r'\|(.+?)\s=\s*(.+)', text[0]))
>>>OrderedDict([('略名 ', 'イギリス'),
             ('日本語国名', 'グレートブリテン及び北アイルランド連合王国'),
             ('公式国名',
              '{{lang|en|United Kingdom of Great Britain and Northern Ireland}}<ref>英語以外での正式国名:<br />'),
             ('国旗画像', 'Flag of the United Kingdom.svg'),
             ('国章画像',
              '[[ファイル:Royal Coat of Arms of the United Kingdom.svg|85px|イギリスの国章]]'),
             ('国章リンク', '([[イギリスの国章|国章]])'),
             ('標語',
              '{{lang|fr|[[Dieu et mon droit]]}}<br />([[フランス語]]:[[Dieu et mon droit|神と我が権利]])'),
             ('国歌',
              "[[女王陛下万歳|{{lang|en|God Save the Queen}}]]{{en icon}}<br />''神よ女王を護り賜え''<br />{{center|[[ファイル:United States Navy Band - God Save the Queen.ogg]]}}"),
             ('地図画像', 'Europe-UK.svg'),
             ('位置画像',
              'United Kingdom (+overseas territories) in the World (+Antarctica claims).svg'),
             ('公用語', '[[英語]]'),
             ('首都', '[[ロンドン]](事実上)'),
             ('最大都市', 'ロンドン'),
             ('元首等肩書', '[[イギリスの君主|女王]]'),
             ('元首等氏名', '[[エリザベス2世]]'),
             ('首相等肩書', '[[イギリスの首相|首相]]'),
             ('首相等氏名', '[[ボリス・ジョンソン]]'),
             ('他元首等肩書1', '[[貴族院 (イギリス)|貴族院議長]]'),
             ('他元首等氏名1', '[[:en:Norman Fowler, Baron Fowler|ノーマン・ファウラー]]'),
             ('他元首等肩書2', '[[庶民院 (イギリス)|庶民院議長]]'),
             ('他元首等氏名2', '{{仮リンク|リンゼイ・ホイル|en|Lindsay Hoyle}}'),
             ('他元首等肩書3', '[[連合王国最高裁判所|最高裁判所長官]]'),
             ('他元首等氏名3',
              '[[:en:Brenda Hale, Baroness Hale of Richmond|ブレンダ・ヘイル]]'),
             ('面積順位', '76'),
             ('面積大きさ', '1 E11'),
             ('面積値', '244,820'),
             ('水面積率', '1.3%'),
             ('人口統計年', '2018'),
             ('人口順位', '22'),
             ('人口大きさ', '1 E7'),
             ('人口値',
              '6643万5600<ref>{{Cite web|url=https://www.ons.gov.uk/peoplepopulationandcommunity/populationandmigration/populationestimates|title=Population estimates - Office for National Statistics|accessdate=2019-06-26|date=2019-06-26}}</ref>'),
             ('人口密度値', '271'),
             ('GDP統計年元', '2012'),
             ('GDP値元',
              '1兆5478億<ref name="imf-statistics-gdp">[http://www.imf.org/external/pubs/ft/weo/2012/02/weodata/weorept.aspx?pr.x=70&pr.y=13&sy=2010&ey=2012&scsm=1&ssd=1&sort=country&ds=.&br=1&c=112&s=NGDP%2CNGDPD%2CPPPGDP%2CPPPPC&grp=0&a=IMF>Data and Statistics>World Economic Outlook Databases>By Countrise>United Kingdom]</ref>'),
             ('GDP統計年MER', '2012'),
             ('GDP順位MER', '6'),
             ('GDP値MER', '2兆4337億<ref name="imf-statistics-gdp" />'),
             ('GDP統計年', '2012'),
             ('GDP順位', '6'),
             ('GDP値', '2兆3162億<ref name="imf-statistics-gdp" />'),
             ('GDP/人', '36,727<ref name="imf-statistics-gdp" />'),
             ('建国形態', '建国'),
             ('確立形態1',
              '[[イングランド王国]]/[[スコットランド王国]]<br />(両国とも[[合同法 (1707年)|1707年合同法]]まで)'),
             ('確立年月日1', '927年/843年'),
             ('確立形態2', '[[グレートブリテン王国]]成立<br />(1707年合同法)'),
             ('確立年月日2', '1707年{{0}}5月{{0}}1日'),
             ('確立形態3',
              '[[グレートブリテン及びアイルランド連合王国]]成立<br />([[合同法 (1800年)|1800年合同法]])'),
             ('確立年月日3', '1801年{{0}}1月{{0}}1日'),
             ('確立形態4', "現在の国号「'''グレートブリテン及び北アイルランド連合王国'''」に変更"),
             ('確立年月日4', '1927年{{0}}4月12日'),
             ('通貨', '[[スターリング・ポンド|UKポンド]] (£)'),
             ('通貨コード', 'GBP'),
             ('時間帯', '±0'),
             ('夏時間', '+1'),
             ('ISO 3166-1', 'GB / GBR'),
             ('ccTLD', '[[.uk]] / [[.gb]]<ref>使用は.ukに比べ圧倒的少数。</ref>'),
             ('国際電話番号', '44'),
             ('注記', '<references/>')])

26. 強調マークアップの除去

テンプレートの抽出のコードを改良し,強調表現の"'"が1回以上出現する部分を除去する.

OrderedDict([(x[0], re.sub(r"'+", '', x[1])) for x in re.findall(r'\|(.+?)\s=\s*(.+)', text[0])])

27. 内部リンクの除去

強調マークアップの除去を改良し,内部リンクを除去.
"["または"]"が出現する箇所を除去する.

OrderedDict([(x[0], re.sub(r"('+|\[+|\]+)", '', x[1])) for x in re.findall(r'\|(.+?)\s=\s*(.+)', text[0])])

28. MediaWikiマークアップの除去

内部リンクの除去を改良し,リンクが出現する部分も除去する.

OrderedDict([(x[0], re.sub(r"('+|\[+|\]+)|<.*>", '', x[1])) for x in re.findall(r'\|(.+?)\s=\s*(.+)', text[0])])

29. 国旗画像のURLを取得するPermalink

MediaWikiマークアップの除去の結果から,"国旗画像"を抽出.
MediaWiki APIを使用し,titleに国旗画像の情報を設定し,画像のURLを含む情報を取得.
imageinfoからurlを抽出.

import requests
URL = "https://en.wikipedia.org/w/api.php"
req = requests.Session()
titles = "File:" + OrderedDict([(x[0], re.sub(r"('+|\[+|\]+)|<.*>", '', x[1])) for x in re.findall(r'\|(.+?)\s=\s*(.+)', text[0])])["国旗画像"]
PARAMS = {"action":"query","format":"json","prop":"imageinfo","titles":titles,"iiprop":"url"}
r = req.get(url=URL, params=PARAMS)
[x["imageinfo"][0]["url"] for x in r.json()["query"]["pages"].values()]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0