import re import json from tqdm import tqdm from loguru import logger from pathlib import Path from typing import Tuple, List project_root = Path(__file__).parent.parent.parent problem_tag = 'Problem' solution_tag = 'Solution' def clean(text: str) -> str: text = text.replace('28. март 2015.', '') text = text.replace('31. март 2018 .', '') text = text.replace('6. април 2019 .', '') return text 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. """ tag_pattern = re.compile(r'\n(\d+)\.\s+', re.IGNORECASE) problem_start_pattern = re.compile(r'## Први дан', re.IGNORECASE) problem_start_position = problem_start_pattern.search(text).start() separator_pattern = re.compile(r'## РЕШЕЊА', re.IGNORECASE) separator_position = separator_pattern.search(text).start() tags = [] tags.extend([(x, problem_tag) for x in tag_pattern.finditer(text, problem_start_position, separator_position)]) problem_num = len(tags) tags.extend([(x, solution_tag) for x in tag_pattern.finditer(text, separator_position)]) 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): problem, solution = '', '' problem_label, problem_match, solution_match = '', '', '' pairs = [] problems_tags = [ (tag, segment) for tag, segment in zip(tags, segments) if tag[1] == 'Problem' ] solution_tags = { tag[0].group(1): (tag, segment) for tag, segment in zip(tags, segments) if tag[1] == 'Solution' } for problem_tag, problem in problems_tags: problem_label = problem_tag[0].group(1) problem_match = problem_tag[0].group(0) solution_tag, solution = solution_tags[problem_label] solution_match = solution_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': "T2", 'problem_label': problem_label, 'problem_type': None, 'exam': 'Serbia_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") def main(): compet_base_path = Path(__file__).resolve().parent.parent compet_md_path = compet_base_path / "md" seg_output_path = compet_base_path / "segmented" total_problem_count = 0 total_solution_count = 0 for smo_md in tqdm(list(compet_md_path.glob('**/*.md')), desc='Segmenting'): output_file = seg_output_path / smo_md.relative_to(compet_md_path).with_suffix('.jsonl') output_file.parent.mkdir(parents=True, exist_ok=True) text = '\n' + clean(smo_md.read_text(encoding="utf-8")) tags, problem_num, solution_num = analyze(text) segments = segment(text, tags) pairs = join(tags, segments) if pairs and problem_num > 0: write_pairs(output_file, pairs) total_problem_count += problem_num total_solution_count += len(pairs) else: logger.warning(f"No problem found in {smo_md}") logger.info(f"Total problem count: {total_problem_count}") logger.info(f"Total solution count: {total_solution_count}") if __name__ == '__main__': main()