Create calculator/utils.py
Browse files- calculator/utils.py +20 -0
calculator/utils.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
def extract_operation(question):
|
| 4 |
+
# Simple regex to identify the operation
|
| 5 |
+
if "add" in question.lower():
|
| 6 |
+
return "add"
|
| 7 |
+
elif "subtract" in question.lower() or "minus" in question.lower():
|
| 8 |
+
return "subtract"
|
| 9 |
+
elif "multiply" in question.lower() or "times" in question.lower():
|
| 10 |
+
return "multiply"
|
| 11 |
+
elif "divide" in question.lower() or "over" in question.lower():
|
| 12 |
+
return "divide"
|
| 13 |
+
else:
|
| 14 |
+
return None
|
| 15 |
+
|
| 16 |
+
def extract_numbers(question):
|
| 17 |
+
# Extract all numbers from the question
|
| 18 |
+
numbers = re.findall(r"[-+]?\d*\.\d+|\d+", question)
|
| 19 |
+
numbers = [float(num) for num in numbers]
|
| 20 |
+
return numbers
|