-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRead_in_Relations.py
More file actions
133 lines (106 loc) · 3.61 KB
/
Read_in_Relations.py
File metadata and controls
133 lines (106 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Read_in_Relations.py - Parse formalized geometric problems from text files
This module parses text files containing geometric predicates separated by semicolons,
with goals marked by question marks.
"""
from typing import List, Tuple, Dict
from relations import predicate, geometric
pred_dict = {
"col": predicate.Col,
"para": predicate.Para,
"cong": predicate.Cong,
"perp": predicate.Perp,
"midp": predicate.Midp,
"cyclic": predicate.Cyclic,
"eqangle": predicate.Eqangle,
"simtri1": predicate.Simtri1,
"simtri2": predicate.Simtri2,
"contri1": predicate.Contri1,
"contri2": predicate.Contri2,
"sameclock": predicate.Sameclock,
"eqratio": predicate.Eqratio,
"circle": predicate.Circle,
"tchord": predicate.Tchord
}
def parse_relations_file(filename: str, points_dict: Dict[str, geometric.Point]) -> Tuple[List, List]:
"""
Parse a text file containing formalized geometric predicates.
Args:
filename: Path to the text file
points_dict: Dictionary of points from the problem
Returns:
Tuple of (assumptions, goals) lists
"""
with open(filename, 'r') as file:
content = file.read().strip()
# Split by semicolons and clean up
statements = [s.strip() for s in content.split(';') if s.strip()]
assumptions = []
goals = []
for statement in statements:
if statement.startswith('?'):
# This is a goal
predicate_text = statement[1:].strip()
goal = _parse_predicate(predicate_text, points_dict)
if goal:
goals.append(goal)
else:
# This is an assumption
assumption = _parse_predicate(statement, points_dict)
if assumption:
assumptions.append(assumption)
return assumptions, goals
def _parse_predicate(predicate_text: str, points_dict: Dict[str, geometric.Point]):
"""
Parse a single predicate from text.
Args:
predicate_text: Text representation of predicate (e.g., "para A B C D")
points_dict: Dictionary of available points
Returns:
Predicate instance or None if parsing fails
"""
parts = predicate_text.strip().split()
if not parts:
return None
pred_type = parts[0].lower()
point_names = parts[1:]
# Get point objects
try:
points = [points_dict[name] for name in point_names]
except KeyError as e:
print(f"Warning: Point {e} not found in points dictionary")
return None
# Create appropriate predicate
if pred_type in pred_dict:
pred_class = pred_dict[pred_type]
return pred_class(*points)
else:
print(f"Warning: Unknown or invalid predicate: {predicate_text}")
return None
def create_sample_relations_file(filename: str):
"""
Create a sample relations file for testing.
Args:
filename: Path where to create the sample file
"""
sample_content = """para A B C D;
para B C D A;
col A E C;
col B E D;
? cong A E E C"""
with open(filename, 'w') as file:
file.write(sample_content)
print(f"Sample relations file created: {filename}")
def create_sample_relations_file2(filename: str):
"""
Create a second sample relations file for testing.
Args:
filename: Path where to create the sample file
"""
sample_content = """midp M A C;
para M N A B;
col N C B;
? midp N C B"""
with open(filename, 'w') as file:
file.write(sample_content)
print(f"Sample relations file created: {filename}")