azrideus-mathparse 0.2.10__tar.gz → 0.2.12__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/PKG-INFO +1 -1
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/azrideus_mathparse.egg-info/PKG-INFO +1 -1
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/azrideus_mathparse.egg-info/SOURCES.txt +1 -0
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/mathparse/__init__.py +1 -1
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/mathparse/mathparse.py +267 -152
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/mathparse/mathwords.py +37 -1
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/tests/test_binary_operations.py +41 -0
- azrideus_mathparse-0.2.12/tests/test_limits.py +70 -0
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/tests/test_unary_operations.py +81 -0
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/LICENSE +0 -0
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/README.md +0 -0
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/azrideus_mathparse.egg-info/dependency_links.txt +0 -0
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/azrideus_mathparse.egg-info/requires.txt +0 -0
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/azrideus_mathparse.egg-info/top_level.txt +0 -0
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/pyproject.toml +0 -0
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/setup.cfg +0 -0
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/tests/test_complex_statements.py +0 -0
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/tests/test_language_support.py +0 -0
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/tests/test_prefix_unary_operations.py +0 -0
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/tests/test_replace_word_tokens.py +0 -0
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/tests/test_simplified_chinese.py +0 -0
- {azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/tests/test_utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: azrideus-mathparse
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.12
|
|
4
4
|
Summary: A fork of mathparse (https://github.com/gunthercox/mathparse) for my own usage; a library for solving mathematical equations contained in strings
|
|
5
5
|
Author: Gunther Cox
|
|
6
6
|
License-Expression: MIT
|
{azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/azrideus_mathparse.egg-info/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: azrideus-mathparse
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.12
|
|
4
4
|
Summary: A fork of mathparse (https://github.com/gunthercox/mathparse) for my own usage; a library for solving mathematical equations contained in strings
|
|
5
5
|
Author: Gunther Cox
|
|
6
6
|
License-Expression: MIT
|
{azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/azrideus_mathparse.egg-info/SOURCES.txt
RENAMED
|
@@ -12,6 +12,7 @@ mathparse/mathwords.py
|
|
|
12
12
|
tests/test_binary_operations.py
|
|
13
13
|
tests/test_complex_statements.py
|
|
14
14
|
tests/test_language_support.py
|
|
15
|
+
tests/test_limits.py
|
|
15
16
|
tests/test_prefix_unary_operations.py
|
|
16
17
|
tests/test_replace_word_tokens.py
|
|
17
18
|
tests/test_simplified_chinese.py
|
|
@@ -1,25 +1,63 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Methods for evaluating mathematical equations in strings.
|
|
3
3
|
"""
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
from decimal import Decimal, DecimalException
|
|
5
6
|
from typing import Union
|
|
6
7
|
from . import mathwords
|
|
8
|
+
import math
|
|
7
9
|
import re
|
|
8
10
|
|
|
9
11
|
|
|
12
|
+
# Limits that protect against expressions which would take too long or use
|
|
13
|
+
# too much memory to evaluate, such as "999999 ** 999999999". Either limit can
|
|
14
|
+
# be changed, or set to None to disable it.
|
|
15
|
+
|
|
16
|
+
# The maximum number of characters in an expression passed to parse()
|
|
17
|
+
MAX_EXPRESSION_LENGTH = 1000
|
|
18
|
+
|
|
19
|
+
# The maximum number of digits in any integer computed during evaluation
|
|
20
|
+
MAX_INTEGER_DIGITS = 50
|
|
21
|
+
|
|
22
|
+
|
|
10
23
|
class PostfixTokenEvaluationException(Exception):
|
|
11
24
|
"""
|
|
12
25
|
Exception to be raised when an expression cannot be evaluated.
|
|
13
26
|
"""
|
|
27
|
+
|
|
28
|
+
pass
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ExpressionTooLargeException(PostfixTokenEvaluationException):
|
|
32
|
+
"""
|
|
33
|
+
Exception to be raised when an expression, or a number computed while
|
|
34
|
+
evaluating it, exceeds the configured size limits.
|
|
35
|
+
"""
|
|
36
|
+
|
|
14
37
|
pass
|
|
15
38
|
|
|
16
39
|
|
|
40
|
+
def check_integer_digits(log10_value: float, token: str) -> None:
|
|
41
|
+
"""
|
|
42
|
+
Raise an ExpressionTooLargeException if an integer whose base 10
|
|
43
|
+
logarithm is (approximately) log10_value has more than MAX_INTEGER_DIGITS
|
|
44
|
+
digits.
|
|
45
|
+
"""
|
|
46
|
+
# An integer n has floor(log10(n)) + 1 digits
|
|
47
|
+
if MAX_INTEGER_DIGITS is not None and log10_value >= MAX_INTEGER_DIGITS:
|
|
48
|
+
raise ExpressionTooLargeException(
|
|
49
|
+
'The result of "{}" would have more than {} digits'.format(
|
|
50
|
+
token, MAX_INTEGER_DIGITS
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
|
|
17
55
|
def is_int(string: str) -> bool:
|
|
18
56
|
"""
|
|
19
57
|
Return true if string is an integer.
|
|
20
58
|
"""
|
|
21
59
|
# Allow leading minus sign
|
|
22
|
-
if string[0] ==
|
|
60
|
+
if string[0] == "-":
|
|
23
61
|
# Must have at least one digit after the minus
|
|
24
62
|
return len(string) > 1 and string[1:].isdigit()
|
|
25
63
|
else:
|
|
@@ -34,7 +72,7 @@ def is_float(string: str) -> bool:
|
|
|
34
72
|
try:
|
|
35
73
|
float(string)
|
|
36
74
|
# Exponent format numbers such as "25e2" are also floats
|
|
37
|
-
return
|
|
75
|
+
return "." in string or "e" in string
|
|
38
76
|
except ValueError:
|
|
39
77
|
return False
|
|
40
78
|
|
|
@@ -66,10 +104,13 @@ def is_symbol(string: str) -> bool:
|
|
|
66
104
|
Return true if the string is a mathematical symbol.
|
|
67
105
|
"""
|
|
68
106
|
return (
|
|
69
|
-
is_int(string)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
107
|
+
is_int(string)
|
|
108
|
+
or is_float(string)
|
|
109
|
+
or is_constant(string)
|
|
110
|
+
or is_unary(string)
|
|
111
|
+
or is_binary(string)
|
|
112
|
+
or (string == "(")
|
|
113
|
+
or (string == ")")
|
|
73
114
|
)
|
|
74
115
|
|
|
75
116
|
|
|
@@ -87,7 +128,7 @@ def to_number(val) -> Union[int, float, str, Decimal]:
|
|
|
87
128
|
Convert a string to an int or float if possible.
|
|
88
129
|
"""
|
|
89
130
|
# If already a number (int, float, Decimal), return as-is
|
|
90
|
-
if isinstance(val, (int, float, Decimal)):
|
|
131
|
+
if isinstance(val, (int, float, Decimal, mathwords.ArgumentList)):
|
|
91
132
|
return val
|
|
92
133
|
|
|
93
134
|
# Check if it's a constant and convert
|
|
@@ -105,7 +146,7 @@ def to_number(val) -> Union[int, float, str, Decimal]:
|
|
|
105
146
|
|
|
106
147
|
# Numbers in exponent format, such as "2.5e3", "1e-6" or ".5E+2"
|
|
107
148
|
SCIENTIFIC_NOTATION_PATTERN = re.compile(
|
|
108
|
-
r
|
|
149
|
+
r"(?<![\w.])(\d+\.?\d*|\.\d+)e([+-]?\d+)(?![\w.])"
|
|
109
150
|
)
|
|
110
151
|
|
|
111
152
|
|
|
@@ -115,9 +156,10 @@ def normalize_scientific_notation(string: str) -> str:
|
|
|
115
156
|
that they are kept as a single token, rather than being split apart by the
|
|
116
157
|
'.' and '+' binary operators. For example "2.5e+3" becomes "25e2".
|
|
117
158
|
"""
|
|
159
|
+
|
|
118
160
|
def replace(match):
|
|
119
161
|
_, digits, exponent = Decimal(match.group(0)).as_tuple()
|
|
120
|
-
return
|
|
162
|
+
return "{}e{}".format("".join(str(d) for d in digits), exponent)
|
|
121
163
|
|
|
122
164
|
return SCIENTIFIC_NOTATION_PATTERN.sub(replace, string)
|
|
123
165
|
|
|
@@ -143,9 +185,9 @@ def create_unicode_word_boundary_pattern(word: str) -> str:
|
|
|
143
185
|
'(?:^|(?<=[\\s+\\-*/^()]))दो(?:$|(?=[\\s+\\-*/^()]))'
|
|
144
186
|
"""
|
|
145
187
|
# Check if word contains only ASCII alphanumeric characters
|
|
146
|
-
if word.isascii() and word.replace(
|
|
188
|
+
if word.isascii() and word.replace("-", "").replace("'", "").isalnum():
|
|
147
189
|
# For ASCII words, use standard word boundaries
|
|
148
|
-
return r
|
|
190
|
+
return r"\b" + re.escape(word) + r"\b"
|
|
149
191
|
else:
|
|
150
192
|
# For non-ASCII (Unicode) words, use space/operator boundaries
|
|
151
193
|
# This pattern matches the word when it's:
|
|
@@ -153,8 +195,8 @@ def create_unicode_word_boundary_pattern(word: str) -> str:
|
|
|
153
195
|
# - At the end of string OR followed by whitespace/operator
|
|
154
196
|
escaped_word = re.escape(word)
|
|
155
197
|
# Boundary characters: whitespace, operators, parentheses
|
|
156
|
-
boundary = r
|
|
157
|
-
return f
|
|
198
|
+
boundary = r"[\s+\-*/^()]"
|
|
199
|
+
return f"(?:^|(?<={boundary})){escaped_word}(?:$|(?={boundary}))"
|
|
158
200
|
|
|
159
201
|
|
|
160
202
|
def find_word_groups(string: str, words: list) -> list:
|
|
@@ -163,29 +205,24 @@ def find_word_groups(string: str, words: list) -> list:
|
|
|
163
205
|
The words parameter should be the list of words to check for
|
|
164
206
|
such as "hundred".
|
|
165
207
|
"""
|
|
166
|
-
scale_pattern =
|
|
208
|
+
scale_pattern = "|".join(words)
|
|
167
209
|
# For example:
|
|
168
210
|
# (?:(?:\d+)\s+(?:hundred|thousand)*\s*)+(?:\d+|hundred|thousand)+
|
|
169
211
|
regex = re.compile(
|
|
170
|
-
r
|
|
171
|
-
scale_pattern +
|
|
172
|
-
r')*\s*)+(?:\d+|' +
|
|
173
|
-
scale_pattern + r')+'
|
|
212
|
+
r"(?:(?:\d+)\s+(?:" + scale_pattern + r")*\s*)+(?:\d+|" + scale_pattern + r")+"
|
|
174
213
|
)
|
|
175
214
|
result = regex.findall(string)
|
|
176
215
|
return result
|
|
177
216
|
|
|
178
217
|
|
|
179
|
-
def replace_word_tokens_simplified_chinese(
|
|
180
|
-
string, stopwords: set[str] = None
|
|
181
|
-
) -> str:
|
|
218
|
+
def replace_word_tokens_simplified_chinese(string, stopwords: set[str] = None) -> str:
|
|
182
219
|
"""
|
|
183
220
|
simplified Chinese version:
|
|
184
221
|
Given a string and an ISO 639-2 language code,
|
|
185
222
|
return the string with the words replaced with
|
|
186
223
|
an operational equivalent.
|
|
187
224
|
"""
|
|
188
|
-
words = mathwords.word_groups_for_language(
|
|
225
|
+
words = mathwords.word_groups_for_language("CHI")
|
|
189
226
|
|
|
190
227
|
# Handle special Chinese power construction patterns FIRST
|
|
191
228
|
# In Chinese, '的...次方' forms a power construction where:
|
|
@@ -194,8 +231,8 @@ def replace_word_tokens_simplified_chinese(
|
|
|
194
231
|
# - '次幂' is similar to '次方'
|
|
195
232
|
# Example: 二的四次方 = 2's 4th power = 2 ^ 4
|
|
196
233
|
# We need to remove '次方' and '次幂' BEFORE they get replaced as operators
|
|
197
|
-
string = string.replace(
|
|
198
|
-
string = string.replace(
|
|
234
|
+
string = string.replace("次方", " ")
|
|
235
|
+
string = string.replace("次幂", " ")
|
|
199
236
|
|
|
200
237
|
# Collect all operators (binary, prefix unary, postfix unary)
|
|
201
238
|
# and process them by length (longest first) to handle cases where
|
|
@@ -204,14 +241,14 @@ def replace_word_tokens_simplified_chinese(
|
|
|
204
241
|
all_operators = {}
|
|
205
242
|
|
|
206
243
|
# Add binary operators
|
|
207
|
-
all_operators.update(words[
|
|
244
|
+
all_operators.update(words["binary_operators"])
|
|
208
245
|
|
|
209
246
|
# Add prefix unary operators
|
|
210
|
-
all_operators.update(words[
|
|
247
|
+
all_operators.update(words["prefix_unary_operators"])
|
|
211
248
|
|
|
212
249
|
# Add postfix unary operators
|
|
213
|
-
if
|
|
214
|
-
all_operators.update(words[
|
|
250
|
+
if "postfix_unary_operators" in words:
|
|
251
|
+
all_operators.update(words["postfix_unary_operators"])
|
|
215
252
|
|
|
216
253
|
# Sort all operators by length (longest first)
|
|
217
254
|
sorted_operators = sorted(all_operators.keys(), key=len, reverse=True)
|
|
@@ -219,36 +256,34 @@ def replace_word_tokens_simplified_chinese(
|
|
|
219
256
|
for operator in sorted_operators:
|
|
220
257
|
if operator in string:
|
|
221
258
|
# 中文没有分隔符,后面需要靠分隔符分割式子,每次识别一个符号都将其分开来
|
|
222
|
-
string = string.replace(
|
|
223
|
-
operator, ' ' + all_operators[operator] + ' '
|
|
224
|
-
)
|
|
259
|
+
string = string.replace(operator, " " + all_operators[operator] + " ")
|
|
225
260
|
|
|
226
261
|
# chinese_scales用list的原因是为了保持从大到小的顺序,亿、万、千...
|
|
227
262
|
# Sort scales by their numeric value (largest first) to ensure correct
|
|
228
263
|
# parsing. For example, '千' (1000) should be found before '百' (100)
|
|
229
|
-
digits = set(words[
|
|
264
|
+
digits = set(words["numbers"].keys())
|
|
230
265
|
scales = sorted(
|
|
231
|
-
words[
|
|
266
|
+
words["scales"].keys(), key=lambda x: words["scales"][x], reverse=True
|
|
232
267
|
)
|
|
233
|
-
digits_scales = words[
|
|
234
|
-
digits_scales.update(words[
|
|
268
|
+
digits_scales = words["numbers"].copy()
|
|
269
|
+
digits_scales.update(words["scales"])
|
|
235
270
|
|
|
236
271
|
# 九千八百万九千八百——> 98009800
|
|
237
272
|
def chinese_string_to_num(str):
|
|
238
|
-
if str ==
|
|
273
|
+
if str == "":
|
|
239
274
|
return 0
|
|
240
275
|
|
|
241
276
|
if str in digits:
|
|
242
277
|
return digits_scales[str]
|
|
243
278
|
|
|
244
|
-
digits_scales.update(words[
|
|
279
|
+
digits_scales.update(words["scales"])
|
|
245
280
|
|
|
246
281
|
for scale in scales:
|
|
247
282
|
index = str.find(scale)
|
|
248
283
|
if index >= 0:
|
|
249
284
|
t1 = chinese_string_to_num(str[:index])
|
|
250
285
|
t2 = digits_scales[scale]
|
|
251
|
-
t3 = chinese_string_to_num(str[index + 1:])
|
|
286
|
+
t3 = chinese_string_to_num(str[index + 1 :])
|
|
252
287
|
return t1 * t2 + t3
|
|
253
288
|
else:
|
|
254
289
|
return digits_scales[str]
|
|
@@ -266,7 +301,7 @@ def replace_word_tokens_simplified_chinese(
|
|
|
266
301
|
if start < end:
|
|
267
302
|
num = str(chinese_string_to_num(string[start:end]))
|
|
268
303
|
# 需要加多一个分隔符
|
|
269
|
-
num_str = str(num) +
|
|
304
|
+
num_str = str(num) + " "
|
|
270
305
|
string = string[:start] + num_str + string[end:]
|
|
271
306
|
index = start + len(num_str)
|
|
272
307
|
start = end = index
|
|
@@ -277,7 +312,7 @@ def replace_word_tokens_simplified_chinese(
|
|
|
277
312
|
if index >= len(string):
|
|
278
313
|
if start < end:
|
|
279
314
|
num = str(chinese_string_to_num(string[start:end]))
|
|
280
|
-
num_str = str(num) +
|
|
315
|
+
num_str = str(num) + " "
|
|
281
316
|
string = string[:start] + num_str + string[end:]
|
|
282
317
|
|
|
283
318
|
break
|
|
@@ -285,9 +320,7 @@ def replace_word_tokens_simplified_chinese(
|
|
|
285
320
|
return string
|
|
286
321
|
|
|
287
322
|
|
|
288
|
-
def replace_word_tokens(
|
|
289
|
-
string: str, language: str, stopwords: set[str] = None
|
|
290
|
-
) -> str:
|
|
323
|
+
def replace_word_tokens(string: str, language: str, stopwords: set[str] = None) -> str:
|
|
291
324
|
"""
|
|
292
325
|
Replace word-based mathematical terms with their symbolic equivalents.
|
|
293
326
|
|
|
@@ -312,15 +345,15 @@ def replace_word_tokens(
|
|
|
312
345
|
words = mathwords.word_groups_for_language(language)
|
|
313
346
|
|
|
314
347
|
# Process operators by length (longest first) to handle compound operators
|
|
315
|
-
binary_operators = words[
|
|
348
|
+
binary_operators = words["binary_operators"]
|
|
316
349
|
sorted_operators = sorted(binary_operators.keys(), key=len, reverse=True)
|
|
317
350
|
for operator in sorted_operators:
|
|
318
351
|
if operator in string:
|
|
319
352
|
string = string.replace(operator, binary_operators[operator])
|
|
320
353
|
|
|
321
354
|
# Handle prefix unary operators (like "square root of")
|
|
322
|
-
if
|
|
323
|
-
prefix_unary_operators = words[
|
|
355
|
+
if "prefix_unary_operators" in words:
|
|
356
|
+
prefix_unary_operators = words["prefix_unary_operators"]
|
|
324
357
|
# Sort by length to handle compound operators where a shorter token
|
|
325
358
|
# is a substring of a longer token
|
|
326
359
|
sorted_prefix_operators = sorted(
|
|
@@ -328,13 +361,11 @@ def replace_word_tokens(
|
|
|
328
361
|
)
|
|
329
362
|
for operator in sorted_prefix_operators:
|
|
330
363
|
if operator in string:
|
|
331
|
-
string = string.replace(
|
|
332
|
-
operator, prefix_unary_operators[operator]
|
|
333
|
-
)
|
|
364
|
+
string = string.replace(operator, prefix_unary_operators[operator])
|
|
334
365
|
|
|
335
366
|
# Handle postfix unary operators (like "squared", "cubed")
|
|
336
|
-
if
|
|
337
|
-
postfix_unary_operators = words[
|
|
367
|
+
if "postfix_unary_operators" in words:
|
|
368
|
+
postfix_unary_operators = words["postfix_unary_operators"]
|
|
338
369
|
# Sort by length to handle compound operators where a shorter token
|
|
339
370
|
# is a substring of a longer token
|
|
340
371
|
sorted_postfix_operators = sorted(
|
|
@@ -343,38 +374,42 @@ def replace_word_tokens(
|
|
|
343
374
|
for operator in sorted_postfix_operators:
|
|
344
375
|
if operator in string:
|
|
345
376
|
# Captures the number/operand before the unary operator
|
|
346
|
-
pattern = r
|
|
347
|
-
replacement = r
|
|
377
|
+
pattern = r"(\w+)\s+" + re.escape(operator)
|
|
378
|
+
replacement = r"(\1 " + postfix_unary_operators[operator] + ")"
|
|
348
379
|
string = re.sub(pattern, replacement, string)
|
|
349
380
|
|
|
350
381
|
# Handle compound numbers:
|
|
351
382
|
# (e.g., "twenty one" -> "(20 + 1)", "fifty four" -> "(50 + 4)")
|
|
352
|
-
numbers = words[
|
|
383
|
+
numbers = words["numbers"]
|
|
353
384
|
|
|
354
385
|
# Create regex patterns for compound numbers
|
|
355
386
|
# Pattern matches: (tens_word) (units_word) where tens_word is
|
|
356
387
|
# 20,30,40,...,90 and units_word is 1-9
|
|
357
|
-
tens_words = {
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
388
|
+
tens_words = {
|
|
389
|
+
word: value
|
|
390
|
+
for word, value in numbers.items()
|
|
391
|
+
if value in [20, 30, 40, 50, 60, 70, 80, 90]
|
|
392
|
+
}
|
|
393
|
+
units_words = {
|
|
394
|
+
word: value
|
|
395
|
+
for word, value in numbers.items()
|
|
396
|
+
if value in [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
397
|
+
}
|
|
363
398
|
|
|
364
399
|
# Preprocess hyphenated compound numbers
|
|
365
400
|
# (e.g., "fifty-four" -> "fifty four")
|
|
366
401
|
for tens_word in tens_words.keys():
|
|
367
402
|
for units_word in units_words.keys():
|
|
368
|
-
hyphenated_pattern = tens_word + r
|
|
369
|
-
space_separated = tens_word +
|
|
403
|
+
hyphenated_pattern = tens_word + r"-" + units_word
|
|
404
|
+
space_separated = tens_word + " " + units_word
|
|
370
405
|
if re.search(hyphenated_pattern, string):
|
|
371
406
|
string = re.sub(hyphenated_pattern, space_separated, string)
|
|
372
407
|
|
|
373
408
|
# Replace compound numbers first (before individual number replacement)
|
|
374
409
|
for tens_word, tens_value in tens_words.items():
|
|
375
410
|
for units_word, units_value in units_words.items():
|
|
376
|
-
compound_pattern = tens_word + r
|
|
377
|
-
compound_replacement = f
|
|
411
|
+
compound_pattern = tens_word + r"\s+" + units_word
|
|
412
|
+
compound_replacement = f"({tens_value} + {units_value})"
|
|
378
413
|
if re.search(compound_pattern, string):
|
|
379
414
|
string = re.sub(compound_pattern, compound_replacement, string)
|
|
380
415
|
|
|
@@ -394,16 +429,16 @@ def replace_word_tokens(
|
|
|
394
429
|
for word in string.split():
|
|
395
430
|
if word not in stopwords:
|
|
396
431
|
filtered_words.append(word)
|
|
397
|
-
string =
|
|
432
|
+
string = " ".join(filtered_words)
|
|
398
433
|
|
|
399
434
|
# Replace scaling multipliers with numeric values
|
|
400
|
-
scales = words[
|
|
401
|
-
end_index_characters = mathwords.BINARY_OPERATORS | {
|
|
435
|
+
scales = words["scales"]
|
|
436
|
+
end_index_characters = mathwords.BINARY_OPERATORS | {"("}
|
|
402
437
|
|
|
403
438
|
word_matches = find_word_groups(string, frozenset(scales.keys()))
|
|
404
439
|
|
|
405
440
|
for match in word_matches:
|
|
406
|
-
string = string.replace(match,
|
|
441
|
+
string = string.replace(match, "(" + match + ")")
|
|
407
442
|
|
|
408
443
|
for scale in frozenset(scales.keys()):
|
|
409
444
|
for _ in range(0, string.count(scale)):
|
|
@@ -413,24 +448,21 @@ def replace_word_tokens(
|
|
|
413
448
|
while is_int(string[start_index - 1]) and start_index > 0:
|
|
414
449
|
start_index -= 1
|
|
415
450
|
|
|
416
|
-
end_index = string.find(
|
|
417
|
-
end_index = string.find(
|
|
451
|
+
end_index = string.find(" ", start_index) + 1
|
|
452
|
+
end_index = string.find(" ", end_index) + 1
|
|
418
453
|
|
|
419
|
-
add =
|
|
454
|
+
add = " + "
|
|
420
455
|
if string[end_index] in end_index_characters:
|
|
421
|
-
add =
|
|
456
|
+
add = ""
|
|
422
457
|
|
|
423
|
-
string = string[:start_index] +
|
|
424
|
-
string = string.replace(
|
|
425
|
-
scale, '* ' + str(scales[scale]) + ')' + add,
|
|
426
|
-
1
|
|
427
|
-
)
|
|
458
|
+
string = string[:start_index] + "(" + string[start_index:]
|
|
459
|
+
string = string.replace(scale, "* " + str(scales[scale]) + ")" + add, 1)
|
|
428
460
|
|
|
429
|
-
string = string.replace(
|
|
461
|
+
string = string.replace(") (", ") + (")
|
|
430
462
|
|
|
431
463
|
# Remove extra parentheses around all scale expressions to simplify output
|
|
432
464
|
# Convert ((number * scale)) to (number * scale) for all scales
|
|
433
|
-
string = re.sub(r
|
|
465
|
+
string = re.sub(r"\(\((\d+\s*\*\s*\d+)\)\)", r"(\1)", string)
|
|
434
466
|
|
|
435
467
|
return string
|
|
436
468
|
|
|
@@ -451,10 +483,10 @@ def preprocess_unary_operators(tokens: list) -> list:
|
|
|
451
483
|
processed_tokens = []
|
|
452
484
|
|
|
453
485
|
# A following minus sign should be treated as unary (negative)
|
|
454
|
-
unary_contexts = mathwords.BINARY_OPERATORS | {
|
|
486
|
+
unary_contexts = mathwords.BINARY_OPERATORS | {"("}
|
|
455
487
|
|
|
456
488
|
for i, token in enumerate(tokens):
|
|
457
|
-
if token ==
|
|
489
|
+
if token == "-":
|
|
458
490
|
# Check if this minus should be treated as unary
|
|
459
491
|
is_unary_minus = False
|
|
460
492
|
|
|
@@ -470,7 +502,7 @@ def preprocess_unary_operators(tokens: list) -> list:
|
|
|
470
502
|
|
|
471
503
|
if is_unary_minus:
|
|
472
504
|
# Convert the unary minus to 'neg' function
|
|
473
|
-
processed_tokens.append(
|
|
505
|
+
processed_tokens.append("neg")
|
|
474
506
|
else:
|
|
475
507
|
# Keep as binary minus
|
|
476
508
|
processed_tokens.append(token)
|
|
@@ -485,14 +517,17 @@ def to_postfix(tokens: list) -> list:
|
|
|
485
517
|
Convert a list of evaluatable tokens to postfix format.
|
|
486
518
|
"""
|
|
487
519
|
precedence = {
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
520
|
+
".": 5,
|
|
521
|
+
"^": 4,
|
|
522
|
+
"/": 3,
|
|
523
|
+
"*": 3,
|
|
524
|
+
"%": 3,
|
|
525
|
+
"+": 2,
|
|
526
|
+
"-": 2,
|
|
527
|
+
# Commas separate function arguments, so every other operator within
|
|
528
|
+
# an argument is evaluated first
|
|
529
|
+
",": 1,
|
|
530
|
+
"(": 0,
|
|
496
531
|
}
|
|
497
532
|
|
|
498
533
|
# Unary functions have a higher precedence than binary operators
|
|
@@ -500,12 +535,12 @@ def to_postfix(tokens: list) -> list:
|
|
|
500
535
|
|
|
501
536
|
# Right-associative operators use strict inequality when popping,
|
|
502
537
|
# so that e.g. 2^3^2 evaluates as 2^(3^2) = 512, not (2^3)^2 = 64
|
|
503
|
-
right_associative = {
|
|
538
|
+
right_associative = {"^"}
|
|
504
539
|
|
|
505
540
|
postfix = []
|
|
506
541
|
opstack = []
|
|
507
542
|
|
|
508
|
-
for token in tokens:
|
|
543
|
+
for index, token in enumerate(tokens):
|
|
509
544
|
if is_int(token):
|
|
510
545
|
postfix.append(token)
|
|
511
546
|
elif is_float(token):
|
|
@@ -514,11 +549,11 @@ def to_postfix(tokens: list) -> list:
|
|
|
514
549
|
postfix.append(token)
|
|
515
550
|
elif is_unary(token):
|
|
516
551
|
opstack.append(token)
|
|
517
|
-
elif token ==
|
|
552
|
+
elif token == "(":
|
|
518
553
|
opstack.append(token)
|
|
519
|
-
elif token ==
|
|
554
|
+
elif token == ")":
|
|
520
555
|
top_token = opstack.pop()
|
|
521
|
-
while top_token !=
|
|
556
|
+
while top_token != "(":
|
|
522
557
|
postfix.append(top_token)
|
|
523
558
|
top_token = opstack.pop()
|
|
524
559
|
elif is_binary(token):
|
|
@@ -527,19 +562,28 @@ def to_postfix(tokens: list) -> list:
|
|
|
527
562
|
# operators like ^ only yield to strictly higher precedence so
|
|
528
563
|
# that 2^3^2 = 2^(3^2) = 512 rather than (2^3)^2 = 64).
|
|
529
564
|
is_left_assoc = token not in right_associative
|
|
565
|
+
|
|
566
|
+
# A decimal point directly after a number is part of that number
|
|
567
|
+
# (e.g. "round 2.5" is round(2.5), not round(2) . 5), so it must
|
|
568
|
+
# not cause a preceding unary function to be applied early
|
|
569
|
+
is_decimal_part = token == "." and index > 0 and tokens[index - 1] != ")"
|
|
570
|
+
|
|
530
571
|
while (opstack != []) and (
|
|
531
572
|
(
|
|
532
|
-
opstack[-1] in precedence
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
573
|
+
opstack[-1] in precedence
|
|
574
|
+
and token in precedence
|
|
575
|
+
and (
|
|
576
|
+
precedence[opstack[-1]] > precedence[token]
|
|
577
|
+
or (
|
|
578
|
+
is_left_assoc
|
|
579
|
+
and precedence[opstack[-1]] == precedence[token]
|
|
536
580
|
)
|
|
537
581
|
)
|
|
538
|
-
)
|
|
539
|
-
(
|
|
540
|
-
is_unary(
|
|
541
|
-
|
|
542
|
-
|
|
582
|
+
)
|
|
583
|
+
or (
|
|
584
|
+
is_unary(opstack[-1])
|
|
585
|
+
and not is_decimal_part
|
|
586
|
+
and unary_precedence >= precedence[token]
|
|
543
587
|
)
|
|
544
588
|
):
|
|
545
589
|
postfix.append(opstack.pop())
|
|
@@ -572,36 +616,77 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
|
572
616
|
a = stack.pop()
|
|
573
617
|
# Convert token (string) to number for unary function evaluation
|
|
574
618
|
a = to_number(a)
|
|
575
|
-
|
|
619
|
+
function = mathwords.UNARY_FUNCTIONS[token]
|
|
620
|
+
# Check the size of n! (using log(n!) = lgamma(n + 1)) before
|
|
621
|
+
# computing it, since a large factorial is slow to calculate
|
|
622
|
+
if function is math.factorial and isinstance(a, int) and a > 1:
|
|
623
|
+
check_integer_digits(math.lgamma(a + 1) / math.log(10), token)
|
|
624
|
+
if isinstance(a, mathwords.ArgumentList) and not getattr(
|
|
625
|
+
function, "variadic", False
|
|
626
|
+
):
|
|
627
|
+
raise PostfixTokenEvaluationException(
|
|
628
|
+
'Function "{}" takes a single argument'.format(token)
|
|
629
|
+
)
|
|
630
|
+
total = function(a)
|
|
576
631
|
elif len(stack) == 1:
|
|
577
632
|
raise PostfixTokenEvaluationException(
|
|
578
|
-
'Insufficient values in expression for operator "{}"'.format(
|
|
579
|
-
token
|
|
580
|
-
)
|
|
633
|
+
'Insufficient values in expression for operator "{}"'.format(token)
|
|
581
634
|
)
|
|
582
635
|
elif len(stack) > 1:
|
|
583
636
|
b = stack.pop()
|
|
584
637
|
a = stack.pop()
|
|
585
638
|
|
|
586
|
-
if token
|
|
639
|
+
if token != "," and (
|
|
640
|
+
isinstance(a, mathwords.ArgumentList)
|
|
641
|
+
or isinstance(b, mathwords.ArgumentList)
|
|
642
|
+
):
|
|
643
|
+
raise PostfixTokenEvaluationException(
|
|
644
|
+
"Comma separated values can only be used as function "
|
|
645
|
+
"arguments, such as max(3, 7)"
|
|
646
|
+
)
|
|
647
|
+
|
|
648
|
+
if token == ",":
|
|
649
|
+
# Collect comma separated function arguments into a list
|
|
650
|
+
# Example: 3 , 7 , 5 = [3, 7, 5]
|
|
651
|
+
if isinstance(a, mathwords.ArgumentList):
|
|
652
|
+
total = a
|
|
653
|
+
else:
|
|
654
|
+
total = mathwords.ArgumentList([to_number(a)])
|
|
655
|
+
total.append(to_number(b))
|
|
656
|
+
elif token == "+":
|
|
587
657
|
total = to_number(a) + to_number(b)
|
|
588
|
-
elif token ==
|
|
658
|
+
elif token == "-":
|
|
589
659
|
total = to_number(a) - to_number(b)
|
|
590
|
-
elif token ==
|
|
660
|
+
elif token == "*":
|
|
591
661
|
total = to_number(a) * to_number(b)
|
|
592
|
-
elif token ==
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
662
|
+
elif token == "^":
|
|
663
|
+
base = to_number(a)
|
|
664
|
+
exponent = to_number(b)
|
|
665
|
+
# Check the size of an integer power before computing it,
|
|
666
|
+
# since a huge power (e.g. 999999 ^ 999999999) would use all
|
|
667
|
+
# available memory. Float and Decimal powers instead raise an
|
|
668
|
+
# overflow error quickly.
|
|
669
|
+
if (
|
|
670
|
+
isinstance(base, int)
|
|
671
|
+
and isinstance(exponent, int)
|
|
672
|
+
and exponent > 0
|
|
673
|
+
and abs(base) > 1
|
|
674
|
+
):
|
|
675
|
+
check_integer_digits(exponent * math.log10(abs(base)), token)
|
|
676
|
+
total = base**exponent
|
|
677
|
+
elif token == "/":
|
|
678
|
+
# Convert tokens such as constants (e.g. "pi") to numbers
|
|
679
|
+
# before passing them to Decimal
|
|
680
|
+
if to_number(b) == 0:
|
|
681
|
+
total = "undefined"
|
|
597
682
|
else:
|
|
598
|
-
total = Decimal(str(a)) / Decimal(str(b))
|
|
599
|
-
elif token ==
|
|
600
|
-
if
|
|
601
|
-
total =
|
|
683
|
+
total = Decimal(str(to_number(a))) / Decimal(str(to_number(b)))
|
|
684
|
+
elif token == "%":
|
|
685
|
+
if to_number(b) == 0:
|
|
686
|
+
total = "undefined"
|
|
602
687
|
else:
|
|
603
688
|
total = to_number(a) % to_number(b)
|
|
604
|
-
elif token ==
|
|
689
|
+
elif token == ".":
|
|
605
690
|
# Treat decimal points as a binary operator that combines the
|
|
606
691
|
# integer and fractional part of two numbers
|
|
607
692
|
# Example: 53 . 25 = 53.25, -3 . 5 = -3.5
|
|
@@ -615,7 +700,7 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
|
615
700
|
total = Decimal(numeric_a)
|
|
616
701
|
else:
|
|
617
702
|
# Check if 'a' has a negative sign (handles -0 case)
|
|
618
|
-
is_negative = str(a).startswith(
|
|
703
|
+
is_negative = str(a).startswith("-")
|
|
619
704
|
|
|
620
705
|
# Convert a to number for calculation
|
|
621
706
|
numeric_a = to_number(a)
|
|
@@ -623,7 +708,7 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
|
623
708
|
# Count the digits in the original string b to preserve
|
|
624
709
|
# leading zeros (e.g., "01" has 2 digits, not 1)
|
|
625
710
|
digits = len(str(b))
|
|
626
|
-
divisor = 10
|
|
711
|
+
divisor = 10**digits
|
|
627
712
|
fractional_part = numeric_b / divisor
|
|
628
713
|
|
|
629
714
|
# Handle negatives: -3 . 5 = -3.5, not -2.5
|
|
@@ -637,28 +722,38 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
|
637
722
|
'Unknown token "{}"'.format(token)
|
|
638
723
|
)
|
|
639
724
|
|
|
725
|
+
# Limit the size of every computed integer, which also prevents
|
|
726
|
+
# repeated operations such as multiplication from growing a number
|
|
727
|
+
# without bound
|
|
728
|
+
if isinstance(total, int) and total:
|
|
729
|
+
check_integer_digits((total.bit_length() - 1) * math.log10(2), token)
|
|
730
|
+
|
|
640
731
|
if total is not None:
|
|
641
732
|
stack.append(total)
|
|
642
733
|
|
|
643
734
|
# If the stack is empty the tokens could not be evaluated
|
|
644
735
|
if not stack:
|
|
645
736
|
raise PostfixTokenEvaluationException(
|
|
646
|
-
|
|
737
|
+
"The postfix expression resulted in an empty stack"
|
|
647
738
|
)
|
|
648
739
|
|
|
649
740
|
result = stack.pop()
|
|
650
741
|
|
|
651
|
-
|
|
742
|
+
if isinstance(result, mathwords.ArgumentList):
|
|
743
|
+
raise PostfixTokenEvaluationException(
|
|
744
|
+
"Comma separated values can only be used as function arguments, "
|
|
745
|
+
"such as max(3, 7)"
|
|
746
|
+
)
|
|
747
|
+
|
|
748
|
+
# Convert final result from string (such as "4" or a constant like "pi")
|
|
749
|
+
# to a number if needed
|
|
652
750
|
if isinstance(result, str):
|
|
653
|
-
|
|
654
|
-
result = int(result)
|
|
655
|
-
elif is_float(result):
|
|
656
|
-
result = float(result)
|
|
751
|
+
result = to_number(result)
|
|
657
752
|
|
|
658
753
|
return result
|
|
659
754
|
|
|
660
755
|
|
|
661
|
-
def tokenize(string: str, language: str = None, escape: str =
|
|
756
|
+
def tokenize(string: str, language: str = None, escape: str = "___") -> list:
|
|
662
757
|
"""
|
|
663
758
|
Convert a string into a list of mathematical tokens for processing.
|
|
664
759
|
|
|
@@ -685,7 +780,7 @@ def tokenize(string: str, language: str = None, escape: str = '___') -> list:
|
|
|
685
780
|
# Ignore punctuation
|
|
686
781
|
if len(string) and not string[-1].isalnum():
|
|
687
782
|
character = string[-1]
|
|
688
|
-
string = string[:-1] +
|
|
783
|
+
string = string[:-1] + " " + character
|
|
689
784
|
|
|
690
785
|
# If language is specified, normalize compound operators by removing
|
|
691
786
|
# spaces between their characters. This handles cases like '乘 以'
|
|
@@ -704,18 +799,18 @@ def tokenize(string: str, language: str = None, escape: str = '___') -> list:
|
|
|
704
799
|
if len(phrase) > 1:
|
|
705
800
|
# Create pattern with optional spaces between each character
|
|
706
801
|
# For example, '乘以' could appear as '乘 以' or '乘 以'
|
|
707
|
-
spaced_phrase =
|
|
802
|
+
spaced_phrase = " ".join(phrase)
|
|
708
803
|
# Replace spaced version with non-spaced version
|
|
709
804
|
string = string.replace(spaced_phrase, phrase)
|
|
710
805
|
|
|
711
806
|
# Python style power operator is an alias for '^'. This must be replaced
|
|
712
807
|
# before '*' is split apart as a binary operator below.
|
|
713
|
-
string = string.replace(
|
|
808
|
+
string = string.replace("**", "^")
|
|
714
809
|
|
|
715
810
|
# Binary operators must have space around them to be tokenized properly
|
|
716
811
|
# Special handling for minus sign: preserve leading negatives
|
|
717
812
|
for operator in mathwords.BINARY_OPERATORS:
|
|
718
|
-
if operator ==
|
|
813
|
+
if operator == "-":
|
|
719
814
|
# For minus sign, use a pattern that only spaces it when it's
|
|
720
815
|
# clearly a binary operator (after digits or closing parenthesis).
|
|
721
816
|
# This preserves leading negatives like "-3" and distinguishes
|
|
@@ -726,25 +821,30 @@ def tokenize(string: str, language: str = None, escape: str = '___') -> list:
|
|
|
726
821
|
# operator)
|
|
727
822
|
# Only add spaces around minus when preceded by a digit or closing
|
|
728
823
|
# parenthesis
|
|
729
|
-
string = re.sub(r
|
|
824
|
+
string = re.sub(r"([\d)])\s*-\s*", r"\1 - ", string)
|
|
825
|
+
# Separate a leading minus from a following constant or function
|
|
826
|
+
# name (e.g. "-pi" or "-cos 0") so that it becomes a unary minus.
|
|
827
|
+
# A minus directly after a word character (as in "twenty-one") is
|
|
828
|
+
# left alone.
|
|
829
|
+
string = re.sub(r"(?<![\w.])-(?=[^\W\d_])", "- ", string)
|
|
730
830
|
else:
|
|
731
|
-
string = string.replace(operator, f
|
|
831
|
+
string = string.replace(operator, f" {operator} ")
|
|
732
832
|
|
|
733
833
|
# Parenthesis must have space around them to be tokenized properly
|
|
734
|
-
string = string.replace(
|
|
735
|
-
string = string.replace(
|
|
834
|
+
string = string.replace("(", " ( ")
|
|
835
|
+
string = string.replace(")", " ) ")
|
|
736
836
|
|
|
737
837
|
if language:
|
|
738
838
|
words = mathwords.words_for_language(language)
|
|
739
839
|
|
|
740
840
|
for phrase in words:
|
|
741
|
-
escaped_phrase = phrase.replace(
|
|
841
|
+
escaped_phrase = phrase.replace(" ", escape)
|
|
742
842
|
string = string.replace(phrase, escaped_phrase)
|
|
743
843
|
|
|
744
844
|
tokens = string.split()
|
|
745
845
|
|
|
746
846
|
for index, token in enumerate(tokens):
|
|
747
|
-
tokens[index] = token.replace(escape,
|
|
847
|
+
tokens[index] = token.replace(escape, " ")
|
|
748
848
|
|
|
749
849
|
return tokens
|
|
750
850
|
|
|
@@ -809,12 +909,17 @@ def parse(
|
|
|
809
909
|
- Supports unary functions: sqrt, log
|
|
810
910
|
- Each expression must use terms from a single language
|
|
811
911
|
- Division by zero returns 'undefined' instead of raising an exception
|
|
912
|
+
- Raises ExpressionTooLargeException if the expression is longer than
|
|
913
|
+
MAX_EXPRESSION_LENGTH, or a result would be too large to compute
|
|
812
914
|
"""
|
|
915
|
+
if MAX_EXPRESSION_LENGTH is not None and len(string) > MAX_EXPRESSION_LENGTH:
|
|
916
|
+
raise ExpressionTooLargeException(
|
|
917
|
+
"The expression is longer than {} characters".format(MAX_EXPRESSION_LENGTH)
|
|
918
|
+
)
|
|
919
|
+
|
|
813
920
|
if language:
|
|
814
|
-
if language ==
|
|
815
|
-
string = replace_word_tokens_simplified_chinese(
|
|
816
|
-
string, stopwords
|
|
817
|
-
)
|
|
921
|
+
if language == "CHI":
|
|
922
|
+
string = replace_word_tokens_simplified_chinese(string, stopwords)
|
|
818
923
|
else:
|
|
819
924
|
string = replace_word_tokens(string, language, stopwords)
|
|
820
925
|
|
|
@@ -822,7 +927,14 @@ def parse(
|
|
|
822
927
|
tokens = preprocess_unary_operators(tokens)
|
|
823
928
|
postfix = to_postfix(tokens)
|
|
824
929
|
|
|
825
|
-
|
|
930
|
+
try:
|
|
931
|
+
return evaluate_postfix(postfix)
|
|
932
|
+
except (OverflowError, DecimalException) as error:
|
|
933
|
+
# Float and Decimal operations raise these when a result is too large
|
|
934
|
+
# (e.g. 10.0 ^ 400 or exp(1000))
|
|
935
|
+
raise ExpressionTooLargeException(
|
|
936
|
+
"The result is too large to compute: {}".format(error)
|
|
937
|
+
) from error
|
|
826
938
|
|
|
827
939
|
|
|
828
940
|
def extract_expression(dirty_string: str, language: str) -> str:
|
|
@@ -875,7 +987,7 @@ def extract_expression(dirty_string: str, language: str) -> str:
|
|
|
875
987
|
# operator. Binary operators (except '(') are only valid at the
|
|
876
988
|
# start if they are unary (such as a leading minus for a negative
|
|
877
989
|
# number)
|
|
878
|
-
if part in mathwords.BINARY_OPERATORS and part !=
|
|
990
|
+
if part in mathwords.BINARY_OPERATORS and part != "(":
|
|
879
991
|
# For a binary operator to be the start of an expression, it
|
|
880
992
|
# must be:
|
|
881
993
|
# 1. At the very beginning (position 0) - could be unary minus
|
|
@@ -896,9 +1008,12 @@ def extract_expression(dirty_string: str, language: str) -> str:
|
|
|
896
1008
|
if all_prev_non_math and i + 1 < len(tokens):
|
|
897
1009
|
next_token = tokens[i + 1]
|
|
898
1010
|
if (
|
|
899
|
-
is_int(next_token)
|
|
900
|
-
|
|
901
|
-
|
|
1011
|
+
is_int(next_token)
|
|
1012
|
+
or is_float(next_token)
|
|
1013
|
+
or is_constant(next_token)
|
|
1014
|
+
or is_unary(next_token)
|
|
1015
|
+
or next_token == "("
|
|
1016
|
+
or is_word(next_token, language)
|
|
902
1017
|
):
|
|
903
1018
|
start_index = i
|
|
904
1019
|
break
|
|
@@ -914,10 +1029,10 @@ def extract_expression(dirty_string: str, language: str) -> str:
|
|
|
914
1029
|
else:
|
|
915
1030
|
end_index -= 1
|
|
916
1031
|
|
|
917
|
-
result =
|
|
1032
|
+
result = " ".join(tokens[start_index:end_index])
|
|
918
1033
|
|
|
919
1034
|
# Remove spaces around decimal points for cleaner output
|
|
920
1035
|
# Replace " . " with "." to convert "-100 . 5" to "-100.5"
|
|
921
|
-
result = result.replace(
|
|
1036
|
+
result = result.replace(" . ", ".")
|
|
922
1037
|
|
|
923
1038
|
return result
|
|
@@ -2,9 +2,12 @@
|
|
|
2
2
|
Utility methods for getting math word terms.
|
|
3
3
|
"""
|
|
4
4
|
import math
|
|
5
|
+
from decimal import Decimal, ROUND_HALF_UP
|
|
5
6
|
|
|
6
7
|
BINARY_OPERATORS = {
|
|
7
|
-
'^', '*', '/', '%', '+', '-', '.'
|
|
8
|
+
'^', '*', '/', '%', '+', '-', '.',
|
|
9
|
+
# Separates the arguments of functions such as max(3, 7)
|
|
10
|
+
','
|
|
8
11
|
}
|
|
9
12
|
|
|
10
13
|
# Each key is an ISO 639-2 language code
|
|
@@ -1676,6 +1679,30 @@ CONSTANTS = {
|
|
|
1676
1679
|
}
|
|
1677
1680
|
|
|
1678
1681
|
|
|
1682
|
+
class ArgumentList(list):
|
|
1683
|
+
"""
|
|
1684
|
+
The comma separated arguments of a function call, such as the "3, 7" in
|
|
1685
|
+
"max(3, 7)". Created by evaluating the ',' binary operator.
|
|
1686
|
+
"""
|
|
1687
|
+
pass
|
|
1688
|
+
|
|
1689
|
+
|
|
1690
|
+
def variadic(function):
|
|
1691
|
+
"""
|
|
1692
|
+
Wrap a function that takes an iterable of values (such as min or max) so
|
|
1693
|
+
that it can be called with either an ArgumentList or a single value.
|
|
1694
|
+
"""
|
|
1695
|
+
def wrapper(x):
|
|
1696
|
+
if isinstance(x, ArgumentList):
|
|
1697
|
+
return function(x)
|
|
1698
|
+
return function([x])
|
|
1699
|
+
|
|
1700
|
+
# Marks the function as accepting an ArgumentList
|
|
1701
|
+
wrapper.variadic = True
|
|
1702
|
+
|
|
1703
|
+
return wrapper
|
|
1704
|
+
|
|
1705
|
+
|
|
1679
1706
|
UNARY_FUNCTIONS = {
|
|
1680
1707
|
# Roots
|
|
1681
1708
|
'sqrt': math.sqrt,
|
|
@@ -1728,6 +1755,15 @@ UNARY_FUNCTIONS = {
|
|
|
1728
1755
|
'ceil': math.ceil,
|
|
1729
1756
|
'floor': math.floor,
|
|
1730
1757
|
'trunc': math.trunc,
|
|
1758
|
+
# Round halves away from zero (2.5 -> 3, -2.5 -> -3) as is commonly
|
|
1759
|
+
# expected, rather than Python's round half to even (2.5 -> 2)
|
|
1760
|
+
'round': lambda x: int(
|
|
1761
|
+
Decimal(str(x)).quantize(Decimal('1'), rounding=ROUND_HALF_UP)
|
|
1762
|
+
),
|
|
1763
|
+
|
|
1764
|
+
# Functions of one or more comma separated arguments
|
|
1765
|
+
'min': variadic(min),
|
|
1766
|
+
'max': variadic(max),
|
|
1731
1767
|
|
|
1732
1768
|
# Special functions
|
|
1733
1769
|
'factorial': math.factorial,
|
|
@@ -255,3 +255,44 @@ class DoubleStarPowerTestCase(TestCase):
|
|
|
255
255
|
|
|
256
256
|
def test_power_with_parenthesis(self):
|
|
257
257
|
self.assertEqual(mathparse.parse('(10 % 4) ** 2'), 4)
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
class ConstantDivisionTestCase(TestCase):
|
|
261
|
+
|
|
262
|
+
def test_constant_numerator(self):
|
|
263
|
+
result = mathparse.parse('pi / 2')
|
|
264
|
+
self.assertAlmostEqual(float(result), mathwords.CONSTANTS['pi'] / 2)
|
|
265
|
+
|
|
266
|
+
def test_constant_denominator(self):
|
|
267
|
+
result = mathparse.parse('2 / pi')
|
|
268
|
+
self.assertAlmostEqual(float(result), 2 / mathwords.CONSTANTS['pi'])
|
|
269
|
+
|
|
270
|
+
def test_constant_modulo(self):
|
|
271
|
+
result = mathparse.parse('10 % pi')
|
|
272
|
+
self.assertAlmostEqual(float(result), 10 % mathwords.CONSTANTS['pi'])
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
class ConstantResultTestCase(TestCase):
|
|
276
|
+
|
|
277
|
+
def test_pi(self):
|
|
278
|
+
self.assertEqual(mathparse.parse('pi'), mathwords.CONSTANTS['pi'])
|
|
279
|
+
|
|
280
|
+
def test_e(self):
|
|
281
|
+
self.assertEqual(mathparse.parse('e'), mathwords.CONSTANTS['e'])
|
|
282
|
+
|
|
283
|
+
def test_constant_in_parenthesis(self):
|
|
284
|
+
self.assertEqual(mathparse.parse('(pi)'), mathwords.CONSTANTS['pi'])
|
|
285
|
+
|
|
286
|
+
def test_negative_constant(self):
|
|
287
|
+
self.assertEqual(mathparse.parse('-pi'), -mathwords.CONSTANTS['pi'])
|
|
288
|
+
|
|
289
|
+
def test_negative_constant_after_operator(self):
|
|
290
|
+
result = mathparse.parse('2 * -pi')
|
|
291
|
+
self.assertEqual(result, -2 * mathwords.CONSTANTS['pi'])
|
|
292
|
+
|
|
293
|
+
def test_negative_function(self):
|
|
294
|
+
self.assertEqual(mathparse.parse('-sqrt 4'), -2.0)
|
|
295
|
+
|
|
296
|
+
def test_hyphenated_number_word_is_unaffected(self):
|
|
297
|
+
result = mathparse.parse('twenty-one plus one', language='ENG')
|
|
298
|
+
self.assertEqual(result, 22)
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
from unittest import TestCase
|
|
2
|
+
from mathparse import mathparse
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class ExpressionLimitTestCase(TestCase):
|
|
6
|
+
|
|
7
|
+
def setUp(self):
|
|
8
|
+
self.max_expression_length = mathparse.MAX_EXPRESSION_LENGTH
|
|
9
|
+
self.max_integer_digits = mathparse.MAX_INTEGER_DIGITS
|
|
10
|
+
|
|
11
|
+
def tearDown(self):
|
|
12
|
+
mathparse.MAX_EXPRESSION_LENGTH = self.max_expression_length
|
|
13
|
+
mathparse.MAX_INTEGER_DIGITS = self.max_integer_digits
|
|
14
|
+
|
|
15
|
+
def test_huge_power(self):
|
|
16
|
+
with self.assertRaises(mathparse.ExpressionTooLargeException):
|
|
17
|
+
mathparse.parse('999999**999999999')
|
|
18
|
+
|
|
19
|
+
def test_huge_power_tower(self):
|
|
20
|
+
with self.assertRaises(mathparse.ExpressionTooLargeException):
|
|
21
|
+
mathparse.parse('9 ^ 9 ^ 9')
|
|
22
|
+
|
|
23
|
+
def test_power_at_limit(self):
|
|
24
|
+
# 10 ^ 999 has exactly 1000 digits
|
|
25
|
+
self.assertEqual(mathparse.parse('10 ^ 999'), 10 ** 999)
|
|
26
|
+
|
|
27
|
+
def test_power_over_limit(self):
|
|
28
|
+
with self.assertRaises(mathparse.ExpressionTooLargeException):
|
|
29
|
+
mathparse.parse('10 ^ 1000')
|
|
30
|
+
|
|
31
|
+
def test_power_of_one_is_not_limited(self):
|
|
32
|
+
self.assertEqual(mathparse.parse('1 ^ 999999999'), 1)
|
|
33
|
+
|
|
34
|
+
def test_huge_factorial(self):
|
|
35
|
+
with self.assertRaises(mathparse.ExpressionTooLargeException):
|
|
36
|
+
mathparse.parse('factorial 10000000')
|
|
37
|
+
|
|
38
|
+
def test_repeated_multiplication(self):
|
|
39
|
+
with self.assertRaises(mathparse.ExpressionTooLargeException):
|
|
40
|
+
mathparse.parse('(2 ^ 3000) * (2 ^ 3000)')
|
|
41
|
+
|
|
42
|
+
def test_float_overflow(self):
|
|
43
|
+
with self.assertRaises(mathparse.ExpressionTooLargeException):
|
|
44
|
+
mathparse.parse('1.5 ** 999999999')
|
|
45
|
+
|
|
46
|
+
def test_function_overflow(self):
|
|
47
|
+
with self.assertRaises(mathparse.ExpressionTooLargeException):
|
|
48
|
+
mathparse.parse('exp(1000)')
|
|
49
|
+
|
|
50
|
+
def test_long_expression(self):
|
|
51
|
+
with self.assertRaises(mathparse.ExpressionTooLargeException):
|
|
52
|
+
mathparse.parse('1' + ' + 1' * 500)
|
|
53
|
+
|
|
54
|
+
def test_is_postfix_evaluation_exception(self):
|
|
55
|
+
# Existing handlers for evaluation errors also catch size errors
|
|
56
|
+
with self.assertRaises(mathparse.PostfixTokenEvaluationException):
|
|
57
|
+
mathparse.parse('999999 ^ 999999999')
|
|
58
|
+
|
|
59
|
+
def test_custom_integer_digit_limit(self):
|
|
60
|
+
mathparse.MAX_INTEGER_DIGITS = 10
|
|
61
|
+
with self.assertRaises(mathparse.ExpressionTooLargeException):
|
|
62
|
+
mathparse.parse('10 ^ 10')
|
|
63
|
+
|
|
64
|
+
def test_disabled_integer_digit_limit(self):
|
|
65
|
+
mathparse.MAX_INTEGER_DIGITS = None
|
|
66
|
+
self.assertEqual(mathparse.parse('2 ^ 5000'), 2 ** 5000)
|
|
67
|
+
|
|
68
|
+
def test_disabled_expression_length_limit(self):
|
|
69
|
+
mathparse.MAX_EXPRESSION_LENGTH = None
|
|
70
|
+
self.assertEqual(mathparse.parse('1' + ' + 1' * 500), 501)
|
|
@@ -221,3 +221,84 @@ class MathModuleUnaryFunctionTestCase(TestCase):
|
|
|
221
221
|
self.assertAlmostEqual(
|
|
222
222
|
float(mathparse.parse('degrees(pi)')), 180.0, places=3
|
|
223
223
|
)
|
|
224
|
+
|
|
225
|
+
def test_sin_of_constant_division(self):
|
|
226
|
+
self.assertAlmostEqual(float(mathparse.parse('sin(pi/2)')), 1.0)
|
|
227
|
+
|
|
228
|
+
def test_round_half_away_from_zero(self):
|
|
229
|
+
self.assertEqual(mathparse.parse('round(2.5)'), 3)
|
|
230
|
+
|
|
231
|
+
def test_round_down(self):
|
|
232
|
+
self.assertEqual(mathparse.parse('round(2.4)'), 2)
|
|
233
|
+
|
|
234
|
+
def test_round_negative_half(self):
|
|
235
|
+
self.assertEqual(mathparse.parse('round(-2.5)'), -3)
|
|
236
|
+
|
|
237
|
+
def test_round_division_result(self):
|
|
238
|
+
self.assertEqual(mathparse.parse('round(7 / 2)'), 4)
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
class UnaryFunctionDecimalArgumentTestCase(TestCase):
|
|
242
|
+
"""
|
|
243
|
+
A decimal number directly after a unary function is its whole argument.
|
|
244
|
+
"""
|
|
245
|
+
|
|
246
|
+
def test_round_without_parenthesis(self):
|
|
247
|
+
self.assertEqual(mathparse.parse('round 2.5'), 3)
|
|
248
|
+
|
|
249
|
+
def test_floor(self):
|
|
250
|
+
self.assertEqual(mathparse.parse('floor 3.7'), 3)
|
|
251
|
+
|
|
252
|
+
def test_sqrt(self):
|
|
253
|
+
self.assertEqual(mathparse.parse('sqrt 2.25'), 1.5)
|
|
254
|
+
|
|
255
|
+
def test_sqrt_then_addition(self):
|
|
256
|
+
self.assertEqual(mathparse.parse('sqrt 2.25 + 1'), 2.5)
|
|
257
|
+
|
|
258
|
+
def test_unary_function_binds_tighter_than_power(self):
|
|
259
|
+
self.assertEqual(mathparse.parse('sqrt 2.25 ^ 2'), 2.25)
|
|
260
|
+
|
|
261
|
+
def test_ceil_then_multiplication(self):
|
|
262
|
+
self.assertEqual(mathparse.parse('ceil 1.2 * 2'), 4)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
class MinMaxFunctionTestCase(TestCase):
|
|
266
|
+
|
|
267
|
+
def test_max_plus_min(self):
|
|
268
|
+
self.assertEqual(mathparse.parse('max(3, 7) + min(2, 5)'), 9)
|
|
269
|
+
|
|
270
|
+
def test_without_spaces(self):
|
|
271
|
+
self.assertEqual(mathparse.parse('max(3,7)'), 7)
|
|
272
|
+
|
|
273
|
+
def test_more_than_two_arguments(self):
|
|
274
|
+
self.assertEqual(mathparse.parse('min(4, 1, 9, 2)'), 1)
|
|
275
|
+
|
|
276
|
+
def test_single_argument(self):
|
|
277
|
+
self.assertEqual(mathparse.parse('max(5)'), 5)
|
|
278
|
+
|
|
279
|
+
def test_expression_arguments(self):
|
|
280
|
+
self.assertEqual(mathparse.parse('max(1 + 2, 3 * 4)'), 12)
|
|
281
|
+
|
|
282
|
+
def test_nested(self):
|
|
283
|
+
self.assertEqual(mathparse.parse('max(min(1, 2), 3)'), 3)
|
|
284
|
+
|
|
285
|
+
def test_negative_arguments(self):
|
|
286
|
+
self.assertEqual(mathparse.parse('max(-3, -7)'), -3)
|
|
287
|
+
|
|
288
|
+
def test_decimal_arguments(self):
|
|
289
|
+
self.assertEqual(mathparse.parse('max(1.5, 1.25)'), 1.5)
|
|
290
|
+
|
|
291
|
+
def test_in_expression(self):
|
|
292
|
+
self.assertEqual(mathparse.parse('2 * max(sqrt 16, 2.5) ^ 2'), 32.0)
|
|
293
|
+
|
|
294
|
+
def test_with_words(self):
|
|
295
|
+
result = mathparse.parse('max(three, seven) plus one', language='ENG')
|
|
296
|
+
self.assertEqual(result, 8)
|
|
297
|
+
|
|
298
|
+
def test_single_argument_function_with_arguments(self):
|
|
299
|
+
with self.assertRaises(mathparse.PostfixTokenEvaluationException):
|
|
300
|
+
mathparse.parse('sin(1, 2)')
|
|
301
|
+
|
|
302
|
+
def test_arguments_outside_function(self):
|
|
303
|
+
with self.assertRaises(mathparse.PostfixTokenEvaluationException):
|
|
304
|
+
mathparse.parse('2 + (1, 2)')
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/azrideus_mathparse.egg-info/requires.txt
RENAMED
|
File without changes
|
{azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/azrideus_mathparse.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{azrideus_mathparse-0.2.10 → azrideus_mathparse-0.2.12}/tests/test_prefix_unary_operations.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|