azrideus-mathparse 0.2.13__tar.gz → 0.2.15__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.13 → azrideus_mathparse-0.2.15}/PKG-INFO +1 -1
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/azrideus_mathparse.egg-info/PKG-INFO +1 -1
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/azrideus_mathparse.egg-info/SOURCES.txt +11 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/mathparse/__init__.py +1 -1
- azrideus_mathparse-0.2.15/mathparse/evaluator.py +220 -0
- azrideus_mathparse-0.2.15/mathparse/exceptions.py +20 -0
- azrideus_mathparse-0.2.15/mathparse/extraction.py +108 -0
- azrideus_mathparse-0.2.15/mathparse/limits.py +32 -0
- azrideus_mathparse-0.2.15/mathparse/mathparse.py +174 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/mathparse/mathwords.py +1857 -1834
- azrideus_mathparse-0.2.15/mathparse/patterns.py +20 -0
- azrideus_mathparse-0.2.15/mathparse/postfix.py +111 -0
- azrideus_mathparse-0.2.15/mathparse/tokenizer.py +197 -0
- azrideus_mathparse-0.2.15/mathparse/tokens.py +111 -0
- azrideus_mathparse-0.2.15/mathparse/word_tokens.py +311 -0
- azrideus_mathparse-0.2.15/tests/test_bitwise_operations.py +82 -0
- azrideus_mathparse-0.2.15/tests/test_hex_numbers.py +94 -0
- azrideus_mathparse-0.2.13/mathparse/mathparse.py +0 -1107
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/LICENSE +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/README.md +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/azrideus_mathparse.egg-info/dependency_links.txt +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/azrideus_mathparse.egg-info/requires.txt +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/azrideus_mathparse.egg-info/top_level.txt +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/pyproject.toml +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/setup.cfg +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/tests/test_binary_operations.py +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/tests/test_bitshift_operations.py +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/tests/test_comparison_operations.py +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/tests/test_complex_statements.py +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/tests/test_language_support.py +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/tests/test_limits.py +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/tests/test_prefix_unary_operations.py +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/tests/test_replace_word_tokens.py +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/tests/test_simplified_chinese.py +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/tests/test_unary_operations.py +0 -0
- {azrideus_mathparse-0.2.13 → azrideus_mathparse-0.2.15}/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.15
|
|
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.13 → azrideus_mathparse-0.2.15}/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.15
|
|
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.13 → azrideus_mathparse-0.2.15}/azrideus_mathparse.egg-info/SOURCES.txt
RENAMED
|
@@ -7,12 +7,23 @@ azrideus_mathparse.egg-info/dependency_links.txt
|
|
|
7
7
|
azrideus_mathparse.egg-info/requires.txt
|
|
8
8
|
azrideus_mathparse.egg-info/top_level.txt
|
|
9
9
|
mathparse/__init__.py
|
|
10
|
+
mathparse/evaluator.py
|
|
11
|
+
mathparse/exceptions.py
|
|
12
|
+
mathparse/extraction.py
|
|
13
|
+
mathparse/limits.py
|
|
10
14
|
mathparse/mathparse.py
|
|
11
15
|
mathparse/mathwords.py
|
|
16
|
+
mathparse/patterns.py
|
|
17
|
+
mathparse/postfix.py
|
|
18
|
+
mathparse/tokenizer.py
|
|
19
|
+
mathparse/tokens.py
|
|
20
|
+
mathparse/word_tokens.py
|
|
12
21
|
tests/test_binary_operations.py
|
|
13
22
|
tests/test_bitshift_operations.py
|
|
23
|
+
tests/test_bitwise_operations.py
|
|
14
24
|
tests/test_comparison_operations.py
|
|
15
25
|
tests/test_complex_statements.py
|
|
26
|
+
tests/test_hex_numbers.py
|
|
16
27
|
tests/test_language_support.py
|
|
17
28
|
tests/test_limits.py
|
|
18
29
|
tests/test_prefix_unary_operations.py
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Evaluation of expressions in postfix (reverse Polish) notation.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from decimal import Decimal
|
|
6
|
+
from typing import Union
|
|
7
|
+
from . import mathwords
|
|
8
|
+
from .exceptions import PostfixTokenEvaluationException
|
|
9
|
+
from .limits import check_integer_digits
|
|
10
|
+
from .tokens import is_constant, is_float, is_hex, is_int, is_unary, to_number
|
|
11
|
+
import math
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
COMPARISON_FUNCTIONS = {
|
|
15
|
+
"<": lambda a, b: a < b,
|
|
16
|
+
">": lambda a, b: a > b,
|
|
17
|
+
"<=": lambda a, b: a <= b,
|
|
18
|
+
">=": lambda a, b: a >= b,
|
|
19
|
+
"==": lambda a, b: a == b,
|
|
20
|
+
"!=": lambda a, b: a != b,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
25
|
+
"""
|
|
26
|
+
Given a list of evaluatable tokens in postfix format,
|
|
27
|
+
calculate a solution.
|
|
28
|
+
"""
|
|
29
|
+
stack = []
|
|
30
|
+
|
|
31
|
+
for token in tokens:
|
|
32
|
+
total = None
|
|
33
|
+
|
|
34
|
+
if is_int(token) or is_float(token) or is_constant(token):
|
|
35
|
+
stack.append(token)
|
|
36
|
+
elif is_unary(token):
|
|
37
|
+
a = stack.pop()
|
|
38
|
+
# Convert token (string) to number for unary function evaluation
|
|
39
|
+
a = to_number(a)
|
|
40
|
+
function = mathwords.UNARY_FUNCTIONS[token]
|
|
41
|
+
# Check the size of n! (using log(n!) = lgamma(n + 1)) before
|
|
42
|
+
# computing it, since a large factorial is slow to calculate
|
|
43
|
+
if function is math.factorial and isinstance(a, int) and a > 1:
|
|
44
|
+
check_integer_digits(math.lgamma(a + 1) / math.log(10), token)
|
|
45
|
+
if isinstance(a, mathwords.ArgumentList) and not getattr(
|
|
46
|
+
function, "variadic", False
|
|
47
|
+
):
|
|
48
|
+
raise PostfixTokenEvaluationException(
|
|
49
|
+
'Function "{}" takes a single argument'.format(token)
|
|
50
|
+
)
|
|
51
|
+
total = function(a)
|
|
52
|
+
elif len(stack) == 1:
|
|
53
|
+
raise PostfixTokenEvaluationException(
|
|
54
|
+
'Insufficient values in expression for operator "{}"'.format(token)
|
|
55
|
+
)
|
|
56
|
+
elif len(stack) > 1:
|
|
57
|
+
b = stack.pop()
|
|
58
|
+
a = stack.pop()
|
|
59
|
+
|
|
60
|
+
if token != "," and (
|
|
61
|
+
isinstance(a, mathwords.ArgumentList)
|
|
62
|
+
or isinstance(b, mathwords.ArgumentList)
|
|
63
|
+
):
|
|
64
|
+
raise PostfixTokenEvaluationException(
|
|
65
|
+
"Comma separated values can only be used as function "
|
|
66
|
+
"arguments, such as max(3, 7)"
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
if token == ",":
|
|
70
|
+
# Collect comma separated function arguments into a list
|
|
71
|
+
# Example: 3 , 7 , 5 = [3, 7, 5]
|
|
72
|
+
if isinstance(a, mathwords.ArgumentList):
|
|
73
|
+
total = a
|
|
74
|
+
else:
|
|
75
|
+
total = mathwords.ArgumentList([to_number(a)])
|
|
76
|
+
total.append(to_number(b))
|
|
77
|
+
elif token == "+":
|
|
78
|
+
total = to_number(a) + to_number(b)
|
|
79
|
+
elif token == "-":
|
|
80
|
+
total = to_number(a) - to_number(b)
|
|
81
|
+
elif token == "*":
|
|
82
|
+
total = to_number(a) * to_number(b)
|
|
83
|
+
elif token == "^":
|
|
84
|
+
base = to_number(a)
|
|
85
|
+
exponent = to_number(b)
|
|
86
|
+
# Check the size of an integer power before computing it,
|
|
87
|
+
# since a huge power (e.g. 999999 ^ 999999999) would use all
|
|
88
|
+
# available memory. Float and Decimal powers instead raise an
|
|
89
|
+
# overflow error quickly.
|
|
90
|
+
if (
|
|
91
|
+
isinstance(base, int)
|
|
92
|
+
and isinstance(exponent, int)
|
|
93
|
+
and exponent > 0
|
|
94
|
+
and abs(base) > 1
|
|
95
|
+
):
|
|
96
|
+
check_integer_digits(exponent * math.log10(abs(base)), token)
|
|
97
|
+
total = base**exponent
|
|
98
|
+
elif token == "/":
|
|
99
|
+
# Convert tokens such as constants (e.g. "pi") to numbers
|
|
100
|
+
# before passing them to Decimal
|
|
101
|
+
if to_number(b) == 0:
|
|
102
|
+
total = "undefined"
|
|
103
|
+
else:
|
|
104
|
+
total = Decimal(str(to_number(a))) / Decimal(str(to_number(b)))
|
|
105
|
+
elif token == "%":
|
|
106
|
+
if to_number(b) == 0:
|
|
107
|
+
total = "undefined"
|
|
108
|
+
else:
|
|
109
|
+
total = to_number(a) % to_number(b)
|
|
110
|
+
elif token in mathwords.COMPARISON_OPERATORS:
|
|
111
|
+
total = COMPARISON_FUNCTIONS[token](to_number(a), to_number(b))
|
|
112
|
+
elif token in mathwords.SHIFT_OPERATORS:
|
|
113
|
+
value = to_number(a)
|
|
114
|
+
count = to_number(b)
|
|
115
|
+
# Bit shifts are only defined for integers (True and False
|
|
116
|
+
# count as 1 and 0)
|
|
117
|
+
if not isinstance(value, int) or not isinstance(count, int):
|
|
118
|
+
raise PostfixTokenEvaluationException(
|
|
119
|
+
'Bit shift operator "{}" requires integer operands'.format(
|
|
120
|
+
token
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
if count < 0:
|
|
124
|
+
raise PostfixTokenEvaluationException(
|
|
125
|
+
'Bit shift operator "{}" requires a non-negative shift '
|
|
126
|
+
"count".format(token)
|
|
127
|
+
)
|
|
128
|
+
if token == "<<":
|
|
129
|
+
# Check the size of the result before computing it, since
|
|
130
|
+
# a huge shift (e.g. 1 << 999999999) would use all
|
|
131
|
+
# available memory. Each shift multiplies the value by 2.
|
|
132
|
+
if value != 0:
|
|
133
|
+
check_integer_digits(
|
|
134
|
+
math.log10(abs(value)) + count * math.log10(2), token
|
|
135
|
+
)
|
|
136
|
+
total = value << count
|
|
137
|
+
else:
|
|
138
|
+
total = value >> count
|
|
139
|
+
elif token in mathwords.BITWISE_OPERATORS:
|
|
140
|
+
left = to_number(a)
|
|
141
|
+
right = to_number(b)
|
|
142
|
+
# Bitwise operators are only defined for integers (True and
|
|
143
|
+
# False count as 1 and 0). The result is never larger than
|
|
144
|
+
# the larger operand, so no size check is needed.
|
|
145
|
+
if not isinstance(left, int) or not isinstance(right, int):
|
|
146
|
+
raise PostfixTokenEvaluationException(
|
|
147
|
+
'Bitwise operator "{}" requires integer operands'.format(
|
|
148
|
+
token
|
|
149
|
+
)
|
|
150
|
+
)
|
|
151
|
+
if token == "&":
|
|
152
|
+
total = int(left) & int(right)
|
|
153
|
+
else:
|
|
154
|
+
total = int(left) | int(right)
|
|
155
|
+
elif token == ".":
|
|
156
|
+
# Treat decimal points as a binary operator that combines the
|
|
157
|
+
# integer and fractional part of two numbers
|
|
158
|
+
# Example: 53 . 25 = 53.25, -3 . 5 = -3.5
|
|
159
|
+
|
|
160
|
+
# Convert b to number
|
|
161
|
+
numeric_b = to_number(b)
|
|
162
|
+
|
|
163
|
+
if numeric_b == 0:
|
|
164
|
+
# Convert a to number
|
|
165
|
+
numeric_a = to_number(a)
|
|
166
|
+
total = Decimal(numeric_a)
|
|
167
|
+
else:
|
|
168
|
+
# Check if 'a' has a negative sign (handles -0 case)
|
|
169
|
+
is_negative = str(a).startswith("-")
|
|
170
|
+
|
|
171
|
+
# Convert a to number for calculation
|
|
172
|
+
numeric_a = to_number(a)
|
|
173
|
+
|
|
174
|
+
# Count the digits in the original string b to preserve
|
|
175
|
+
# leading zeros (e.g., "01" has 2 digits, not 1)
|
|
176
|
+
digits = len(str(b))
|
|
177
|
+
divisor = 10**digits
|
|
178
|
+
fractional_part = numeric_b / divisor
|
|
179
|
+
|
|
180
|
+
# Handle negatives: -3 . 5 = -3.5, not -2.5
|
|
181
|
+
# Also -0 . 5 = -0.5 (check string since -0 == 0)
|
|
182
|
+
if is_negative:
|
|
183
|
+
total = numeric_a - fractional_part
|
|
184
|
+
else:
|
|
185
|
+
total = numeric_a + fractional_part
|
|
186
|
+
else:
|
|
187
|
+
raise PostfixTokenEvaluationException(
|
|
188
|
+
'Unknown token "{}"'.format(token)
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
# Limit the size of every computed integer, which also prevents
|
|
192
|
+
# repeated operations such as multiplication from growing a number
|
|
193
|
+
# without bound
|
|
194
|
+
if isinstance(total, int) and total:
|
|
195
|
+
check_integer_digits((total.bit_length() - 1) * math.log10(2), token)
|
|
196
|
+
|
|
197
|
+
if total is not None:
|
|
198
|
+
stack.append(total)
|
|
199
|
+
|
|
200
|
+
# If the stack is empty the tokens could not be evaluated
|
|
201
|
+
if not stack:
|
|
202
|
+
raise PostfixTokenEvaluationException(
|
|
203
|
+
"The postfix expression resulted in an empty stack"
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
result = stack.pop()
|
|
207
|
+
|
|
208
|
+
if isinstance(result, mathwords.ArgumentList):
|
|
209
|
+
raise PostfixTokenEvaluationException(
|
|
210
|
+
"Comma separated values can only be used as function arguments, "
|
|
211
|
+
"such as max(3, 7)"
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
# Convert final result from string (such as "4" or a constant like "pi")
|
|
215
|
+
# to a number if needed. A hexadecimal string produced by hex() is kept
|
|
216
|
+
# as-is, since that is the requested output format.
|
|
217
|
+
if isinstance(result, str) and not is_hex(result):
|
|
218
|
+
result = to_number(result)
|
|
219
|
+
|
|
220
|
+
return result
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Exceptions raised when an expression cannot be evaluated.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class PostfixTokenEvaluationException(Exception):
|
|
7
|
+
"""
|
|
8
|
+
Exception to be raised when an expression cannot be evaluated.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
pass
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ExpressionTooLargeException(PostfixTokenEvaluationException):
|
|
15
|
+
"""
|
|
16
|
+
Exception to be raised when an expression, or a number computed while
|
|
17
|
+
evaluating it, exceeds the configured size limits.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
pass
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Extraction of mathematical expressions from sentences containing other text.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from . import mathwords
|
|
6
|
+
from .tokenizer import tokenize
|
|
7
|
+
from .tokens import is_constant, is_float, is_int, is_symbol, is_unary, is_word
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def extract_expression(dirty_string: str, language: str) -> str:
|
|
11
|
+
"""
|
|
12
|
+
Extract a mathematical expression from a sentence containing extra text.
|
|
13
|
+
|
|
14
|
+
This function identifies and extracts the mathematical portion from
|
|
15
|
+
natural language sentences like:
|
|
16
|
+
"What is 4 + 4?" or "Calculate five plus three".
|
|
17
|
+
It works by finding the longest sequence of mathematical symbols and words.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
dirty_string (str): A sentence or phrase containing a mathematical
|
|
21
|
+
expression mixed with other text.
|
|
22
|
+
language (str): ISO 639-2 language code to identify mathematical
|
|
23
|
+
words in the target language.
|
|
24
|
+
|
|
25
|
+
Returns:
|
|
26
|
+
str: The extracted mathematical expression as a string.
|
|
27
|
+
|
|
28
|
+
Examples:
|
|
29
|
+
>>> extract_expression("What is 5 plus 3?", language='ENG')
|
|
30
|
+
'5 plus 3'
|
|
31
|
+
|
|
32
|
+
>>> extract_expression(
|
|
33
|
+
"Please calculate two times seven", language='ENG'
|
|
34
|
+
)
|
|
35
|
+
'two times seven'
|
|
36
|
+
|
|
37
|
+
>>> extract_expression(
|
|
38
|
+
"The result of 10 / 2 should be 5", language=None
|
|
39
|
+
)
|
|
40
|
+
'10 / 2'
|
|
41
|
+
|
|
42
|
+
Note:
|
|
43
|
+
- The function looks for continuous sequences of mathematical terms
|
|
44
|
+
- Non-mathematical words at the beginning and end are stripped
|
|
45
|
+
- The language parameter is required to identify word-based math terms
|
|
46
|
+
"""
|
|
47
|
+
tokens = tokenize(dirty_string, language)
|
|
48
|
+
|
|
49
|
+
start_index = 0
|
|
50
|
+
end_index = len(tokens)
|
|
51
|
+
|
|
52
|
+
# Find the start of the mathematical expression
|
|
53
|
+
# Skip over non-mathematical tokens AND isolated binary operators
|
|
54
|
+
for i, part in enumerate(tokens):
|
|
55
|
+
if is_symbol(part) or is_word(part, language):
|
|
56
|
+
# A potential start was found, so check if it's a standalone binary
|
|
57
|
+
# operator. Binary operators (except '(') are only valid at the
|
|
58
|
+
# start if they are unary (such as a leading minus for a negative
|
|
59
|
+
# number)
|
|
60
|
+
if part in mathwords.BINARY_OPERATORS and part != "(":
|
|
61
|
+
# For a binary operator to be the start of an expression, it
|
|
62
|
+
# must be:
|
|
63
|
+
# 1. At the very beginning (position 0) - could be unary minus
|
|
64
|
+
# 2. OR all previous tokens were non-mathematical - also could
|
|
65
|
+
# be unary
|
|
66
|
+
# If there were non-math tokens before it, it's likely a
|
|
67
|
+
# separator
|
|
68
|
+
|
|
69
|
+
# Check if all previous tokens are non-mathematical
|
|
70
|
+
all_prev_non_math = True
|
|
71
|
+
for j in range(i):
|
|
72
|
+
if is_symbol(tokens[j]) or is_word(tokens[j], language):
|
|
73
|
+
all_prev_non_math = False
|
|
74
|
+
break
|
|
75
|
+
|
|
76
|
+
# Only include this operator if at start OR all previous
|
|
77
|
+
# non-math AND followed by a mathematical token
|
|
78
|
+
if all_prev_non_math and i + 1 < len(tokens):
|
|
79
|
+
next_token = tokens[i + 1]
|
|
80
|
+
if (
|
|
81
|
+
is_int(next_token)
|
|
82
|
+
or is_float(next_token)
|
|
83
|
+
or is_constant(next_token)
|
|
84
|
+
or is_unary(next_token)
|
|
85
|
+
or next_token == "("
|
|
86
|
+
or is_word(next_token, language)
|
|
87
|
+
):
|
|
88
|
+
start_index = i
|
|
89
|
+
break
|
|
90
|
+
else:
|
|
91
|
+
# Start indexes can be an opening parenthesis, or a non-binary
|
|
92
|
+
# operator
|
|
93
|
+
start_index = i
|
|
94
|
+
break
|
|
95
|
+
|
|
96
|
+
for part in reversed(tokens):
|
|
97
|
+
if is_symbol(part) or is_word(part, language):
|
|
98
|
+
break
|
|
99
|
+
else:
|
|
100
|
+
end_index -= 1
|
|
101
|
+
|
|
102
|
+
result = " ".join(tokens[start_index:end_index])
|
|
103
|
+
|
|
104
|
+
# Remove spaces around decimal points for cleaner output
|
|
105
|
+
# Replace " . " with "." to convert "-100 . 5" to "-100.5"
|
|
106
|
+
result = result.replace(" . ", ".")
|
|
107
|
+
|
|
108
|
+
return result
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Size limits that protect against expressions which would take too long or
|
|
3
|
+
use too much memory to evaluate.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from .exceptions import ExpressionTooLargeException
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# Limits that protect against expressions which would take too long or use
|
|
10
|
+
# too much memory to evaluate, such as "999999 ** 999999999". Either limit can
|
|
11
|
+
# be changed, or set to None to disable it.
|
|
12
|
+
|
|
13
|
+
# The maximum number of characters in an expression passed to parse()
|
|
14
|
+
MAX_EXPRESSION_LENGTH = 1000
|
|
15
|
+
|
|
16
|
+
# The maximum number of digits in any integer computed during evaluation
|
|
17
|
+
MAX_INTEGER_DIGITS = 50
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def check_integer_digits(log10_value: float, token: str) -> None:
|
|
21
|
+
"""
|
|
22
|
+
Raise an ExpressionTooLargeException if an integer whose base 10
|
|
23
|
+
logarithm is (approximately) log10_value has more than MAX_INTEGER_DIGITS
|
|
24
|
+
digits.
|
|
25
|
+
"""
|
|
26
|
+
# An integer n has floor(log10(n)) + 1 digits
|
|
27
|
+
if MAX_INTEGER_DIGITS is not None and log10_value >= MAX_INTEGER_DIGITS:
|
|
28
|
+
raise ExpressionTooLargeException(
|
|
29
|
+
'The result of "{}" would have more than {} digits'.format(
|
|
30
|
+
token, MAX_INTEGER_DIGITS
|
|
31
|
+
)
|
|
32
|
+
)
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Methods for evaluating mathematical equations in strings.
|
|
3
|
+
|
|
4
|
+
This module is the public entry point of mathparse. The work is split across
|
|
5
|
+
the following modules, and everything they define can be imported from here:
|
|
6
|
+
|
|
7
|
+
- exceptions: errors raised when an expression cannot be evaluated
|
|
8
|
+
- limits: size limits on expressions and computed integers
|
|
9
|
+
- patterns: regular expressions for numbers and operators
|
|
10
|
+
- tokens: classification of tokens and conversion to numbers
|
|
11
|
+
- word_tokens: replacement of math words with symbols
|
|
12
|
+
- tokenizer: splitting of strings into tokens
|
|
13
|
+
- postfix: conversion of tokens to postfix notation
|
|
14
|
+
- evaluator: evaluation of postfix expressions
|
|
15
|
+
- extraction: finding an expression inside a sentence
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from decimal import Decimal, DecimalException
|
|
19
|
+
from typing import Union
|
|
20
|
+
import sys
|
|
21
|
+
import types
|
|
22
|
+
|
|
23
|
+
from . import limits
|
|
24
|
+
from .evaluator import COMPARISON_FUNCTIONS, evaluate_postfix # noqa: F401
|
|
25
|
+
from .exceptions import ( # noqa: F401
|
|
26
|
+
ExpressionTooLargeException,
|
|
27
|
+
PostfixTokenEvaluationException,
|
|
28
|
+
)
|
|
29
|
+
from .extraction import extract_expression # noqa: F401
|
|
30
|
+
from .limits import check_integer_digits # noqa: F401
|
|
31
|
+
from .patterns import ( # noqa: F401
|
|
32
|
+
COMPARISON_OPERATOR_PATTERN,
|
|
33
|
+
HEX_NUMBER_PATTERN,
|
|
34
|
+
SCIENTIFIC_NOTATION_PATTERN,
|
|
35
|
+
)
|
|
36
|
+
from .postfix import to_postfix
|
|
37
|
+
from .tokenizer import ( # noqa: F401
|
|
38
|
+
normalize_hex_numbers,
|
|
39
|
+
normalize_scientific_notation,
|
|
40
|
+
preprocess_unary_operators,
|
|
41
|
+
tokenize,
|
|
42
|
+
)
|
|
43
|
+
from .tokens import ( # noqa: F401
|
|
44
|
+
is_binary,
|
|
45
|
+
is_constant,
|
|
46
|
+
is_float,
|
|
47
|
+
is_hex,
|
|
48
|
+
is_int,
|
|
49
|
+
is_symbol,
|
|
50
|
+
is_unary,
|
|
51
|
+
is_word,
|
|
52
|
+
to_number,
|
|
53
|
+
)
|
|
54
|
+
from .word_tokens import ( # noqa: F401
|
|
55
|
+
create_unicode_word_boundary_pattern,
|
|
56
|
+
find_word_groups,
|
|
57
|
+
replace_word_tokens,
|
|
58
|
+
replace_word_tokens_simplified_chinese,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
# The size limits live in the limits module, where they are read during
|
|
63
|
+
# evaluation. Reading or setting them on this module (for example
|
|
64
|
+
# mathparse.MAX_INTEGER_DIGITS = 5000) is forwarded to the limits module, so
|
|
65
|
+
# that existing code which changes them keeps working.
|
|
66
|
+
_LIMIT_NAMES = {"MAX_EXPRESSION_LENGTH", "MAX_INTEGER_DIGITS"}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def __getattr__(name):
|
|
70
|
+
if name in _LIMIT_NAMES:
|
|
71
|
+
return getattr(limits, name)
|
|
72
|
+
raise AttributeError("module {!r} has no attribute {!r}".format(__name__, name))
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class _MathparseModule(types.ModuleType):
|
|
76
|
+
def __setattr__(self, name, value):
|
|
77
|
+
if name in _LIMIT_NAMES:
|
|
78
|
+
setattr(limits, name, value)
|
|
79
|
+
else:
|
|
80
|
+
super().__setattr__(name, value)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
sys.modules[__name__].__class__ = _MathparseModule
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def parse(
|
|
87
|
+
string: str, language: str = None, stopwords: set[str] = None
|
|
88
|
+
) -> Union[int, float, str, Decimal]:
|
|
89
|
+
"""
|
|
90
|
+
Parse and evaluate a mathematical expression from a string.
|
|
91
|
+
|
|
92
|
+
This is the main entry point for mathparse. It can handle both numeric
|
|
93
|
+
expressions (like "2 + 3 * 4") and word-based expressions in various
|
|
94
|
+
languages (like "five plus three" in English).
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
string (str): The mathematical expression to parse and evaluate.
|
|
98
|
+
Can contain numbers, operators, parentheses, constants,
|
|
99
|
+
and functions. For word-based parsing, must use terms
|
|
100
|
+
from the specified language.
|
|
101
|
+
language (str, optional): ISO 639-2 language code for word-based
|
|
102
|
+
parsing. Supported codes: 'ENG', 'FRE', 'GER',
|
|
103
|
+
'GRE', 'ITA', 'MAR', 'RUS', 'POR'. If None,
|
|
104
|
+
only numeric expressions are supported.
|
|
105
|
+
stopwords (set[str], optional): A set of words to ignore during
|
|
106
|
+
parsing. This can be used to filter out
|
|
107
|
+
non-mathematical words in expressions.
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
int, float, or str: The result of the mathematical expression.
|
|
111
|
+
Returns 'undefined' for division by zero.
|
|
112
|
+
For division operations, returns a Decimal object
|
|
113
|
+
to maintain precision.
|
|
114
|
+
|
|
115
|
+
Raises:
|
|
116
|
+
InvalidLanguageCodeException:
|
|
117
|
+
An unsupported language code was provided.
|
|
118
|
+
PostfixTokenEvaluationException:
|
|
119
|
+
The expression cannot be evaluated.
|
|
120
|
+
|
|
121
|
+
Examples:
|
|
122
|
+
>>> parse('2 + 3 * 4')
|
|
123
|
+
14
|
|
124
|
+
|
|
125
|
+
>>> parse('five plus three', language='ENG')
|
|
126
|
+
8
|
|
127
|
+
|
|
128
|
+
>>> parse('(seven * nine) + 8 - (45 plus two)', language='ENG')
|
|
129
|
+
24
|
|
130
|
+
|
|
131
|
+
>>> parse('sqrt 16')
|
|
132
|
+
4.0
|
|
133
|
+
|
|
134
|
+
>>> parse('pi * 2')
|
|
135
|
+
6.283386
|
|
136
|
+
|
|
137
|
+
>>> parse('10 / 0')
|
|
138
|
+
'undefined'
|
|
139
|
+
|
|
140
|
+
Note:
|
|
141
|
+
- Follows standard order of operations (PEMDAS)
|
|
142
|
+
- Supports mathematical constants: pi, e
|
|
143
|
+
- Supports unary functions: sqrt, log
|
|
144
|
+
- Supports comparison operators (<, >, <=, >=, ==, !=), which
|
|
145
|
+
evaluate to True or False after any arithmetic on either side
|
|
146
|
+
- Each expression must use terms from a single language
|
|
147
|
+
- Division by zero returns 'undefined' instead of raising an exception
|
|
148
|
+
- Raises ExpressionTooLargeException if the expression is longer than
|
|
149
|
+
MAX_EXPRESSION_LENGTH, or a result would be too large to compute
|
|
150
|
+
"""
|
|
151
|
+
max_length = limits.MAX_EXPRESSION_LENGTH
|
|
152
|
+
if max_length is not None and len(string) > max_length:
|
|
153
|
+
raise ExpressionTooLargeException(
|
|
154
|
+
"The expression is longer than {} characters".format(max_length)
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
if language:
|
|
158
|
+
if language == "CHI":
|
|
159
|
+
string = replace_word_tokens_simplified_chinese(string, stopwords)
|
|
160
|
+
else:
|
|
161
|
+
string = replace_word_tokens(string, language, stopwords)
|
|
162
|
+
|
|
163
|
+
tokens = tokenize(string, language)
|
|
164
|
+
tokens = preprocess_unary_operators(tokens)
|
|
165
|
+
postfix = to_postfix(tokens)
|
|
166
|
+
|
|
167
|
+
try:
|
|
168
|
+
return evaluate_postfix(postfix)
|
|
169
|
+
except (OverflowError, DecimalException) as error:
|
|
170
|
+
# Float and Decimal operations raise these when a result is too large
|
|
171
|
+
# (e.g. 10.0 ^ 400 or exp(1000))
|
|
172
|
+
raise ExpressionTooLargeException(
|
|
173
|
+
"The result is too large to compute: {}".format(error)
|
|
174
|
+
) from error
|