PrashanthB461 commited on
Commit
0487336
·
verified ·
1 Parent(s): 1f5aef9

Update calculator/utils.py

Browse files
Files changed (1) hide show
  1. calculator/utils.py +12 -15
calculator/utils.py CHANGED
@@ -1,20 +1,17 @@
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
 
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
+