|
|
|
|
|
|
|
|
|
|
|
|
|
|
''' Script to segment IMO shortlist md files using regex. It takes as input |
|
|
files in en-shortlist and outputs en-shortlist-seg |
|
|
To run: |
|
|
`python segment_script/segment.py` |
|
|
To debug (or see covered use cases listed in fixtures/): |
|
|
`pytest test_segment` |
|
|
''' |
|
|
|
|
|
from collections import defaultdict |
|
|
import os |
|
|
import re |
|
|
import pandas as pd |
|
|
import json |
|
|
|
|
|
|
|
|
base = 'md' |
|
|
seg_base = 'segmented' |
|
|
|
|
|
section_re = re.compile(r'##\s+([A-Za-z]\w.*)') |
|
|
problem_re = re.compile( |
|
|
r'^(?:##\s*)?((?:[AGNC]\s*\d+))\.*\s*(.*?)(?:\((.*?)\))?$', |
|
|
re.MULTILINE |
|
|
) |
|
|
solution_re = re.compile( |
|
|
r'^(?:##\s*)?(Solution(?: \d+)?\.)\s*(.*?)(?=(?:Solution|Comment|A\d+|G\d+|N\d+|C\d+|##|$))', |
|
|
re.MULTILINE | re.DOTALL |
|
|
) |
|
|
|
|
|
def add_content(section, label, text_class, text, problems, solutions): |
|
|
text_str = " ".join(text).strip() |
|
|
if text_class == "problem": |
|
|
|
|
|
problems.append({"section": section, "label": label, "problem": text_str}) |
|
|
elif text_class == "solution": |
|
|
|
|
|
solutions.append({"label": label, "solution": text_str}) |
|
|
|
|
|
def parse(file): |
|
|
with open(file, 'r') as file: |
|
|
content = file.read() |
|
|
problems, solutions = [], [] |
|
|
current_section, current_label, current_class = None, None, None |
|
|
current_lines = [] |
|
|
for line in content.splitlines(): |
|
|
if match := problem_re.match(line): |
|
|
label, text, country = match.groups() |
|
|
label = label.replace(" ", "") |
|
|
add_content(current_section, current_label, current_class, current_lines, problems, solutions) |
|
|
current_class = "problem" |
|
|
current_label = label |
|
|
current_lines = [text] |
|
|
elif match := solution_re.match(line): |
|
|
label, text = match.groups() |
|
|
add_content(current_section, current_label, current_class, current_lines, problems, solutions) |
|
|
current_class = "solution" |
|
|
current_lines = [text] |
|
|
elif match := section_re.match(line): |
|
|
add_content(current_section, current_label, current_class, current_lines, problems, solutions) |
|
|
current_class = "section" |
|
|
text, = match.groups() |
|
|
current_section = text |
|
|
else: |
|
|
current_lines.append(line) |
|
|
add_content(current_section, current_label, current_class, current_lines, problems, solutions) |
|
|
problems_df = pd.DataFrame(problems).drop_duplicates(subset=["label", "problem"]) |
|
|
solutions_df = pd.DataFrame(solutions) |
|
|
return problems_df, solutions_df |
|
|
|
|
|
def join(problems_df, solutions_df): |
|
|
pairs_df = problems_df.merge(solutions_df, on=["label"], how="left") |
|
|
return pairs_df |
|
|
|
|
|
def add_metadata(pairs_df): |
|
|
pairs_df.rename(columns={"section": "problem_type", "label": "problem_label"}, inplace=True) |
|
|
pairs_df['tier'] = 0 |
|
|
return pairs_df |
|
|
|
|
|
def write_pairs(filename, pairs_df): |
|
|
pairs_df.to_json(filename, orient="records", lines=True) |
|
|
|
|
|
|
|
|
os.makedirs(seg_base, exist_ok=True) |
|
|
for name in os.listdir(base): |
|
|
if "compendium" not in name: |
|
|
print(name) |
|
|
problems, solutions = parse(os.path.join(base, name)) |
|
|
pairs_df = join(problems, solutions) |
|
|
pairs_df = add_metadata(pairs_df) |
|
|
print(pairs_df) |
|
|
basename = os.path.splitext(name)[0] |
|
|
print(f"{seg_base}/{basename}.jsonl") |
|
|
write_pairs(f"{seg_base}/{basename}.jsonl", pairs_df) |