"""
22.Extraction of category name
Extract the article category names (by name, not line by line).
"""
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")
uk_text_list = uk_text.split("\n")
ans = [x for x in uk_text_list if "Category:" in x[:11]]
# ans:
# [[Category:Commonwealth of Nations]]
# [[Category:Commonwealth Kingdom|*]]
# [[Category:G8 member countries]]
# [[Category:European Union member states|Former]]
# [[Category:Maritime nation]]
# [[Category:Existing sovereign country]]
# [[Category:Island country]]
# [[Category:A nation / territory established in 1801]]
# ans22
def extract_category_value(string: str) -> str:
"""
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: [[Category:England|*]]
Output: 'England'
"""
pattern = re.compile(
r"""
^ #Beginning of line
.* #Any character 0 or more
\[\[Category:
( #Start the group to be captured
.*? #Any character 0 or more, non-greedy match (when greedy, the latter half'|'Involves decorations that start with)
) #Group end
(?: #Start a group that is not captured
\|.* # '|'0 or more characters following
)? #Group end, 0 or 1 appearance
\]\]
.* #Any character 0 or more
$ #End of line
""",
re.VERBOSE | re.S,
)
result = re.findall(pattern, string)[0]
return result
category_values = [extract_category_value(s) for s in ans]
print(category_values)
# ['England',
# 'Commonwealth of Nations',
# 'Commonwealth Kingdom',
# 'G8 member countries',
# 'European Union member states',
# 'Maritime nation',
# 'Existing sovereign country',
# 'Island country',
# 'A nation / territory established in 1801']
Recommended Posts