Update calculator/utils.py
Browse files- calculator/utils.py +12 -15
calculator/utils.py
CHANGED
|
@@ -1,20 +1,17 @@
|
|
| 1 |
import re
|
| 2 |
|
| 3 |
def extract_operation(question):
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
return None
|
| 15 |
|
| 16 |
def extract_numbers(question):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
numbers = [float(num) for num in numbers]
|
| 20 |
-
return numbers
|
|
|
|
| 1 |
import re
|
| 2 |
|
| 3 |
def extract_operation(question):
|
| 4 |
+
ops = {
|
| 5 |
+
"add": "add", "plus": "add", "+": "add",
|
| 6 |
+
"subtract": "subtract", "minus": "subtract", "-": "subtract",
|
| 7 |
+
"multiply": "multiply", "times": "multiply", "*": "multiply",
|
| 8 |
+
"divide": "divide", "over": "divide", "/": "divide"
|
| 9 |
+
}
|
| 10 |
+
for word in question.lower().split():
|
| 11 |
+
if word in ops:
|
| 12 |
+
return ops[word]
|
| 13 |
+
return None
|
|
|
|
| 14 |
|
| 15 |
def extract_numbers(question):
|
| 16 |
+
return [float(n) for n in re.findall(r"[-+]?\d*\.\d+|\d+", question)]
|
| 17 |
+
|
|
|
|
|
|