mathparse 0.2.1__py3-none-any.whl → 0.2.2__py3-none-any.whl
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.
- mathparse/__init__.py +1 -1
- mathparse/mathparse.py +232 -38
- mathparse/mathwords.py +19 -14
- {mathparse-0.2.1.dist-info → mathparse-0.2.2.dist-info}/METADATA +17 -7
- mathparse-0.2.2.dist-info/RECORD +8 -0
- {mathparse-0.2.1.dist-info → mathparse-0.2.2.dist-info}/WHEEL +1 -1
- mathparse-0.2.1.dist-info/RECORD +0 -8
- {mathparse-0.2.1.dist-info → mathparse-0.2.2.dist-info}/licenses/LICENSE +0 -0
- {mathparse-0.2.1.dist-info → mathparse-0.2.2.dist-info}/top_level.txt +0 -0
mathparse/__init__.py
CHANGED
mathparse/mathparse.py
CHANGED
|
@@ -2,19 +2,19 @@
|
|
|
2
2
|
Methods for evaluating mathematical equations in strings.
|
|
3
3
|
"""
|
|
4
4
|
from decimal import Decimal
|
|
5
|
+
from typing import Union
|
|
5
6
|
from . import mathwords
|
|
6
7
|
import re
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
class PostfixTokenEvaluationException(Exception):
|
|
10
11
|
"""
|
|
11
|
-
Exception to be raised when
|
|
12
|
-
is not a part of the ISO 639-2 standard.
|
|
12
|
+
Exception to be raised when an expression cannot be evaluated.
|
|
13
13
|
"""
|
|
14
14
|
pass
|
|
15
15
|
|
|
16
16
|
|
|
17
|
-
def is_int(string):
|
|
17
|
+
def is_int(string: str) -> bool:
|
|
18
18
|
"""
|
|
19
19
|
Return true if string is an integer.
|
|
20
20
|
"""
|
|
@@ -25,7 +25,7 @@ def is_int(string):
|
|
|
25
25
|
return False
|
|
26
26
|
|
|
27
27
|
|
|
28
|
-
def is_float(string):
|
|
28
|
+
def is_float(string: str) -> bool:
|
|
29
29
|
"""
|
|
30
30
|
Return true if the string is a float.
|
|
31
31
|
"""
|
|
@@ -36,14 +36,14 @@ def is_float(string):
|
|
|
36
36
|
return False
|
|
37
37
|
|
|
38
38
|
|
|
39
|
-
def is_constant(string):
|
|
39
|
+
def is_constant(string: str) -> bool:
|
|
40
40
|
"""
|
|
41
41
|
Return true if the string is a mathematical constant.
|
|
42
42
|
"""
|
|
43
43
|
return mathwords.CONSTANTS.get(string, False)
|
|
44
44
|
|
|
45
45
|
|
|
46
|
-
def is_unary(string):
|
|
46
|
+
def is_unary(string: str) -> bool:
|
|
47
47
|
"""
|
|
48
48
|
Return true if the string is a defined unary mathematical
|
|
49
49
|
operator function.
|
|
@@ -51,14 +51,14 @@ def is_unary(string):
|
|
|
51
51
|
return string in mathwords.UNARY_FUNCTIONS
|
|
52
52
|
|
|
53
53
|
|
|
54
|
-
def is_binary(string):
|
|
54
|
+
def is_binary(string: str) -> bool:
|
|
55
55
|
"""
|
|
56
56
|
Return true if the string is a defined binary operator.
|
|
57
57
|
"""
|
|
58
58
|
return string in mathwords.BINARY_OPERATORS
|
|
59
59
|
|
|
60
60
|
|
|
61
|
-
def is_symbol(string):
|
|
61
|
+
def is_symbol(string: str) -> bool:
|
|
62
62
|
"""
|
|
63
63
|
Return true if the string is a mathematical symbol.
|
|
64
64
|
"""
|
|
@@ -70,7 +70,7 @@ def is_symbol(string):
|
|
|
70
70
|
)
|
|
71
71
|
|
|
72
72
|
|
|
73
|
-
def is_word(word, language):
|
|
73
|
+
def is_word(word: str, language: str) -> bool:
|
|
74
74
|
"""
|
|
75
75
|
Return true if the word is a math word for the specified language.
|
|
76
76
|
"""
|
|
@@ -79,7 +79,7 @@ def is_word(word, language):
|
|
|
79
79
|
return word in words
|
|
80
80
|
|
|
81
81
|
|
|
82
|
-
def find_word_groups(string, words):
|
|
82
|
+
def find_word_groups(string: str, words: list) -> list:
|
|
83
83
|
"""
|
|
84
84
|
Find matches for words in the format "3 thousand 6 hundred 2".
|
|
85
85
|
The words parameter should be the list of words to check for
|
|
@@ -98,40 +98,102 @@ def find_word_groups(string, words):
|
|
|
98
98
|
return result
|
|
99
99
|
|
|
100
100
|
|
|
101
|
-
def replace_word_tokens(
|
|
101
|
+
def replace_word_tokens(
|
|
102
|
+
string: str, language: str, stopwords: set[str] = None
|
|
103
|
+
) -> str:
|
|
102
104
|
"""
|
|
105
|
+
Replace word-based mathematical terms with their symbolic equivalents.
|
|
106
|
+
|
|
103
107
|
Given a string and an ISO 639-2 language code,
|
|
104
108
|
return the string with the words replaced with
|
|
105
109
|
an operational equivalent.
|
|
106
110
|
"""
|
|
107
111
|
words = mathwords.word_groups_for_language(language)
|
|
108
112
|
|
|
109
|
-
# Replace operator words with numeric operators
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
operators.update(words['unary_operators'])
|
|
113
|
-
|
|
114
|
-
for operator in list(operators.keys()):
|
|
113
|
+
# Replace binary operator words with numeric operators
|
|
114
|
+
binary_operators = words['binary_operators']
|
|
115
|
+
for operator in frozenset(binary_operators.keys()):
|
|
115
116
|
if operator in string:
|
|
116
|
-
string = string.replace(operator,
|
|
117
|
+
string = string.replace(operator, binary_operators[operator])
|
|
118
|
+
|
|
119
|
+
# Handle prefix unary operators (like "square root of")
|
|
120
|
+
if 'prefix_unary_operators' in words:
|
|
121
|
+
prefix_unary_operators = words['prefix_unary_operators']
|
|
122
|
+
for operator in frozenset(prefix_unary_operators.keys()):
|
|
123
|
+
if operator in string:
|
|
124
|
+
string = string.replace(
|
|
125
|
+
operator, prefix_unary_operators[operator]
|
|
126
|
+
)
|
|
117
127
|
|
|
118
|
-
#
|
|
128
|
+
# Handle postfix unary operators (like "squared", "cubed")
|
|
129
|
+
if 'postfix_unary_operators' in words:
|
|
130
|
+
postfix_unary_operators = words['postfix_unary_operators']
|
|
131
|
+
for operator in frozenset(postfix_unary_operators.keys()):
|
|
132
|
+
if operator in string:
|
|
133
|
+
# Captures the number/operand before the unary operator
|
|
134
|
+
pattern = r'(\w+)\s+' + re.escape(operator)
|
|
135
|
+
replacement = r'(\1 ' + postfix_unary_operators[operator] + ')'
|
|
136
|
+
string = re.sub(pattern, replacement, string)
|
|
137
|
+
|
|
138
|
+
# Handle compound numbers:
|
|
139
|
+
# (e.g., "twenty one" -> "(20 + 1)", "fifty four" -> "(50 + 4)")
|
|
119
140
|
numbers = words['numbers']
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
141
|
+
|
|
142
|
+
# Create regex patterns for compound numbers
|
|
143
|
+
# Pattern matches: (tens_word) (units_word) where tens_word is
|
|
144
|
+
# 20,30,40,...,90 and units_word is 1-9
|
|
145
|
+
tens_words = {word: value for word, value in numbers.items() if value in [
|
|
146
|
+
20, 30, 40, 50, 60, 70, 80, 90
|
|
147
|
+
]}
|
|
148
|
+
units_words = {word: value for word, value in numbers.items() if value in [
|
|
149
|
+
1, 2, 3, 4, 5, 6, 7, 8, 9
|
|
150
|
+
]}
|
|
151
|
+
|
|
152
|
+
# Preprocess hyphenated compound numbers
|
|
153
|
+
# (e.g., "fifty-four" -> "fifty four")
|
|
154
|
+
for tens_word in tens_words.keys():
|
|
155
|
+
for units_word in units_words.keys():
|
|
156
|
+
hyphenated_pattern = tens_word + r'-' + units_word
|
|
157
|
+
space_separated = tens_word + ' ' + units_word
|
|
158
|
+
if re.search(hyphenated_pattern, string):
|
|
159
|
+
string = re.sub(hyphenated_pattern, space_separated, string)
|
|
160
|
+
|
|
161
|
+
# Replace compound numbers first (before individual number replacement)
|
|
162
|
+
for tens_word, tens_value in tens_words.items():
|
|
163
|
+
for units_word, units_value in units_words.items():
|
|
164
|
+
compound_pattern = tens_word + r'\s+' + units_word
|
|
165
|
+
compound_replacement = f'({tens_value} + {units_value})'
|
|
166
|
+
if re.search(compound_pattern, string):
|
|
167
|
+
string = re.sub(compound_pattern, compound_replacement, string)
|
|
168
|
+
|
|
169
|
+
# Replace number words with numeric values
|
|
170
|
+
for number in frozenset(numbers.keys()):
|
|
171
|
+
# Use word boundaries to prevent partial matches
|
|
172
|
+
# (e.g., "nine" in "nineteen")
|
|
173
|
+
pattern = r'\b' + re.escape(number) + r'\b'
|
|
174
|
+
if re.search(pattern, string):
|
|
175
|
+
string = re.sub(pattern, str(numbers[number]), string)
|
|
176
|
+
|
|
177
|
+
# Remove words specified to be ignored
|
|
178
|
+
if stopwords:
|
|
179
|
+
filtered_words = []
|
|
180
|
+
|
|
181
|
+
for word in string.split():
|
|
182
|
+
if word not in stopwords:
|
|
183
|
+
filtered_words.append(word)
|
|
184
|
+
string = ' '.join(filtered_words)
|
|
123
185
|
|
|
124
186
|
# Replace scaling multipliers with numeric values
|
|
125
187
|
scales = words['scales']
|
|
126
188
|
end_index_characters = mathwords.BINARY_OPERATORS
|
|
127
189
|
end_index_characters.add('(')
|
|
128
190
|
|
|
129
|
-
word_matches = find_word_groups(string,
|
|
191
|
+
word_matches = find_word_groups(string, frozenset(scales.keys()))
|
|
130
192
|
|
|
131
193
|
for match in word_matches:
|
|
132
194
|
string = string.replace(match, '(' + match + ')')
|
|
133
195
|
|
|
134
|
-
for scale in
|
|
196
|
+
for scale in frozenset(scales.keys()):
|
|
135
197
|
for _ in range(0, string.count(scale)):
|
|
136
198
|
start_index = string.find(scale) - 1
|
|
137
199
|
end_index = len(string)
|
|
@@ -154,10 +216,14 @@ def replace_word_tokens(string, language):
|
|
|
154
216
|
|
|
155
217
|
string = string.replace(') (', ') + (')
|
|
156
218
|
|
|
219
|
+
# Remove extra parentheses around all scale expressions to simplify output
|
|
220
|
+
# Convert ((number * scale)) to (number * scale) for all scales
|
|
221
|
+
string = re.sub(r'\(\((\d+\s*\*\s*\d+)\)\)', r'(\1)', string)
|
|
222
|
+
|
|
157
223
|
return string
|
|
158
224
|
|
|
159
225
|
|
|
160
|
-
def to_postfix(tokens):
|
|
226
|
+
def to_postfix(tokens: list) -> list:
|
|
161
227
|
"""
|
|
162
228
|
Convert a list of evaluatable tokens to postfix format.
|
|
163
229
|
"""
|
|
@@ -170,6 +236,9 @@ def to_postfix(tokens):
|
|
|
170
236
|
'(': 1
|
|
171
237
|
}
|
|
172
238
|
|
|
239
|
+
# Unary functions have a higher precedence than binary operators
|
|
240
|
+
unary_precedence = max(precedence.values()) + 1
|
|
241
|
+
|
|
173
242
|
postfix = []
|
|
174
243
|
opstack = []
|
|
175
244
|
|
|
@@ -189,12 +258,28 @@ def to_postfix(tokens):
|
|
|
189
258
|
while top_token != '(':
|
|
190
259
|
postfix.append(top_token)
|
|
191
260
|
top_token = opstack.pop()
|
|
192
|
-
|
|
261
|
+
elif is_binary(token):
|
|
262
|
+
# For binary operators, pop operators with higher or
|
|
263
|
+
# equal precedence
|
|
193
264
|
while (opstack != []) and (
|
|
194
|
-
|
|
265
|
+
(
|
|
266
|
+
opstack[-1] in precedence and token in precedence and (
|
|
267
|
+
precedence[opstack[-1]] >= precedence[token]
|
|
268
|
+
)
|
|
269
|
+
) or
|
|
270
|
+
(
|
|
271
|
+
is_unary(
|
|
272
|
+
opstack[-1]
|
|
273
|
+
) and unary_precedence >= precedence[token]
|
|
274
|
+
)
|
|
195
275
|
):
|
|
196
276
|
postfix.append(opstack.pop())
|
|
197
277
|
opstack.append(token)
|
|
278
|
+
else:
|
|
279
|
+
# Raise exception for unsupported mathematical terms
|
|
280
|
+
raise PostfixTokenEvaluationException(
|
|
281
|
+
'Unsupported mathematical term: "{}"'.format(token)
|
|
282
|
+
)
|
|
198
283
|
|
|
199
284
|
while opstack != []:
|
|
200
285
|
postfix.append(opstack.pop())
|
|
@@ -202,7 +287,7 @@ def to_postfix(tokens):
|
|
|
202
287
|
return postfix
|
|
203
288
|
|
|
204
289
|
|
|
205
|
-
def evaluate_postfix(tokens):
|
|
290
|
+
def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
206
291
|
"""
|
|
207
292
|
Given a list of evaluatable tokens in postfix format,
|
|
208
293
|
calculate a solution.
|
|
@@ -217,7 +302,13 @@ def evaluate_postfix(tokens):
|
|
|
217
302
|
elif is_unary(token):
|
|
218
303
|
a = stack.pop()
|
|
219
304
|
total = mathwords.UNARY_FUNCTIONS[token](a)
|
|
220
|
-
elif len(stack):
|
|
305
|
+
elif len(stack) == 1:
|
|
306
|
+
raise PostfixTokenEvaluationException(
|
|
307
|
+
'Insufficient values in expression for operator "{}"'.format(
|
|
308
|
+
token
|
|
309
|
+
)
|
|
310
|
+
)
|
|
311
|
+
elif len(stack) > 1:
|
|
221
312
|
b = stack.pop()
|
|
222
313
|
a = stack.pop()
|
|
223
314
|
if token == '+':
|
|
@@ -235,7 +326,7 @@ def evaluate_postfix(tokens):
|
|
|
235
326
|
total = Decimal(str(a)) / Decimal(str(b))
|
|
236
327
|
else:
|
|
237
328
|
raise PostfixTokenEvaluationException(
|
|
238
|
-
'Unknown token {}'.format(token)
|
|
329
|
+
'Unknown token "{}"'.format(token)
|
|
239
330
|
)
|
|
240
331
|
|
|
241
332
|
if total is not None:
|
|
@@ -250,9 +341,23 @@ def evaluate_postfix(tokens):
|
|
|
250
341
|
return stack.pop()
|
|
251
342
|
|
|
252
343
|
|
|
253
|
-
def tokenize(string, language=None, escape='___'):
|
|
344
|
+
def tokenize(string: str, language: str = None, escape: str = '___') -> list:
|
|
254
345
|
"""
|
|
255
|
-
|
|
346
|
+
Convert a string into a list of mathematical tokens for processing.
|
|
347
|
+
|
|
348
|
+
Args:
|
|
349
|
+
string (str): The input string containing a mathematical expression.
|
|
350
|
+
|
|
351
|
+
language (str, optional): ISO 639-2 language code for word-based
|
|
352
|
+
parsing. If None, only numeric expressions
|
|
353
|
+
are supported.
|
|
354
|
+
|
|
355
|
+
escape (str, optional): A string used to temporarily replace spaces
|
|
356
|
+
in multi-word phrases during tokenization.
|
|
357
|
+
Default is '___'.
|
|
358
|
+
|
|
359
|
+
Returns:
|
|
360
|
+
list: A list of tokens extracted from the input string.
|
|
256
361
|
"""
|
|
257
362
|
# Set all words to lowercase
|
|
258
363
|
string = string.lower()
|
|
@@ -281,12 +386,69 @@ def tokenize(string, language=None, escape='___'):
|
|
|
281
386
|
return tokens
|
|
282
387
|
|
|
283
388
|
|
|
284
|
-
def parse(
|
|
389
|
+
def parse(
|
|
390
|
+
string: str, language: str = None, stopwords: set[str] = None
|
|
391
|
+
) -> Union[int, float, str, Decimal]:
|
|
285
392
|
"""
|
|
286
|
-
|
|
393
|
+
Parse and evaluate a mathematical expression from a string.
|
|
394
|
+
|
|
395
|
+
This is the main entry point for mathparse. It can handle both numeric
|
|
396
|
+
expressions (like "2 + 3 * 4") and word-based expressions in various
|
|
397
|
+
languages (like "five plus three" in English).
|
|
398
|
+
|
|
399
|
+
Args:
|
|
400
|
+
string (str): The mathematical expression to parse and evaluate.
|
|
401
|
+
Can contain numbers, operators, parentheses, constants,
|
|
402
|
+
and functions. For word-based parsing, must use terms
|
|
403
|
+
from the specified language.
|
|
404
|
+
language (str, optional): ISO 639-2 language code for word-based
|
|
405
|
+
parsing. Supported codes: 'ENG', 'FRE', 'GER',
|
|
406
|
+
'GRE', 'ITA', 'MAR', 'RUS', 'POR'. If None,
|
|
407
|
+
only numeric expressions are supported.
|
|
408
|
+
stopwords (set[str], optional): A set of words to ignore during
|
|
409
|
+
parsing. This can be used to filter out
|
|
410
|
+
non-mathematical words in expressions.
|
|
411
|
+
|
|
412
|
+
Returns:
|
|
413
|
+
int, float, or str: The result of the mathematical expression.
|
|
414
|
+
Returns 'undefined' for division by zero.
|
|
415
|
+
For division operations, returns a Decimal object
|
|
416
|
+
to maintain precision.
|
|
417
|
+
|
|
418
|
+
Raises:
|
|
419
|
+
InvalidLanguageCodeException:
|
|
420
|
+
An unsupported language code was provided.
|
|
421
|
+
PostfixTokenEvaluationException:
|
|
422
|
+
The expression cannot be evaluated.
|
|
423
|
+
|
|
424
|
+
Examples:
|
|
425
|
+
>>> parse('2 + 3 * 4')
|
|
426
|
+
14
|
|
427
|
+
|
|
428
|
+
>>> parse('five plus three', language='ENG')
|
|
429
|
+
8
|
|
430
|
+
|
|
431
|
+
>>> parse('(seven * nine) + 8 - (45 plus two)', language='ENG')
|
|
432
|
+
24
|
|
433
|
+
|
|
434
|
+
>>> parse('sqrt 16')
|
|
435
|
+
4.0
|
|
436
|
+
|
|
437
|
+
>>> parse('pi * 2')
|
|
438
|
+
6.283386
|
|
439
|
+
|
|
440
|
+
>>> parse('10 / 0')
|
|
441
|
+
'undefined'
|
|
442
|
+
|
|
443
|
+
Note:
|
|
444
|
+
- Follows standard order of operations (PEMDAS)
|
|
445
|
+
- Supports mathematical constants: pi, e
|
|
446
|
+
- Supports unary functions: sqrt, log
|
|
447
|
+
- Each expression must use terms from a single language
|
|
448
|
+
- Division by zero returns 'undefined' instead of raising an exception
|
|
287
449
|
"""
|
|
288
450
|
if language:
|
|
289
|
-
string = replace_word_tokens(string, language)
|
|
451
|
+
string = replace_word_tokens(string, language, stopwords)
|
|
290
452
|
|
|
291
453
|
tokens = tokenize(string)
|
|
292
454
|
postfix = to_postfix(tokens)
|
|
@@ -294,10 +456,42 @@ def parse(string, language=None):
|
|
|
294
456
|
return evaluate_postfix(postfix)
|
|
295
457
|
|
|
296
458
|
|
|
297
|
-
def extract_expression(dirty_string, language):
|
|
459
|
+
def extract_expression(dirty_string: str, language: str) -> str:
|
|
298
460
|
"""
|
|
299
|
-
|
|
300
|
-
|
|
461
|
+
Extract a mathematical expression from a sentence containing extra text.
|
|
462
|
+
|
|
463
|
+
This function identifies and extracts the mathematical portion from
|
|
464
|
+
natural language sentences like:
|
|
465
|
+
"What is 4 + 4?" or "Calculate five plus three".
|
|
466
|
+
It works by finding the longest sequence of mathematical symbols and words.
|
|
467
|
+
|
|
468
|
+
Args:
|
|
469
|
+
dirty_string (str): A sentence or phrase containing a mathematical
|
|
470
|
+
expression mixed with other text.
|
|
471
|
+
language (str): ISO 639-2 language code to identify mathematical
|
|
472
|
+
words in the target language.
|
|
473
|
+
|
|
474
|
+
Returns:
|
|
475
|
+
str: The extracted mathematical expression as a string.
|
|
476
|
+
|
|
477
|
+
Examples:
|
|
478
|
+
>>> extract_expression("What is 5 plus 3?", language='ENG')
|
|
479
|
+
'5 plus 3'
|
|
480
|
+
|
|
481
|
+
>>> extract_expression(
|
|
482
|
+
"Please calculate two times seven", language='ENG'
|
|
483
|
+
)
|
|
484
|
+
'two times seven'
|
|
485
|
+
|
|
486
|
+
>>> extract_expression(
|
|
487
|
+
"The result of 10 / 2 should be 5", language=None
|
|
488
|
+
)
|
|
489
|
+
'10 / 2'
|
|
490
|
+
|
|
491
|
+
Note:
|
|
492
|
+
- The function looks for continuous sequences of mathematical terms
|
|
493
|
+
- Non-mathematical words at the beginning and end are stripped
|
|
494
|
+
- The language parameter is required to identify word-based math terms
|
|
301
495
|
"""
|
|
302
496
|
tokens = tokenize(dirty_string, language)
|
|
303
497
|
|
mathparse/mathwords.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
from __future__ import unicode_literals
|
|
3
1
|
import math
|
|
4
2
|
|
|
5
3
|
"""
|
|
@@ -12,10 +10,12 @@ BINARY_OPERATORS = {
|
|
|
12
10
|
|
|
13
11
|
MATH_WORDS = {
|
|
14
12
|
'ENG': {
|
|
15
|
-
'
|
|
13
|
+
'prefix_unary_operators': {
|
|
14
|
+
'square root of': 'sqrt'
|
|
15
|
+
},
|
|
16
|
+
'postfix_unary_operators': {
|
|
16
17
|
'squared': '^ 2',
|
|
17
18
|
'cubed': '^ 3',
|
|
18
|
-
'square root of': 'sqrt'
|
|
19
19
|
},
|
|
20
20
|
'binary_operators': {
|
|
21
21
|
'plus': '+',
|
|
@@ -165,10 +165,12 @@ MATH_WORDS = {
|
|
|
165
165
|
}
|
|
166
166
|
},
|
|
167
167
|
'GRE': {
|
|
168
|
-
'
|
|
168
|
+
'prefix_unary_operators': {
|
|
169
|
+
'τετραγωνική ρίζα του': 'sqrt'
|
|
170
|
+
},
|
|
171
|
+
'postfix_unary_operators': {
|
|
169
172
|
'στο τετράγωνο': '^ 2',
|
|
170
173
|
'στον κύβο': '^ 3',
|
|
171
|
-
'τετραγωνική ρίζα του': 'sqrt'
|
|
172
174
|
},
|
|
173
175
|
'binary_operators': {
|
|
174
176
|
'συν': '+', 'και': '+',
|
|
@@ -369,10 +371,12 @@ MATH_WORDS = {
|
|
|
369
371
|
}
|
|
370
372
|
},
|
|
371
373
|
'POR': {
|
|
372
|
-
'
|
|
374
|
+
'prefix_unary_operators': {
|
|
375
|
+
'raiz quadrada de': 'sqrt'
|
|
376
|
+
},
|
|
377
|
+
'postfix_unary_operators': {
|
|
373
378
|
'ao quadrado': '^ 2',
|
|
374
379
|
'ao cubo': '^ 3',
|
|
375
|
-
'raiz quadrada de': 'sqrt'
|
|
376
380
|
},
|
|
377
381
|
'binary_operators': {
|
|
378
382
|
'mais': '+',
|
|
@@ -442,13 +446,14 @@ UNARY_FUNCTIONS = {
|
|
|
442
446
|
|
|
443
447
|
class InvalidLanguageCodeException(Exception):
|
|
444
448
|
"""
|
|
445
|
-
Exception to be raised when a language code is
|
|
446
|
-
|
|
449
|
+
Exception to be raised when a language code is specified that is not a part
|
|
450
|
+
of the ISO 639-2 standard, or if the specified language is not yet
|
|
451
|
+
supported by mathparse.
|
|
447
452
|
"""
|
|
448
453
|
pass
|
|
449
454
|
|
|
450
455
|
|
|
451
|
-
def word_groups_for_language(language_code):
|
|
456
|
+
def word_groups_for_language(language_code: str) -> dict[str, dict[str, str]]:
|
|
452
457
|
"""
|
|
453
458
|
Return the math word groups for a language code.
|
|
454
459
|
The language_code should be an ISO 639-2 language code.
|
|
@@ -462,16 +467,16 @@ def word_groups_for_language(language_code):
|
|
|
462
467
|
return MATH_WORDS[language_code]
|
|
463
468
|
|
|
464
469
|
|
|
465
|
-
def words_for_language(language_code):
|
|
470
|
+
def words_for_language(language_code: str) -> set[str]:
|
|
466
471
|
"""
|
|
467
472
|
Return the math words for a language code.
|
|
468
473
|
The language_code should be an ISO 639-2 language code.
|
|
469
474
|
https://www.loc.gov/standards/iso639-2/php/code_list.php
|
|
470
475
|
"""
|
|
471
476
|
word_groups = word_groups_for_language(language_code)
|
|
472
|
-
words =
|
|
477
|
+
words = set()
|
|
473
478
|
|
|
474
479
|
for group in word_groups:
|
|
475
|
-
words.
|
|
480
|
+
words.update(word_groups[group].keys())
|
|
476
481
|
|
|
477
482
|
return words
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mathparse
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: mathparse is a library for solving mathematical equations contained in strings
|
|
5
5
|
Author: Gunther Cox
|
|
6
6
|
License-Expression: MIT
|
|
@@ -36,24 +36,34 @@ from mathparse import mathparse
|
|
|
36
36
|
mathparse.parse('50 * (85 / 100)')
|
|
37
37
|
>>> 42.5
|
|
38
38
|
|
|
39
|
-
mathparse.parse('one hundred times fifty four',
|
|
39
|
+
mathparse.parse('one hundred times fifty four', language='ENG')
|
|
40
40
|
>>> 5400
|
|
41
41
|
|
|
42
42
|
mathparse.parse('(seven * nine) + 8 - (45 plus two)', language='ENG')
|
|
43
43
|
>>> 24
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
+
## Security
|
|
47
|
+
|
|
48
|
+
Mathparse does not employ the use of Python's [`eval` function](https://docs.python.org/3/library/functions.html#eval) when evaluating provided mathematical expressions. This is a measure to prevent arbitrary code execution vulnerabilities. See https://mathparse.chatterbot.us/postfix/ for additional details.
|
|
49
|
+
|
|
50
|
+
Mathparse is a standalone Python package and requires zero dependencies to function.
|
|
51
|
+
|
|
52
|
+
## Language Support
|
|
53
|
+
|
|
54
|
+
The language parameter must be set in order to evaluate an equation that uses word operators.
|
|
55
|
+
The language code should be a valid [ISO 639-2](https://www.loc.gov/standards/iso639-2/php/code_list.php) language code.
|
|
56
|
+
|
|
46
57
|
## Installation
|
|
47
58
|
|
|
48
59
|
```bash
|
|
49
60
|
pip install mathparse
|
|
50
61
|
```
|
|
51
62
|
|
|
52
|
-
##
|
|
63
|
+
## Documentation
|
|
53
64
|
|
|
54
|
-
|
|
55
|
-
The language code should be a valid [ISO 639-2](https://www.loc.gov/standards/iso639-2/php/code_list.php) language code.
|
|
65
|
+
See the full documentation at https://mathparse.chatterbot.us
|
|
56
66
|
|
|
57
|
-
##
|
|
67
|
+
## Changelog
|
|
58
68
|
|
|
59
|
-
See [release notes](https://github.com/gunthercox/
|
|
69
|
+
See [release notes](https://github.com/gunthercox/mathparse/releases) for changes.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
mathparse/__init__.py,sha256=9Ey4fS-L-fEKlmx-dvNIcjs8UcKbU3QYtXsQXg-H0rE,110
|
|
2
|
+
mathparse/mathparse.py,sha256=rn6o8Ona6moWH3Rw3d-v14UgJ2U1hsL7ViHcCOgfekY,16398
|
|
3
|
+
mathparse/mathwords.py,sha256=enaemAM96SvZY7wBXS1bKIEjBmcOQULSztWSPiq4aHc,13458
|
|
4
|
+
mathparse-0.2.2.dist-info/licenses/LICENSE,sha256=_buf4D_BxjT3x8SBhP0Rh966qP3zpbButcLI4bPq_TY,1068
|
|
5
|
+
mathparse-0.2.2.dist-info/METADATA,sha256=d2R-KEcUrVpxHlBqnxSxM9yY-RipMUg8va2A5EPN1Po,2325
|
|
6
|
+
mathparse-0.2.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
7
|
+
mathparse-0.2.2.dist-info/top_level.txt,sha256=rAQj3bfKyZhbFL6WxVLRCqnvwQPI2ZCc_FNrwx6Hlmc,10
|
|
8
|
+
mathparse-0.2.2.dist-info/RECORD,,
|
mathparse-0.2.1.dist-info/RECORD
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
mathparse/__init__.py,sha256=_3iITTMVoA5v1mEbRKsLcAFU2BtH2sV9SPTkfbpU-xs,110
|
|
2
|
-
mathparse/mathparse.py,sha256=_tmr746_Ofx1_CWfoKOlKc4phBNHLtiR_w-JhVbnexo,8242
|
|
3
|
-
mathparse/mathwords.py,sha256=p7wMTdSUk3DiQ8SgW8U2AOMjVHXyqMKGNNKKxzWlr4o,13231
|
|
4
|
-
mathparse-0.2.1.dist-info/licenses/LICENSE,sha256=_buf4D_BxjT3x8SBhP0Rh966qP3zpbButcLI4bPq_TY,1068
|
|
5
|
-
mathparse-0.2.1.dist-info/METADATA,sha256=xWgOv4h29tHeOpUxh1YeLzyKRUIh7ENn2mJJIv5sPKI,1838
|
|
6
|
-
mathparse-0.2.1.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
|
|
7
|
-
mathparse-0.2.1.dist-info/top_level.txt,sha256=rAQj3bfKyZhbFL6WxVLRCqnvwQPI2ZCc_FNrwx6Hlmc,10
|
|
8
|
-
mathparse-0.2.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|