これは自然言語処理100本ノック2020に挑んだ、とある人間の記録。
他の問題はこちら → 自然言語処理100本ノック2020から逃げるな まとめ
28. MediaWikiマークアップの除去
Pythonコード
import NLP100_27 import re def get_template_no_mediawiki_markup(): dict = NLP100_27.get_template_no_emphasis_markup() dict = remove_file_markup(dict) dict = remove_template_lang(dict) dict = remove_ref(dict) dict = remove_external_link(dict) return dict def remove_file_markup(dict): for k, v in dict.items(): # ファイルの除去 p = re.compile(r''' \[\[ # [[からスタート ファイル: # ファイルの文字列 ([^|]*?) # (|を含まない文字列) の繰り返し (url) (\|[^|]*?)? # |と(|を含まない文字列)| の繰り返し (thumb) (\|[^|]*?)? # |を含まない文字列(非貪欲) (説明文) \]\] # ]]で終わり ''', re.MULTILINE + re.VERBOSE) dict[k] = p.sub(r'\3', v) return dict def remove_template_lang(dict): for k, v in dict.items(): p = re.compile(r''' \{\{ lang # langで始まる \|.*? # |文字列 (言語タグ) \|(.*?) # |文字列 \}\} ''', re.MULTILINE + re.VERBOSE) dict[k] = p.sub(r'\1', v) return dict def remove_ref(dict): for k, v in dict.items(): p = re.compile(r''' \< \/? # /が0か1回 [ref|br] # refかbrが出現 .*? # 文字列 \> # >で終わる ''', re.MULTILINE + re.VERBOSE) dict[k] = p.sub('', v) return dict def remove_external_link(dict): for k, v in dict.items(): # 外部リンクの除去 []ありの場合 p1 = re.compile(r''' (\[ # [からスタート(明示的な場合) http # httpから始まる .*? # 文字列 (\s(.*?))?a # 空白+文字列 (表示文字) \]) #]でおわり(明示的な場合) ''', re.MULTILINE + re.VERBOSE) dict[k] = p1.sub(r'\2', v) p2 = re.compile(r''' http.*?([\s\|]) # httpから始まって空白か|が来るまでの文字列 ''', re.MULTILINE + re.VERBOSE) dict[k] = p2.sub('', dict[k]) return dict if __name__ == "__main__": dict = get_template_no_mediawiki_markup() for k, v in dict.items(): print('{key}: {value}'.format(key=k, value=v))
27. 内部リンクの除去で作成したget_template_no_emphasis_markup関数を利用しています。
実行結果
略名: イギリス
日本語国名: グレートブリテン及び北アイルランド連合王国
公式国名: United Kingdom of Great Britain and Northern Ireland英語以外での正式国名:
*An Rìoghachd Aonaichte na Breatainn Mhòr agus Eirinn mu Thuath(スコットランド・ゲール語)
*Teyrnas Gyfunol Prydain Fawr a Gogledd Iwerddon(ウェールズ語)
*Ríocht Aontaithe na Breataine Móire agus Tuaisceart na hÉireann(アイルランド語)
*An Rywvaneth Unys a Vreten Veur hag Iwerdhon Glédh(コーンウォール語)
*Unitit Kinrick o Great Breetain an Northren Ireland(スコットランド語)
**Claught Kängrick o Docht Brätain an Norlin Airlann、Unitet Kängdom o Great Brittain an Norlin Airlann(アルスター・スコットランド語)
国旗画像: Flag of the United Kingdom.svg
国章画像: |イギリスの国章
国章リンク: (国章)
標語: Dieu et mon droit(フランス語:神と我が権利)
国歌: [[女王陛下万歳|God Save the Queen]]{{en icon}}神よ女王を護り賜え{{center|}}
地図画像: Europe-UK.svg
位置画像: United Kingdom (+overseas territories) in the World (+Antarctica claims).svg
公用語: 英語
首都: ロンドン(事実上)
最大都市: ロンドン
元首等肩書: 女王
元首等氏名: エリザベス2世
首相等肩書: 首相
首相等氏名: ボリス・ジョンソン
他元首等肩書1: 貴族院議長
他元首等氏名1: ノーマン・ファウラー
他元首等肩書2: 庶民院議長
他元首等氏名2: {{仮リンク|リンゼイ・ホイル|en|Lindsay Hoyle}}
他元首等肩書3: 最高裁判所長官
他元首等氏名3: ブレンダ・ヘイル
面積順位: 76
面積大きさ: 1 E11
面積値: 244,820
水面積率: 1.3%
人口統計年: 2018
人口順位: 22
人口大きさ: 1 E7
人口値: 6643万5600{{Cite web|url=title=Population estimates - Office for National Statistics|accessdate=2019-06-26|date=2019-06-26}}
人口密度値: 271
GDP統計年元: 2012
GDP値元: 1兆5478億[and Statistics>World Economic Outlook Databases>By Countrise>United Kingdom]
GDP統計年MER: 2012
GDP順位MER: 6
GDP値MER: 2兆4337億
GDP統計年: 2012
GDP順位: 6
GDP値: 2兆3162億
GDP/人: 36,727
建国形態: 建国
確立形態1: イングランド王国/スコットランド王国(両国とも1707年合同法まで)
確立年月日1: 927年/843年
確立形態2: グレートブリテン王国成立(1707年合同法)
確立年月日2: 1707年{{0}}5月{{0}}1日
確立形態3: グレートブリテン及びアイルランド連合王国成立(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使用は.ukに比べ圧倒的少数。
国際電話番号: 44
注記:
27. 内部リンクの除去とのdiff
3,9c3,9
< 公式国名: {{lang|en|United Kingdom of Great Britain and Northern Ireland}}<ref>英語以外での正式国名:<br />
< *{{lang|gd|An Rìoghachd Aonaichte na Breatainn Mhòr agus Eirinn mu Thuath}}(スコットランド・ゲール語)
< *{{lang|cy|Teyrnas Gyfunol Prydain Fawr a Gogledd Iwerddon}}(ウェールズ語)
< *{{lang|ga|Ríocht Aontaithe na Breataine Móire agus Tuaisceart na hÉireann}}(アイルランド語)
< *{{lang|kw|An Rywvaneth Unys a Vreten Veur hag Iwerdhon Glédh}}(コーンウォール語)
< *{{lang|sco|Unitit Kinrick o Great Breetain an Northren Ireland}}(スコットランド語)
< **{{lang|sco|Claught Kängrick o Docht Brätain an Norlin Airlann}}、{{lang|sco|Unitet Kängdom o Great Brittain an Norlin Airlann}}(アルスター・スコットランド語)</ref>
---
> 公式国名: United Kingdom of Great Britain and Northern Ireland英語以外での正式国名:
> *An Rìoghachd Aonaichte na Breatainn Mhòr agus Eirinn mu Thuath(スコットランド・ゲール語)
> *Teyrnas Gyfunol Prydain Fawr a Gogledd Iwerddon(ウェールズ語)
> *Ríocht Aontaithe na Breataine Móire agus Tuaisceart na hÉireann(アイルランド語)
> *An Rywvaneth Unys a Vreten Veur hag Iwerdhon Glédh(コーンウォール語)
> *Unitit Kinrick o Great Breetain an Northren Ireland(スコットランド語)
> **Claught Kängrick o Docht Brätain an Norlin Airlann、Unitet Kängdom o Great Brittain an Norlin Airlann(アルスター・スコットランド語)
11c11
< 国章画像: [[ファイル:Royal Coat of Arms of the United Kingdom.svg|85px|イギリスの国章]]
---
> 国章画像: |イギリスの国章
13,14c13,14
< 標語: {{lang|fr|Dieu et mon droit}}<br />(フランス語:神と我が権利)
< 国歌: [[女王陛下万歳|{{lang|en|God Save the Queen}}]]{{en icon}}<br />神よ女王を護り賜え<br />{{center|[[ファイル:United States Navy Band - God Save the Queen.ogg]]}}
---
> 標語: Dieu et mon droit(フランス語:神と我が権利)
> 国歌: [[女王陛下万歳|God Save the Queen]]{{en icon}}神よ女王を護り賜え{{center|}}
37c37
< 人口値: 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>
---
> 人口値: 6643万5600{{Cite web|url=title=Population estimates - Office for National Statistics|accessdate=2019-06-26|date=2019-06-26}}
40c40
< 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値元: 1兆5478億[and Statistics>World Economic Outlook Databases>By Countrise>United Kingdom]
43c43
< GDP値MER: 2兆4337億<ref name="imf-statistics-gdp" />
---
> GDP値MER: 2兆4337億
46,47c46,47
< GDP値: 2兆3162億<ref name="imf-statistics-gdp" />
< GDP/人: 36,727<ref name="imf-statistics-gdp" />
---
> GDP値: 2兆3162億
> GDP/人: 36,727
49c49
< 確立形態1: イングランド王国/スコットランド王国<br />(両国とも1707年合同法まで)
---
> 確立形態1: イングランド王国/スコットランド王国(両国とも1707年合同法まで)
51c51
< 確立形態2: グレートブリテン王国成立<br />(1707年合同法)
---
> 確立形態2: グレートブリテン王国成立(1707年合同法)
53c53
< 確立形態3: グレートブリテン及びアイルランド連合王国成立<br />(1800年合同法)
---
> 確立形態3: グレートブリテン及びアイルランド連合王国成立(1800年合同法)
62c62
< ccTLD: .uk / .gb<ref>使用は.ukに比べ圧倒的少数。</ref>
---
> ccTLD: .uk / .gb使用は.ukに比べ圧倒的少数。
64c64
< 注記: <references/>
---
> 注記: