nailarais1 commited on
Commit
7fee0e0
·
verified ·
1 Parent(s): 97a6621

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -20
app.py CHANGED
@@ -51,39 +51,77 @@ word_to_num = {
51
 
52
  # Function to convert speech input into valid math expressions
53
  def convert_speech_to_math(text):
54
- # Replace words with correct math functions (e.g., "factorial" -> "factorial()")
55
  text = text.lower()
56
 
57
- # Handle factorial, permutation, combinations, log, etc.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  text = re.sub(r'factorial of (\w+)', lambda m: f'factorial({word_to_num.get(m.group(1), m.group(1))})', text)
59
  text = re.sub(r'permutation of (\d+), (\d+)', r'permutation(\1, \2)', text)
60
- text = re.sub(r'combinations of (\d+), (\d+)', r'combinations(\1, \2)', text)
61
- text = re.sub(r'log of (\d+)', r'log(\1)', text)
62
 
63
- # Handle calculus terms like differentiation, integration, limit
64
  text = re.sub(r'differentiate (.+)', r'diff(\1, x)', text)
65
  text = re.sub(r'integrate (.+)', r'integrate(\1, x)', text)
66
  text = re.sub(r'limit of (.+)', r'limit(\1, x, 0)', text)
67
 
68
- # Handle trigonometric functions
69
- text = re.sub(r'sin of (.+)', r'sin(\1)', text)
70
- text = re.sub(r'cos of (.+)', r'cos(\1)', text)
71
- text = re.sub(r'tan of (.+)', r'tan(\1)', text)
72
-
73
- # Handle inverse trigonometric functions
74
- text = re.sub(r'asin of (.+)', r'asin(\1)', text)
75
- text = re.sub(r'acos of (.+)', r'acos(\1)', text)
76
- text = re.sub(r'atan of (.+)', r'atan(\1)', text)
77
-
78
- # Handle matrix-related terms
79
- text = re.sub(r'matrix (\[.*\])', r'Matrix(\1)', text)
80
 
81
- # Ensure all recognized functions are in the correct format
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  text = re.sub(r'(\w+)\s*\(', r'\1(', text) # Remove spaces before parentheses
83
-
84
- # If there's any common mis-formatting, clean it
85
  return text
86
 
 
87
  # Main function to handle input and process it for math calculation (text or voice)
88
  def calculator(audio=None, text_input=None):
89
  if audio:
 
51
 
52
  # Function to convert speech input into valid math expressions
53
  def convert_speech_to_math(text):
54
+ # Convert the input text to lowercase
55
  text = text.lower()
56
 
57
+ # Mapping words to corresponding math functions
58
+ # Arithmetic operations
59
+ text = re.sub(r'add (\d+) and (\d+)', r'\1 + \2', text)
60
+ text = re.sub(r'subtract (\d+) from (\d+)', r'\2 - \1', text)
61
+ text = re.sub(r'multiply (\d+) by (\d+)', r'\1 * \2', text)
62
+ text = re.sub(r'divide (\d+) by (\d+)', r'\1 / \2', text)
63
+
64
+ # Exponentiation and roots
65
+ text = re.sub(r'power of (\d+) to (\d+)', r'\1**\2', text)
66
+ text = re.sub(r'square of (\d+)', r'\1**2', text)
67
+ text = re.sub(r'cube of (\d+)', r'\1**3', text)
68
+ text = re.sub(r'square root of (\d+)', r'sqrt(\1)', text)
69
+ text = re.sub(r'cube root of (\d+)', r'cbrt(\1)', text)
70
+
71
+ # Logarithmic operations
72
+ text = re.sub(r'log base (\d+) of (\d+)', r'log(\2, \1)', text) # log base b of x
73
+ text = re.sub(r'natural log of (\d+)', r'ln(\1)', text)
74
+
75
+ # Trigonometric operations
76
+ text = re.sub(r'sine of (.+)', r'sin(\1)', text)
77
+ text = re.sub(r'cosine of (.+)', r'cos(\1)', text)
78
+ text = re.sub(r'tangent of (.+)', r'tan(\1)', text)
79
+
80
+ # Inverse trigonometric operations
81
+ text = re.sub(r'arc sine of (.+)', r'asin(\1)', text)
82
+ text = re.sub(r'arc cosine of (.+)', r'acos(\1)', text)
83
+ text = re.sub(r'arc tangent of (.+)', r'atan(\1)', text)
84
+
85
+ # Factorials and combinatorics
86
  text = re.sub(r'factorial of (\w+)', lambda m: f'factorial({word_to_num.get(m.group(1), m.group(1))})', text)
87
  text = re.sub(r'permutation of (\d+), (\d+)', r'permutation(\1, \2)', text)
88
+ text = re.sub(r'combination of (\d+), (\d+)', r'combinations(\1, \2)', text)
 
89
 
90
+ # Calculus
91
  text = re.sub(r'differentiate (.+)', r'diff(\1, x)', text)
92
  text = re.sub(r'integrate (.+)', r'integrate(\1, x)', text)
93
  text = re.sub(r'limit of (.+)', r'limit(\1, x, 0)', text)
94
 
95
+ # Matrices
96
+ text = re.sub(r'matrix of (\[.*\])', r'Matrix(\1)', text)
 
 
 
 
 
 
 
 
 
 
97
 
98
+ # Complex numbers
99
+ text = re.sub(r'absolute value of (.+)', r'Abs(\1)', text)
100
+ text = re.sub(r'conjugate of (.+)', r'conjugate(\1)', text)
101
+
102
+ # Set operations
103
+ text = re.sub(r'union of (.+) and (.+)', r'Union(\1, \2)', text)
104
+ text = re.sub(r'intersection of (.+) and (.+)', r'Intersection(\1, \2)', text)
105
+ text = re.sub(r'complement of (.+)', r'Complement(\1)', text)
106
+
107
+ # Statistical operations
108
+ text = re.sub(r'mean of (.+)', r'Mean(\1)', text)
109
+ text = re.sub(r'median of (.+)', r'Median(\1)', text)
110
+ text = re.sub(r'standard deviation of (.+)', r'std(\1)', text)
111
+ text = re.sub(r'variance of (.+)', r'variance(\1)', text)
112
+
113
+ # Custom math operations
114
+ text = re.sub(r'absolute difference between (\d+) and (\d+)', r'Abs(\1 - \2)', text)
115
+ text = re.sub(r'floor of (.+)', r'floor(\1)', text)
116
+ text = re.sub(r'ceiling of (.+)', r'ceiling(\1)', text)
117
+
118
+ # Miscellaneous formatting fixes
119
  text = re.sub(r'(\w+)\s*\(', r'\1(', text) # Remove spaces before parentheses
120
+ text = re.sub(r'to the power of (\d+)', r'**\1', text) # Handle "to the power of"
121
+
122
  return text
123
 
124
+
125
  # Main function to handle input and process it for math calculation (text or voice)
126
  def calculator(audio=None, text_input=None):
127
  if audio: