|
|
|
|
|
|
|
|
|
|
|
|
|
|
''' Script to segment IMO shortlist md files using regex. It takes as input |
|
|
the file en-compendium.md in en-shortlist and outputs the segmentation |
|
|
(problem/solution pairs) in en-shortlist-seg |
|
|
To run: |
|
|
`python segment_compendium.py` |
|
|
To debug (or see covered use cases by regex): |
|
|
`pytest test_segment_compendium` |
|
|
''' |
|
|
|
|
|
import os |
|
|
import re |
|
|
import pandas as pd |
|
|
|
|
|
|
|
|
base = 'en-shortlist' |
|
|
seg_base = 'en-shortlist-seg' |
|
|
basename = 'en-compendium' |
|
|
|
|
|
|
|
|
level1_re = re.compile(r"^##\s+(Problems|Solutions|Notation and Abbreviations)$") |
|
|
year_re = re.compile(r"^[^=]*,\s+(\d{4})\s*$") |
|
|
problem_section_re = re.compile(r"^###\s+(\d+\.\d+\.\d+)\s+(.+)$") |
|
|
solution_section_re = re.compile(r"^###\s+(\d+\.\d+)\s+([\w\s]+)\s+(\d{4})$") |
|
|
problem_or_solution_re = re.compile(r"^(?:\[.*?\])?\s*(\d+)\s*\.\s*(.+)$") |
|
|
|
|
|
|
|
|
def add_content(current_dict): |
|
|
required_keys = ["year", "category", "section_label", "label", "lines"] |
|
|
if not all(current_dict[key] for key in required_keys): |
|
|
return |
|
|
text_str = " ".join(current_dict["lines"]).strip() |
|
|
entry = { |
|
|
"year": current_dict["year"], |
|
|
"category": current_dict["category"], |
|
|
"section": current_dict["section_label"], |
|
|
"label": current_dict["label"], |
|
|
} |
|
|
if current_dict["class"] == "problem": |
|
|
entry["problem"] = text_str |
|
|
current_dict["problems"].append(entry) |
|
|
elif current_dict["class"] == "solution": |
|
|
entry["solution"] = text_str |
|
|
current_dict["solutions"].append(entry) |
|
|
|
|
|
|
|
|
def get_category(s:str): |
|
|
cat = None |
|
|
if 'contest' in s.lower(): |
|
|
cat = 'contest' |
|
|
elif 'shortlisted' in s.lower(): |
|
|
cat = 'shortlisted' |
|
|
elif 'longlisted' in s.lower(): |
|
|
cat = 'longlisted' |
|
|
return cat |
|
|
|
|
|
|
|
|
def get_matching_section_label(s:str): |
|
|
""" |
|
|
extracts the section number to be used a a join key to pair a problem and solution |
|
|
for problems: 3.44.1 -> 44 |
|
|
for solutions: 4.20 -> 20 |
|
|
""" |
|
|
return s.split('.')[1] |
|
|
|
|
|
|
|
|
def parse(file): |
|
|
with open(file, 'r') as file: |
|
|
content = file.read() |
|
|
|
|
|
current = { |
|
|
"year": None, |
|
|
"category": None, |
|
|
"section_label": None, |
|
|
"label": None, |
|
|
"class": None, |
|
|
"lines": [], |
|
|
"problems": [], |
|
|
"solutions": [] |
|
|
} |
|
|
for line in content.splitlines(): |
|
|
if match := level1_re.match(line): |
|
|
add_content(current) |
|
|
title, = match.groups() |
|
|
current["class"] = { |
|
|
"Problems": "problem", |
|
|
"Solutions": "solution", |
|
|
}.get(title, "other") |
|
|
current["lines"] = [] |
|
|
elif match := year_re.match(line): |
|
|
add_content(current) |
|
|
current["year"] = match.group(1) |
|
|
current["lines"] = [] |
|
|
elif match := problem_section_re.match(line): |
|
|
add_content(current) |
|
|
number, title = match.groups() |
|
|
current["section_label"] = get_matching_section_label(number) |
|
|
current["category"] = get_category(title) |
|
|
current["lines"] = [] |
|
|
elif match := solution_section_re.match(line): |
|
|
add_content(current) |
|
|
number, title, year = match.groups() |
|
|
current["section_label"] = get_matching_section_label(number) |
|
|
current["category"] = get_category(title) |
|
|
current["year"] = year |
|
|
current["lines"] = [] |
|
|
elif match := problem_or_solution_re.match(line): |
|
|
add_content(current) |
|
|
current["label"] = match.group(1) |
|
|
current["lines"] = [line] |
|
|
else: |
|
|
if current["lines"]: |
|
|
current["lines"].append(line) |
|
|
problems_df = pd.DataFrame(current["problems"]) |
|
|
solutions_df = pd.DataFrame(current["solutions"]) |
|
|
return problems_df, solutions_df |
|
|
|
|
|
def join(problems_df, solutions_df): |
|
|
pairs_df = problems_df.merge(solutions_df, on=["year", "category", "section", "label"], how="outer") |
|
|
return pairs_df |
|
|
|
|
|
def add_metadata(pairs_df): |
|
|
problem_type_mapping = { |
|
|
"A": "Algebra", |
|
|
"C": "Combinatorics", |
|
|
"G": "Geometry", |
|
|
"N": "Number Theory" |
|
|
} |
|
|
pairs_df['problem_type'] = pairs_df['problem'].str.extract(r'^\d+\.\s*([ACGN])\d*')[0] |
|
|
pairs_df['problem_type'] = pairs_df['problem_type'] .map(problem_type_mapping) |
|
|
pairs_df['tier'] = 0 |
|
|
pairs_df.rename(columns={"category": "problem_phase"}, inplace=True) |
|
|
pairs_df = pairs_df.drop(columns=['section', 'label']) |
|
|
return pairs_df |
|
|
|
|
|
def write_pairs(filename, pairs_df): |
|
|
pairs_df.to_json(filename, orient="records", lines=True) |
|
|
|
|
|
|
|
|
problems, solutions = parse(f"{base}/{basename}.md") |
|
|
pairs_df = join(problems, solutions) |
|
|
pairs_df = pairs_df[pairs_df.notnull().all(axis=1)] |
|
|
pairs_df = add_metadata(pairs_df) |
|
|
write_pairs(f"{seg_base}/{basename}.jsonl", pairs_df) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|