from pathlib import Path import re import json from typing import List, Tuple from tqdm import tqdm project_root = Path(__file__).parent.parent.parent problem_tag = "Problem" solution_tag = "Solution" def analyze(text: str) -> Tuple[List, int]: """ Analyze the text and return the tags and problem number. Args: text (str): The markdown text to analyze. Returns: Tuple[List, int]: A tuple containing the tags and problem number. """ problem_pattern = re.compile(r"(?:(?:\n|# )Problem\s+(\d+)\.?|\n(\d+)\.)") solution_pattern = re.compile(r"(?:\n|# )(Alternative\s+)?Solution(?:\s+(\d+)|\.|:|\n|\s+I+\.)") tags = [] tags.extend([(x, problem_tag) for x in problem_pattern.finditer(text)]) problem_num = len(tags) tags.extend([(x, solution_tag) for x in solution_pattern.finditer(text)]) solution_num = len(tags) - problem_num tags.sort(key=lambda x: x[0].start()) return tags, problem_num, solution_num def segment(text: str, tags): starts = [] ends = [] for i in range(len(tags)): starts.append(tags[i][0].end()) if i + 1 < len(tags): ends.append(tags[i + 1][0].start()) else: ends.append(len(text)) return [text[start:end].strip() for start, end in zip(starts, ends)] def join(tags, segments, by_tag=False): problem, solution = "", "" problem_label, problem_match, solution_match = "", "", "" pairs = [] if by_tag: seen = {} for tag, segment in zip(tags, segments): match = tag[0].group(0) if match in seen: problem, problem_match = seen[match] pairs.append((problem, segment, tag[0].group(1), problem_match, match)) else: seen[match] = (segment, match) else: for tag, segment in zip(tags, segments): if tag[1] == problem_tag: problem = segment problem_match = tag[0].group(0) problem_label = tag[0].group(1) else: solution = segment solution_match = tag[0].group(0) pairs.append( (problem, solution, problem_label, problem_match, solution_match) ) return pairs def write_pairs(output_file: Path, pairs): year = re.search(r"(\d{4})", output_file.stem).group(1) output_jsonl_text = "" for problem, solution, problem_label, problem_match, solution_match in pairs: output_jsonl_text += ( json.dumps( { "year": year, "tier": "T3", "problem_label": problem_label, "problem_type": None, "exam": "Benelux_MO", "problem": problem, "solution": solution, "metadata": { "resource_path": output_file.relative_to( project_root ).as_posix(), "problem_match": problem_match, "solution_match": solution_match, }, }, ensure_ascii=False, ) + "\n" ) output_file.write_text(output_jsonl_text, encoding="utf-8") if __name__ == "__main__": compet_base_path = Path(__file__).resolve().parent.parent compet_md_path = compet_base_path / "md" seg_output_path = compet_base_path / "segmented" for md_path in tqdm(list(compet_md_path.glob("**/*.md")), desc="Segmenting"): output_file = seg_output_path / md_path.relative_to(compet_md_path).with_suffix( ".jsonl" ) output_file.parent.mkdir(parents=True, exist_ok=True) text = "\n" + md_path.read_text(encoding="utf-8") tags, problem_num, solution_num = analyze(text) if problem_num > 0: segments = segment(text, tags) pairs = join(tags, segments, by_tag=solution_num == 0) write_pairs(output_file, pairs)