-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeterministic.py
More file actions
276 lines (227 loc) · 8.26 KB
/
Copy pathdeterministic.py
File metadata and controls
276 lines (227 loc) · 8.26 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import re
import math
# -----------------------------
# Allowed math functions
# -----------------------------
ALLOWED_NAMES = {
k: getattr(math, k) for k in dir(math) if not k.startswith("_")
}
ALLOWED_NAMES.update({
"abs": abs,
"round": round
})
UNIT_ALIASES = {
"km": "km",
"kilometer": "km",
"kilometers": "km",
"kilometre": "km",
"kilometres": "km",
"m": "m",
"meter": "m",
"meters": "m",
"metre": "m",
"metres": "m",
"cm": "cm",
"centimeter": "cm",
"centimeters": "cm",
"centimetre": "cm",
"centimetres": "cm",
"mile": "mile",
"miles": "mile",
"kg": "kg",
"kilogram": "kg",
"kilograms": "kg",
"lb": "lb",
"lbs": "lb",
"pound": "lb",
"pounds": "lb",
"minute": "minute",
"minutes": "minute",
"min": "minute",
"mins": "minute",
"second": "second",
"seconds": "second",
"sec": "second",
"secs": "second",
"hour": "hour",
"hours": "hour",
"hr": "hour",
"hrs": "hour",
"c": "celsius",
"celsius": "celsius",
"f": "fahrenheit",
"fahrenheit": "fahrenheit",
}
LINEAR_CONVERSIONS = {
("km", "mile"): lambda value: value * 0.621371,
("mile", "km"): lambda value: value / 0.621371,
("kg", "lb"): lambda value: value * 2.20462,
("lb", "kg"): lambda value: value / 2.20462,
("cm", "m"): lambda value: value / 100,
("m", "cm"): lambda value: value * 100,
("m", "km"): lambda value: value / 1000,
("km", "m"): lambda value: value * 1000,
("minute", "second"): lambda value: value * 60,
("second", "minute"): lambda value: value / 60,
("hour", "minute"): lambda value: value * 60,
("minute", "hour"): lambda value: value / 60,
}
UNIT_DISPLAY = {
"km": ("km", "km"),
"m": ("meter", "meters"),
"cm": ("cm", "cm"),
"mile": ("mile", "miles"),
"kg": ("kg", "kg"),
"lb": ("pound", "pounds"),
"minute": ("minute", "minutes"),
"second": ("second", "seconds"),
"hour": ("hour", "hours"),
"celsius": ("°C", "°C"),
"fahrenheit": ("°F", "°F"),
}
# -----------------------------
# Prime Check
# -----------------------------
def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
# -----------------------------
# Safe Evaluator
# -----------------------------
def safe_eval(expr: str):
try:
expr = expr.replace("^", "**")
# Convert 15% → (15/100)
expr = re.sub(r"(\d+)%", r"(\1/100)", expr)
code = compile(expr, "<string>", "eval")
for name in code.co_names:
if name not in ALLOWED_NAMES:
return None
result = eval(code, {"__builtins__": {}}, ALLOWED_NAMES)
# Clean float formatting
if isinstance(result, float):
if result.is_integer():
result = int(result)
else:
result = round(result, 6)
return result
except:
return None
def format_numeric(value):
if isinstance(value, float):
if value.is_integer():
return str(int(value))
return str(round(value, 6))
return str(value)
def normalize_prompt(prompt: str):
prompt = prompt.lower().strip()
prompt = re.sub(r"\s+", " ", prompt)
return prompt
def normalize_math_prompt(prompt: str):
prompt = normalize_prompt(prompt)
prompt = re.sub(
r"^(what is|calculate|solve|result of|compute|evaluate|convert|can you calculate|can you compute)\s+",
"",
prompt,
)
prompt = re.sub(r"\bmultiplied by\b", "*", prompt)
prompt = re.sub(r"\bdivided by\b", "/", prompt)
prompt = re.sub(r"\btimes\b", "*", prompt)
prompt = re.sub(r"\bplus\b", "+", prompt)
prompt = re.sub(r"\bminus\b", "-", prompt)
prompt = re.sub(r"^square root of\s+(\d+\.?\d*)$", r"sqrt(\1)", prompt)
prompt = re.sub(r"^sqrt\s+(\d+\.?\d*)$", r"sqrt(\1)", prompt)
return prompt.strip(" ?.")
def normalize_unit_name(unit: str):
return UNIT_ALIASES.get(unit.lower().strip(), unit.lower().strip())
def format_unit_label(unit: str, value: float):
singular, plural = UNIT_DISPLAY.get(unit, (unit, unit))
return singular if abs(value) == 1 else plural
# -----------------------------
# Unit Conversions
# -----------------------------
def unit_conversion(prompt: str):
prompt = normalize_prompt(prompt)
prompt = prompt.replace(" into ", " to ").replace(" in ", " to ")
conversion_match = re.search(r"(\d+\.?\d*)\s*([a-z°]+)\s+to\s+([a-z°]+)", prompt)
if not conversion_match:
return None
value = float(conversion_match.group(1))
source_unit = normalize_unit_name(conversion_match.group(2).replace("°", ""))
target_unit = normalize_unit_name(conversion_match.group(3).replace("°", ""))
if (source_unit, target_unit) == ("celsius", "fahrenheit"):
result = round((value * 9 / 5) + 32, 2)
return f"{format_numeric(value)}°C = {format_numeric(result)}°F"
if (source_unit, target_unit) == ("fahrenheit", "celsius"):
result = round((value - 32) * 5 / 9, 2)
return f"{format_numeric(value)}°F = {format_numeric(result)}°C"
converter = LINEAR_CONVERSIONS.get((source_unit, target_unit))
if converter:
result = converter(value)
source_label = format_unit_label(source_unit, value)
target_label = format_unit_label(target_unit, result)
return f"{format_numeric(value)} {source_label} = {format_numeric(result)} {target_label}"
return None
# -----------------------------
# Deterministic Entry Point
# -----------------------------
def deterministic_engine(prompt: str):
prompt = prompt.strip()
prompt_lower = normalize_prompt(prompt)
# Strip natural language wrappers for math
math_prompt = normalize_math_prompt(prompt_lower)
greetings = ["hi", "hello", "hey", "yo", "sup", "good morning", "good evening", "good afternoon", "greetings", "what's up", "how are you", "how's it going", "how do you do", "nice to meet you", "pleased to meet you", "how are you doing", "how have you been", "what's new", "what's going on", "how's everything", "how's life", "how's your day", "how's it going", "how's it hanging","Hey, whats up"]
if prompt_lower.strip() in greetings:
return "Hello! How can I help you today?"
# 1️⃣ Prime check
prime_match = re.search(r"(?:is\s+)?(\d+)\s+(?:a\s+)?prime(?:\s+number)?", prompt_lower)
if not prime_match:
prime_match = re.search(r"check if (\d+) is prime", prompt_lower)
if prime_match:
number = int(prime_match.group(1))
return f"{number} is prime: {is_prime(number)}"
# 2️⃣ Percentage natural language: "15% of 200"
percent_of = re.search(r"(\d+)%\s*of\s*(\d+)", prompt_lower)
if percent_of:
a = float(percent_of.group(1))
b = float(percent_of.group(2))
result = (a / 100) * b
if result.is_integer():
result = int(result)
return f"Result: {result}"
# 3️⃣ Pure arithmetic detection (must contain at least one digit)
math_pattern = r"^(?=.*\d)[0-9\.\+\-\*/\(\)%\s\^!<>=&|~]*$"
if re.fullmatch(math_pattern, math_prompt):
result = safe_eval(math_prompt)
if result is not None:
return f"Result: {result}"
else:
return "This mathematical expression appears to be invalid or contains unsupported syntax."
# 4️⃣ Function-based math detection
allowed_fns = ["sqrt", "log", "sin", "cos", "tan", "pi", "e", "abs", "round"]
if any(fn in math_prompt for fn in allowed_fns):
result = safe_eval(math_prompt)
if result is not None:
return f"Result: {result}"
else:
stripped = math_prompt
for fn in allowed_fns:
stripped = stripped.replace(fn, "")
math_pattern_extended = r"^[0-9\.\+\-\*/\(\)%\s\^!<>=&|~,]*$"
if re.fullmatch(math_pattern_extended, stripped):
return "This mathematical expression appears to be invalid or contains unsupported syntax."
# 5️⃣ Unit conversion
conversion = unit_conversion(prompt)
if conversion:
return conversion
return None