Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -96,18 +96,27 @@ word_to_num = {
|
|
96 |
"hundred": 100, "thousand": 1000
|
97 |
}
|
98 |
|
99 |
-
|
|
|
100 |
def convert_speech_to_math(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
# Convert the input text to lowercase
|
102 |
text = text.lower()
|
103 |
|
104 |
-
# Mapping words to corresponding math functions
|
105 |
# Arithmetic operations
|
106 |
text = re.sub(r'add (\d+) and (\d+)', r'\1 + \2', text)
|
107 |
text = re.sub(r'subtract (\d+) from (\d+)', r'\2 - \1', text)
|
108 |
text = re.sub(r'multiply (\d+) by (\d+)', r'\1 * \2', text)
|
109 |
text = re.sub(r'divide (\d+) by (\d+)', r'\1 / \2', text)
|
110 |
-
|
111 |
# Exponentiation and roots
|
112 |
text = re.sub(r'power of (\d+) to (\d+)', r'\1**\2', text)
|
113 |
text = re.sub(r'square of (\d+)', r'\1**2', text)
|
@@ -116,9 +125,10 @@ def convert_speech_to_math(text):
|
|
116 |
text = re.sub(r'cube root of (\d+)', r'cbrt(\1)', text)
|
117 |
|
118 |
# Logarithmic operations
|
119 |
-
text = re.sub(r'log
|
120 |
-
text = re.sub(r'
|
121 |
-
|
|
|
122 |
# Trigonometric operations
|
123 |
text = re.sub(r'sine of (.+)', r'sin(\1)', text)
|
124 |
text = re.sub(r'cosine of (.+)', r'cos(\1)', text)
|
@@ -128,9 +138,9 @@ def convert_speech_to_math(text):
|
|
128 |
text = re.sub(r'arc sine of (.+)', r'asin(\1)', text)
|
129 |
text = re.sub(r'arc cosine of (.+)', r'acos(\1)', text)
|
130 |
text = re.sub(r'arc tangent of (.+)', r'atan(\1)', text)
|
131 |
-
|
132 |
# Factorials and combinatorics
|
133 |
-
text = re.sub(r'factorial of (\w+)', lambda m: f'factorial({
|
134 |
text = re.sub(r'permutation of (\d+), (\d+)', r'permutation(\1, \2)', text)
|
135 |
text = re.sub(r'combination of (\d+), (\d+)', r'combinations(\1, \2)', text)
|
136 |
|
@@ -145,12 +155,12 @@ def convert_speech_to_math(text):
|
|
145 |
# Complex numbers
|
146 |
text = re.sub(r'absolute value of (.+)', r'Abs(\1)', text)
|
147 |
text = re.sub(r'conjugate of (.+)', r'conjugate(\1)', text)
|
148 |
-
|
149 |
# Set operations
|
150 |
text = re.sub(r'union of (.+) and (.+)', r'Union(\1, \2)', text)
|
151 |
text = re.sub(r'intersection of (.+) and (.+)', r'Intersection(\1, \2)', text)
|
152 |
text = re.sub(r'complement of (.+)', r'Complement(\1)', text)
|
153 |
-
|
154 |
# Statistical operations
|
155 |
text = re.sub(r'mean of (.+)', r'Mean(\1)', text)
|
156 |
text = re.sub(r'median of (.+)', r'Median(\1)', text)
|
@@ -163,11 +173,16 @@ def convert_speech_to_math(text):
|
|
163 |
text = re.sub(r'ceiling of (.+)', r'ceiling(\1)', text)
|
164 |
|
165 |
# Miscellaneous formatting fixes
|
166 |
-
text = re.sub(r'
|
167 |
text = re.sub(r'to the power of (\d+)', r'**\1', text) # Handle "to the power of"
|
168 |
-
|
|
|
|
|
|
|
|
|
169 |
return text
|
170 |
|
|
|
171 |
def calculator(audio=None, text_input=None, tts_engine="auto"):
|
172 |
"""
|
173 |
Main function to handle input and process it for math calculation (text or voice).
|
|
|
96 |
"hundred": 100, "thousand": 1000
|
97 |
}
|
98 |
|
99 |
+
import re
|
100 |
+
|
101 |
def convert_speech_to_math(text):
|
102 |
+
"""
|
103 |
+
Converts natural language math queries into valid mathematical expressions.
|
104 |
+
|
105 |
+
Args:
|
106 |
+
text (str): The natural language input.
|
107 |
+
|
108 |
+
Returns:
|
109 |
+
str: The converted mathematical expression.
|
110 |
+
"""
|
111 |
# Convert the input text to lowercase
|
112 |
text = text.lower()
|
113 |
|
|
|
114 |
# Arithmetic operations
|
115 |
text = re.sub(r'add (\d+) and (\d+)', r'\1 + \2', text)
|
116 |
text = re.sub(r'subtract (\d+) from (\d+)', r'\2 - \1', text)
|
117 |
text = re.sub(r'multiply (\d+) by (\d+)', r'\1 * \2', text)
|
118 |
text = re.sub(r'divide (\d+) by (\d+)', r'\1 / \2', text)
|
119 |
+
|
120 |
# Exponentiation and roots
|
121 |
text = re.sub(r'power of (\d+) to (\d+)', r'\1**\2', text)
|
122 |
text = re.sub(r'square of (\d+)', r'\1**2', text)
|
|
|
125 |
text = re.sub(r'cube root of (\d+)', r'cbrt(\1)', text)
|
126 |
|
127 |
# Logarithmic operations
|
128 |
+
text = re.sub(r'log of (\d+)', r'log(\1)', text) # Convert "log of 100" to "log(100)"
|
129 |
+
text = re.sub(r'log base (\d+) of (\d+)', r'log(\2, \1)', text) # Handle "log base 2 of 8"
|
130 |
+
text = re.sub(r'natural log of (\d+)', r'ln(\1)', text) # Handle "natural log of 10"
|
131 |
+
|
132 |
# Trigonometric operations
|
133 |
text = re.sub(r'sine of (.+)', r'sin(\1)', text)
|
134 |
text = re.sub(r'cosine of (.+)', r'cos(\1)', text)
|
|
|
138 |
text = re.sub(r'arc sine of (.+)', r'asin(\1)', text)
|
139 |
text = re.sub(r'arc cosine of (.+)', r'acos(\1)', text)
|
140 |
text = re.sub(r'arc tangent of (.+)', r'atan(\1)', text)
|
141 |
+
|
142 |
# Factorials and combinatorics
|
143 |
+
text = re.sub(r'factorial of (\w+)', lambda m: f'factorial({m.group(1)})', text)
|
144 |
text = re.sub(r'permutation of (\d+), (\d+)', r'permutation(\1, \2)', text)
|
145 |
text = re.sub(r'combination of (\d+), (\d+)', r'combinations(\1, \2)', text)
|
146 |
|
|
|
155 |
# Complex numbers
|
156 |
text = re.sub(r'absolute value of (.+)', r'Abs(\1)', text)
|
157 |
text = re.sub(r'conjugate of (.+)', r'conjugate(\1)', text)
|
158 |
+
|
159 |
# Set operations
|
160 |
text = re.sub(r'union of (.+) and (.+)', r'Union(\1, \2)', text)
|
161 |
text = re.sub(r'intersection of (.+) and (.+)', r'Intersection(\1, \2)', text)
|
162 |
text = re.sub(r'complement of (.+)', r'Complement(\1)', text)
|
163 |
+
|
164 |
# Statistical operations
|
165 |
text = re.sub(r'mean of (.+)', r'Mean(\1)', text)
|
166 |
text = re.sub(r'median of (.+)', r'Median(\1)', text)
|
|
|
173 |
text = re.sub(r'ceiling of (.+)', r'ceiling(\1)', text)
|
174 |
|
175 |
# Miscellaneous formatting fixes
|
176 |
+
text = re.sub(r'\s*\(', r'(', text) # Remove spaces before parentheses
|
177 |
text = re.sub(r'to the power of (\d+)', r'**\1', text) # Handle "to the power of"
|
178 |
+
|
179 |
+
# Handle additional edge cases
|
180 |
+
text = re.sub(r'logof(\d+)', r'log(\1)', text) # Correct misinterpreted "logof100"
|
181 |
+
text = re.sub(r'sqrt(\d+)', r'sqrt(\1)', text) # Ensure proper sqrt function
|
182 |
+
|
183 |
return text
|
184 |
|
185 |
+
|
186 |
def calculator(audio=None, text_input=None, tts_engine="auto"):
|
187 |
"""
|
188 |
Main function to handle input and process it for math calculation (text or voice).
|