nailarais1 commited on
Commit
f6bde50
·
verified ·
1 Parent(s): f8fd0bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -16
app.py CHANGED
@@ -268,10 +268,10 @@ def convert_speech_to_math(text):
268
  """Convert natural language to math expressions"""
269
  if not text or text.strip() == "":
270
  return "0"
271
-
272
  text = text.lower().strip()
273
-
274
- # Enhanced pattern matching
275
  patterns = [
276
  (r'add\s+(\d+)\s+and\s+(\d+)', r'\1 + \2'),
277
  (r'what is\s+(\d+)\s+plus\s+(\d+)', r'\1 + \2'),
@@ -293,27 +293,39 @@ def convert_speech_to_math(text):
293
  (r'tangent of\s+(.+)', r'tan(\1)'),
294
  (r'derivative of\s+(.+)', r'diff(\1, x)'),
295
  (r'integral of\s+(.+)', r'integrate(\1, x)'),
296
- (r'factorial of\s+(\d+)', r'factorial\((\d+)\)'), # Corrected pattern for factorial
297
  ]
298
-
299
  for pattern, replacement in patterns:
300
- text = re.sub(pattern, replacement, text)
301
-
302
- # Word replacements
 
 
 
 
303
  replacements = {
304
  'zero': '0', 'one': '1', 'two': '2', 'three': '3', 'four': '4',
305
  'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9',
306
  'ten': '10', 'plus': '+', 'minus': '-', 'times': '*', 'multiplied by': '*',
307
- 'divided by': '/', 'over': '/', 'pi': 'pi', 'e': 'E', 'equals': '=='
308
  }
309
-
310
  for word, replacement in replacements.items():
311
- text = re.sub(r'\b' + word + r'\b', replacement, text)
312
-
313
- # Clean up
314
- text = re.sub(r'\s*([+\-*/^()])\s*', r'\1', text)
315
- text = re.sub(r'(\d)([a-zA-Z(])', r'\1*\2', text) # Add multiplication sign if missing
316
-
 
 
 
 
 
 
 
 
317
  return text
318
 
319
  def evaluate_advanced_math(expression):
 
268
  """Convert natural language to math expressions"""
269
  if not text or text.strip() == "":
270
  return "0"
271
+
272
  text = text.lower().strip()
273
+
274
+ # Enhanced pattern matching with raw strings to avoid regex issues
275
  patterns = [
276
  (r'add\s+(\d+)\s+and\s+(\d+)', r'\1 + \2'),
277
  (r'what is\s+(\d+)\s+plus\s+(\d+)', r'\1 + \2'),
 
293
  (r'tangent of\s+(.+)', r'tan(\1)'),
294
  (r'derivative of\s+(.+)', r'diff(\1, x)'),
295
  (r'integral of\s+(.+)', r'integrate(\1, x)'),
296
+ (r'factorial of\s+(\d+)', r'factorial(\1)'),
297
  ]
298
+
299
  for pattern, replacement in patterns:
300
+ try:
301
+ text = re.sub(pattern, replacement, text)
302
+ except Exception as e:
303
+ print(f"Regex pattern failed: {pattern}, error: {e}")
304
+ continue
305
+
306
+ # Word replacements (safe method)
307
  replacements = {
308
  'zero': '0', 'one': '1', 'two': '2', 'three': '3', 'four': '4',
309
  'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9',
310
  'ten': '10', 'plus': '+', 'minus': '-', 'times': '*', 'multiplied by': '*',
311
+ 'divided by': '/', 'over': '/', 'pi': 'pi', 'e': 'E'
312
  }
313
+
314
  for word, replacement in replacements.items():
315
+ text = text.replace(word, replacement)
316
+
317
+ # Clean up spaces around operators (safe method)
318
+ operators = ['+', '-', '*', '/', '^', '(', ')']
319
+ for op in operators:
320
+ text = text.replace(f' {op} ', op).replace(f' {op}', op).replace(f'{op} ', op)
321
+
322
+ # Add multiplication where implied (safe method)
323
+ text = text.replace(')(', ')*(')
324
+
325
+ # Handle number followed by letter/variable
326
+ import re
327
+ text = re.sub(r'(\d)([a-zA-Z(])', r'\1*\2', text)
328
+
329
  return text
330
 
331
  def evaluate_advanced_math(expression):