[PYTHON] 100 language processing knocks (2020): 23

"""
23.Section structure
Section names and their levels contained in the article (eg ""==Section name==If ", display 1).
"""

import json
import re


def get_uk_text(path):
    with open(path) as f:
        for line in f:
            line_data = json.loads(line)
            if line_data["title"] == "England":
                data = line_data
                break
    return data["text"]


uk_text = get_uk_text("jawiki-country.json")
# See uk_text.txt


# ans23
def get_section(string: str) -> list:
    """
    https://docs.python.org/3/library/re.html#regular-expression-syntax

    - re.VERBOSE  allow us add command to explain the regular expression
    - re.S        allow to recognize '\n'
    - (...)       matches whatever regular expression is inside the parentheses,
    - (?:...)     a non-capturing version of regular parentheses.
    - ?           causes the resulting RE to match 0 or 1 repetitions
    - *?          the '*' qualifier is greedy.
                  Adding ? after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched.
                  e.g. <.*> is matched against '<a> b <c>'
                  e.g. <.*?> will match only '<a>'
    Input:
        - ===Major cities===
    Return:
        - [('==', 'footnote'), ...]
    """
    pattern = re.compile(
        r"""
            ^     #Beginning of line
            (={2,}) #Capture target, 2 or more'='
            \s*     #0 or more extra whitespace ('philosophy'Or'marriage'Removed because there are extra spaces before and after
            (.+?)   #Capture target, one or more arbitrary characters, non-greedy (preventing entrainment of subsequent conditions)
            \s*     #Extra 0 or more whitespace
            \1      #Back reference, same content as the first capture target
            .*      #Any character is 0 or more
            $       #End of line
            """,
        re.MULTILINE | re.VERBOSE,
    )
    result = re.findall(pattern, string)
    return result


def get_level(sections: list):
    result = []
    for level, value in sections:
        level = len(level) - 1
        result.append((level, value))
    return result


sections = get_section(uk_text)  # [('==', 'footnote'), ...]
result = get_level(sections)  # [(1, 'footnote'), ...]

for level, value in result:
    print(" " * 2 * (level - 1) + value)
#Country name
#history
#Geography
#Major cities
#climate
#Politics
#Head of state
#Law
#Domestic affairs
#Local administrative division
#Diplomacy / military

Recommended Posts

100 language processing knocks 03 ~ 05
100 language processing knocks (2020): 40
100 language processing knocks (2020): 32
100 language processing knocks (2020): 35
100 language processing knocks (2020): 47
100 language processing knocks (2020): 39
100 language processing knocks (2020): 22
100 language processing knocks (2020): 26
100 language processing knocks (2020): 34
100 language processing knocks (2020): 29
100 language processing knocks (2020): 49
100 language processing knocks 06 ~ 09
100 language processing knocks (2020): 43
100 language processing knocks (2020): 24
100 language processing knocks (2020): 45
100 language processing knocks (2020): 10-19
100 language processing knocks (2020): 30
100 language processing knocks (2020): 00-09
100 language processing knocks (2020): 31
100 language processing knocks (2020): 48
100 language processing knocks (2020): 41
100 language processing knocks (2020): 37
100 language processing knocks (2020): 25
100 language processing knocks (2020): 23
100 language processing knocks (2020): 33
100 language processing knocks (2020): 20
100 language processing knocks (2020): 27
100 language processing knocks (2020): 46
100 language processing knocks (2020): 21
100 language processing knocks (2020): 36
100 amateur language processing knocks: 41
100 amateur language processing knocks: 71
100 amateur language processing knocks: 56
100 amateur language processing knocks: 24
100 amateur language processing knocks: 50
100 amateur language processing knocks: 59
100 amateur language processing knocks: 70
100 amateur language processing knocks: 62
100 amateur language processing knocks: 60
100 amateur language processing knocks: 92
100 amateur language processing knocks: 30
100 amateur language processing knocks: 84
100 amateur language processing knocks: 33
100 amateur language processing knocks: 46
100 amateur language processing knocks: 88
100 amateur language processing knocks: 89
100 amateur language processing knocks: 40
100 amateur language processing knocks: 45
100 amateur language processing knocks: 43
100 amateur language processing knocks: 55
100 amateur language processing knocks: 22
100 amateur language processing knocks: 61
100 amateur language processing knocks: 04
100 amateur language processing knocks: 63
100 amateur language processing knocks: 78
100 amateur language processing knocks: 12
100 amateur language processing knocks: 14
100 amateur language processing knocks: 08
100 amateur language processing knocks: 42
100 language processing knocks ~ Chapter 1
100 amateur language processing knocks: 19