Ejemplo n.º 1
0
'''
27. 内部リンクの除去
26の処理に加えて,テンプレートの値からMediaWikiの内部リンクマークアップを除去し,テキストに変換せよ(参考: マークアップ早見表).
'''
from chr3 import ARTICLE_UK, re
regex1 = re.compile(r'^\|(.+?)\s=\s(.+?)$', re.MULTILINE)
regex2 = re.compile(r'\'{3}')
regex3 = re.compile(r'\[\[(.*?)\]\]')
uk_dict = {}
for i in regex1.findall(ARTICLE_UK()):
    prepare = regex2.sub("", i[1])
    prepare = regex3.sub(r'\1', prepare)
    uk_dict[i[0]] = prepare
print(uk_dict)
Ejemplo n.º 2
0
'''
26. 強調マークアップの除去
25の処理時に,テンプレートの値からMediaWikiの強調マークアップ(弱い強調,強調,強い強調のすべて)を除去してテキストに変換せよ(参考: マークアップ早見表).
'''
from chr3 import ARTICLE_UK, re
regex1 = re.compile(r'^\|(.+?)\s=\s(.+?)$', re.MULTILINE)
regex2 = re.compile(r'\'{3}')
uk_dict = {}
for i in regex1.findall(ARTICLE_UK()):
    prepare = regex2.sub("", i[1])
    uk_dict[i[0]] = prepare
print(uk_dict)
Ejemplo n.º 3
0
'''
22. カテゴリ名の抽出
記事のカテゴリ名を(行単位ではなく名前で)抽出せよ.
'''
from chr3 import ARTICLE_UK, re
article_UK = ARTICLE_UK().split('\n')
regex = re.compile(r'\[\[Category:(.+?)(\]\]|\|\*\]\])')
for lines in article_UK:
    result = regex.search(lines)
    if result:
        print(result.group(1))

#どっちでもできる
regex = re.compile(r'^\[\[Category:(.+?)(\]\]|\|\*\]\])$', re.MULTILINE)
for lines in regex.findall(ARTICLE_UK()):
    print(lines[0])
Ejemplo n.º 4
0
'''
28. MediaWikiマークアップの除去
27の処理に加えて,テンプレートの値からMediaWikiマークアップを可能な限り除去し,国の基本情報を整形せよ.
'''
from chr3 import ARTICLE_UK,re
regex1 = re.compile(r'^\|(.+?)\s=\s(.+?)$',re.MULTILINE)
regex2 = re.compile(r'\'{3}')
regex3 = re.compile(r'\[\[(.*?)\]\]')
regex4 = re.compile(r'\{\{lang\|en\|(.*?)\}\}')
regex5 = re.compile(r'<\/?(br|ref|references)(.*?)>')
uk_dict  = {}
for i in regex1.findall(ARTICLE_UK()):
    prepare = regex2.sub('', i[1])
    prepare = regex3.sub(r'\1', prepare)
    prepare = regex4.sub(r'\1', prepare)
    prepare = regex5.sub('', prepare)
    uk_dict[i[0]] = prepare
print(uk_dict)
Ejemplo n.º 5
0
'''
25. テンプレートの抽出
記事中に含まれる「基礎情報」テンプレートのフィールド名と値を抽出し,辞書オブジェクトとして格納せよ.
'''
from chr3 import ARTICLE_UK, re

regex = re.compile(r'^\|(.+?)\s=\s(.+?)$', re.MULTILINE)
print({i[0]: i[1] for i in regex.findall(ARTICLE_UK())})
Ejemplo n.º 6
0
'''
21. カテゴリ名を含む行を抽出
記事中でカテゴリ名を宣言している行を抽出せよ.
'''
from chr3 import ARTICLE_UK, re
article_UK = ARTICLE_UK().split('\n')
for lines in article_UK:
    if 'Category' in lines:
        print(lines)

#行だから上で良さそうだけど[]無しも一応。
regex = re.compile(r'\[\[(Category.*?)(\||])')
for lines in article_UK:
    result = regex.search(lines)
    if result:
        print(result.group(1))
Ejemplo n.º 7
0
'''
23. セクション構造
記事中に含まれるセクション名とそのレベル(例えば"== セクション名 =="なら1)を表示せよ.
'''
from chr3 import ARTICLE_UK,re
article_UK = ARTICLE_UK().split('\n')
regex = re.compile(r'(={2,})(.*?)(={2,})')
for lines in article_UK:
    result = regex.search(lines)
    if result:
        print(result.group(2).lstrip(),len(result.group(1)) - 1)

#どっちでもできる
regex = re.compile(r'(={2,})(.*?)(={2,})',re.MULTILINE)
for lines in regex.findall(ARTICLE_UK()):
    print(lines[1].lstrip(),len(lines[0])-1)
Ejemplo n.º 8
0
'''
24. ファイル参照の抽出
記事から参照されているメディアファイルをすべて抜き出せ
'''
from chr3 import ARTICLE_UK, re
article_UK = ARTICLE_UK().split('\n')
regex = re.compile(r'\[\[(File|ファイル):(.*?)\|')
for lines in article_UK:
    result = regex.search(lines)
    if result:
        print(result.group(2))

#どっちでもできる
regex = re.compile(r'\[\[(File|ファイル):(.*?)\|', re.MULTILINE)
for lines in regex.findall(ARTICLE_UK()):
    print(lines[1])