Buckets:
| import os | |
| import json | |
| from typing import List, Dict, Tuple, Any, Optional | |
| from ..core.geometry import OlympiadProblem, GeometricPoint, GeometricTriangle | |
| class GeometryDataLoader: | |
| def __init__(self, data_dir: str = "data/"): | |
| self.data_dir = data_dir | |
| self.problems = [] | |
| def load_problems_from_directory(self, subdir: str = "") -> List[OlympiadProblem]: | |
| full_path = os.path.join(self.data_dir, subdir) | |
| if not os.path.exists(full_path): | |
| print(f"Directory {full_path} does not exist. Creating sample problems.") | |
| return self._create_sample_problems() | |
| problems = [] | |
| for filename in os.listdir(full_path): | |
| if filename.endswith(".json"): | |
| filepath = os.path.join(full_path, filename) | |
| try: | |
| problem = self._load_problem_from_json(filepath) | |
| if problem: | |
| problems.append(problem) | |
| except Exception as e: | |
| print(f"Error loading {filename}: {e}") | |
| return problems | |
| def _load_problem_from_json(self, filepath: str) -> Optional[OlympiadProblem]: | |
| with open(filepath, "r") as f: | |
| data = json.load(f) | |
| problem = OlympiadProblem( | |
| name=data.get("name", "Unknown Problem"), | |
| statement=data.get("statement", ""), | |
| ) | |
| for point_data in data.get("points", []): | |
| point = GeometricPoint( | |
| name=point_data["name"], | |
| x=point_data.get("x"), | |
| y=point_data.get("y"), | |
| properties=point_data.get("properties", {}), | |
| ) | |
| problem.add_point(point) | |
| for triangle_data in data.get("triangles", []): | |
| vertex_names = triangle_data["vertices"] | |
| vertices = [ | |
| problem.points[name] for name in vertex_names if name in problem.points | |
| ] | |
| if len(vertices) == 3: | |
| triangle = GeometricTriangle( | |
| name=triangle_data["name"], | |
| vertices=vertices, | |
| properties=triangle_data.get("properties", {}), | |
| ) | |
| problem.add_triangle(triangle) | |
| for constraint in data.get("constraints", []): | |
| problem.add_constraint(constraint) | |
| if "goal" in data: | |
| problem.set_goal(data["goal"]) | |
| return problem | |
| def _create_sample_problems(self) -> List[OlympiadProblem]: | |
| problems = [] | |
| problem1 = OlympiadProblem( | |
| "Triangle Angle Sum", | |
| "In triangle ABC, prove that the sum of angles A, B, and C equals 180 degrees.", | |
| ) | |
| A = GeometricPoint("A", 0, 0) | |
| B = GeometricPoint("B", 4, 0) | |
| C = GeometricPoint("C", 2, 3) | |
| problem1.add_point(A) | |
| problem1.add_point(B) | |
| problem1.add_point(C) | |
| triangle = GeometricTriangle("ABC", [A, B, C]) | |
| problem1.add_triangle(triangle) | |
| problem1.add_constraint("A, B, C are vertices of triangle ABC") | |
| problem1.set_goal("Prove that ∠A + ∠B + ∠C = 180°") | |
| problems.append(problem1) | |
| problem2 = OlympiadProblem( | |
| "Pythagorean Theorem", | |
| "In right triangle ABC with right angle at C, prove that AB² = AC² + BC².", | |
| ) | |
| A = GeometricPoint("A", 0, 0) | |
| B = GeometricPoint("B", 4, 0) | |
| C = GeometricPoint("C", 0, 3) | |
| problem2.add_point(A) | |
| problem2.add_point(B) | |
| problem2.add_point(C) | |
| triangle = GeometricTriangle("ABC", [A, B, C]) | |
| triangle.properties["right_angle"] = True | |
| triangle.properties["right_angle_vertex"] = "C" | |
| problem2.add_triangle(triangle) | |
| problem2.add_constraint("Angle ACB = 90°") | |
| problem2.set_goal("Prove AB² = AC² + BC²") | |
| problems.append(problem2) | |
| return problems | |
| def save_problems_to_directory( | |
| self, problems: List[OlympiadProblem], subdir: str = "" | |
| ): | |
| full_path = os.path.join(self.data_dir, subdir) | |
| os.makedirs(full_path, exist_ok=True) | |
| for i, problem in enumerate(problems): | |
| filename = f"problem_{i+1:03d}.json" | |
| filepath = os.path.join(full_path, filename) | |
| self._save_problem_to_json(problem, filepath) | |
| def _save_problem_to_json(self, problem: OlympiadProblem, filepath: str): | |
| data = { | |
| "name": problem.name, | |
| "statement": problem.statement, | |
| "goal": problem.goal, | |
| "constraints": problem.constraints, | |
| "points": [], | |
| "triangles": [], | |
| } | |
| for point in problem.points.values(): | |
| point_data = { | |
| "name": point.name, | |
| "x": point.x, | |
| "y": point.y, | |
| "properties": point.properties, | |
| } | |
| data["points"].append(point_data) | |
| for triangle in problem.triangles.values(): | |
| triangle_data = { | |
| "name": triangle.name, | |
| "vertices": [v.name for v in triangle.vertices], | |
| "properties": triangle.properties, | |
| } | |
| data["triangles"].append(triangle_data) | |
| with open(filepath, "w") as f: | |
| json.dump(data, f, indent=2) | |
Xet Storage Details
- Size:
- 5.35 kB
- Xet hash:
- 4de3186d309a8fa07c9b865fc18c80efe1240e68c5acbfc19abcbc6580e8ced3
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.