mathparse 0.2.1__tar.gz → 0.2.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mathparse
3
- Version: 0.2.1
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', mathparse.codes.ENG)
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
- ## Language support
63
+ ## Documentation
53
64
 
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.
65
+ See the full documentation at https://mathparse.chatterbot.us
56
66
 
57
- ## History
67
+ ## Changelog
58
68
 
59
- See [release notes](https://github.com/gunthercox/ChatterBot/releases) for changes.
69
+ See [release notes](https://github.com/gunthercox/mathparse/releases) for changes.
@@ -0,0 +1,43 @@
1
+ # mathparse
2
+
3
+ The `mathparse` library is a Python module designed to evaluate mathematical equations contained in strings.
4
+
5
+ Here are a few examples:
6
+
7
+ ```python
8
+ from mathparse import mathparse
9
+
10
+ mathparse.parse('50 * (85 / 100)')
11
+ >>> 42.5
12
+
13
+ mathparse.parse('one hundred times fifty four', language='ENG')
14
+ >>> 5400
15
+
16
+ mathparse.parse('(seven * nine) + 8 - (45 plus two)', language='ENG')
17
+ >>> 24
18
+ ```
19
+
20
+ ## Security
21
+
22
+ 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.
23
+
24
+ Mathparse is a standalone Python package and requires zero dependencies to function.
25
+
26
+ ## Language Support
27
+
28
+ The language parameter must be set in order to evaluate an equation that uses word operators.
29
+ The language code should be a valid [ISO 639-2](https://www.loc.gov/standards/iso639-2/php/code_list.php) language code.
30
+
31
+ ## Installation
32
+
33
+ ```bash
34
+ pip install mathparse
35
+ ```
36
+
37
+ ## Documentation
38
+
39
+ See the full documentation at https://mathparse.chatterbot.us
40
+
41
+ ## Changelog
42
+
43
+ See [release notes](https://github.com/gunthercox/mathparse/releases) for changes.
@@ -2,4 +2,4 @@
2
2
  mathparse is a library for solving mathematical equations contained in strings
3
3
  """
4
4
 
5
- __version__ = '0.2.1'
5
+ __version__ = '0.2.2'
@@ -0,0 +1,513 @@
1
+ """
2
+ Methods for evaluating mathematical equations in strings.
3
+ """
4
+ from decimal import Decimal
5
+ from typing import Union
6
+ from . import mathwords
7
+ import re
8
+
9
+
10
+ class PostfixTokenEvaluationException(Exception):
11
+ """
12
+ Exception to be raised when an expression cannot be evaluated.
13
+ """
14
+ pass
15
+
16
+
17
+ def is_int(string: str) -> bool:
18
+ """
19
+ Return true if string is an integer.
20
+ """
21
+ try:
22
+ int(string)
23
+ return True
24
+ except ValueError:
25
+ return False
26
+
27
+
28
+ def is_float(string: str) -> bool:
29
+ """
30
+ Return true if the string is a float.
31
+ """
32
+ try:
33
+ float(string)
34
+ return '.' in string
35
+ except ValueError:
36
+ return False
37
+
38
+
39
+ def is_constant(string: str) -> bool:
40
+ """
41
+ Return true if the string is a mathematical constant.
42
+ """
43
+ return mathwords.CONSTANTS.get(string, False)
44
+
45
+
46
+ def is_unary(string: str) -> bool:
47
+ """
48
+ Return true if the string is a defined unary mathematical
49
+ operator function.
50
+ """
51
+ return string in mathwords.UNARY_FUNCTIONS
52
+
53
+
54
+ def is_binary(string: str) -> bool:
55
+ """
56
+ Return true if the string is a defined binary operator.
57
+ """
58
+ return string in mathwords.BINARY_OPERATORS
59
+
60
+
61
+ def is_symbol(string: str) -> bool:
62
+ """
63
+ Return true if the string is a mathematical symbol.
64
+ """
65
+ return (
66
+ is_int(string) or is_float(string) or
67
+ is_constant(string) or is_unary(string) or
68
+ is_binary(string) or
69
+ (string == '(') or (string == ')')
70
+ )
71
+
72
+
73
+ def is_word(word: str, language: str) -> bool:
74
+ """
75
+ Return true if the word is a math word for the specified language.
76
+ """
77
+ words = mathwords.words_for_language(language)
78
+
79
+ return word in words
80
+
81
+
82
+ def find_word_groups(string: str, words: list) -> list:
83
+ """
84
+ Find matches for words in the format "3 thousand 6 hundred 2".
85
+ The words parameter should be the list of words to check for
86
+ such as "hundred".
87
+ """
88
+ scale_pattern = '|'.join(words)
89
+ # For example:
90
+ # (?:(?:\d+)\s+(?:hundred|thousand)*\s*)+(?:\d+|hundred|thousand)+
91
+ regex = re.compile(
92
+ r'(?:(?:\d+)\s+(?:' +
93
+ scale_pattern +
94
+ r')*\s*)+(?:\d+|' +
95
+ scale_pattern + r')+'
96
+ )
97
+ result = regex.findall(string)
98
+ return result
99
+
100
+
101
+ def replace_word_tokens(
102
+ string: str, language: str, stopwords: set[str] = None
103
+ ) -> str:
104
+ """
105
+ Replace word-based mathematical terms with their symbolic equivalents.
106
+
107
+ Given a string and an ISO 639-2 language code,
108
+ return the string with the words replaced with
109
+ an operational equivalent.
110
+ """
111
+ words = mathwords.word_groups_for_language(language)
112
+
113
+ # Replace binary operator words with numeric operators
114
+ binary_operators = words['binary_operators']
115
+ for operator in frozenset(binary_operators.keys()):
116
+ if operator in string:
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
+ )
127
+
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)")
140
+ numbers = words['numbers']
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)
185
+
186
+ # Replace scaling multipliers with numeric values
187
+ scales = words['scales']
188
+ end_index_characters = mathwords.BINARY_OPERATORS
189
+ end_index_characters.add('(')
190
+
191
+ word_matches = find_word_groups(string, frozenset(scales.keys()))
192
+
193
+ for match in word_matches:
194
+ string = string.replace(match, '(' + match + ')')
195
+
196
+ for scale in frozenset(scales.keys()):
197
+ for _ in range(0, string.count(scale)):
198
+ start_index = string.find(scale) - 1
199
+ end_index = len(string)
200
+
201
+ while is_int(string[start_index - 1]) and start_index > 0:
202
+ start_index -= 1
203
+
204
+ end_index = string.find(' ', start_index) + 1
205
+ end_index = string.find(' ', end_index) + 1
206
+
207
+ add = ' + '
208
+ if string[end_index] in end_index_characters:
209
+ add = ''
210
+
211
+ string = string[:start_index] + '(' + string[start_index:]
212
+ string = string.replace(
213
+ scale, '* ' + str(scales[scale]) + ')' + add,
214
+ 1
215
+ )
216
+
217
+ string = string.replace(') (', ') + (')
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
+
223
+ return string
224
+
225
+
226
+ def to_postfix(tokens: list) -> list:
227
+ """
228
+ Convert a list of evaluatable tokens to postfix format.
229
+ """
230
+ precedence = {
231
+ '/': 4,
232
+ '*': 4,
233
+ '+': 3,
234
+ '-': 3,
235
+ '^': 2,
236
+ '(': 1
237
+ }
238
+
239
+ # Unary functions have a higher precedence than binary operators
240
+ unary_precedence = max(precedence.values()) + 1
241
+
242
+ postfix = []
243
+ opstack = []
244
+
245
+ for token in tokens:
246
+ if is_int(token):
247
+ postfix.append(int(token))
248
+ elif is_float(token):
249
+ postfix.append(float(token))
250
+ elif token in mathwords.CONSTANTS:
251
+ postfix.append(mathwords.CONSTANTS[token])
252
+ elif is_unary(token):
253
+ opstack.append(token)
254
+ elif token == '(':
255
+ opstack.append(token)
256
+ elif token == ')':
257
+ top_token = opstack.pop()
258
+ while top_token != '(':
259
+ postfix.append(top_token)
260
+ top_token = opstack.pop()
261
+ elif is_binary(token):
262
+ # For binary operators, pop operators with higher or
263
+ # equal precedence
264
+ while (opstack != []) and (
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
+ )
275
+ ):
276
+ postfix.append(opstack.pop())
277
+ opstack.append(token)
278
+ else:
279
+ # Raise exception for unsupported mathematical terms
280
+ raise PostfixTokenEvaluationException(
281
+ 'Unsupported mathematical term: "{}"'.format(token)
282
+ )
283
+
284
+ while opstack != []:
285
+ postfix.append(opstack.pop())
286
+
287
+ return postfix
288
+
289
+
290
+ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
291
+ """
292
+ Given a list of evaluatable tokens in postfix format,
293
+ calculate a solution.
294
+ """
295
+ stack = []
296
+
297
+ for token in tokens:
298
+ total = None
299
+
300
+ if is_int(token) or is_float(token) or is_constant(token):
301
+ stack.append(token)
302
+ elif is_unary(token):
303
+ a = stack.pop()
304
+ total = mathwords.UNARY_FUNCTIONS[token](a)
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:
312
+ b = stack.pop()
313
+ a = stack.pop()
314
+ if token == '+':
315
+ total = a + b
316
+ elif token == '-':
317
+ total = a - b
318
+ elif token == '*':
319
+ total = a * b
320
+ elif token == '^':
321
+ total = a ** b
322
+ elif token == '/':
323
+ if Decimal(str(b)) == 0:
324
+ total = 'undefined'
325
+ else:
326
+ total = Decimal(str(a)) / Decimal(str(b))
327
+ else:
328
+ raise PostfixTokenEvaluationException(
329
+ 'Unknown token "{}"'.format(token)
330
+ )
331
+
332
+ if total is not None:
333
+ stack.append(total)
334
+
335
+ # If the stack is empty the tokens could not be evaluated
336
+ if not stack:
337
+ raise PostfixTokenEvaluationException(
338
+ 'The postfix expression resulted in an empty stack'
339
+ )
340
+
341
+ return stack.pop()
342
+
343
+
344
+ def tokenize(string: str, language: str = None, escape: str = '___') -> list:
345
+ """
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.
361
+ """
362
+ # Set all words to lowercase
363
+ string = string.lower()
364
+
365
+ # Ignore punctuation
366
+ if len(string) and not string[-1].isalnum():
367
+ character = string[-1]
368
+ string = string[:-1] + ' ' + character
369
+
370
+ # Parenthesis must have space around them to be tokenized properly
371
+ string = string.replace('(', ' ( ')
372
+ string = string.replace(')', ' ) ')
373
+
374
+ if language:
375
+ words = mathwords.words_for_language(language)
376
+
377
+ for phrase in words:
378
+ escaped_phrase = phrase.replace(' ', escape)
379
+ string = string.replace(phrase, escaped_phrase)
380
+
381
+ tokens = string.split()
382
+
383
+ for index, token in enumerate(tokens):
384
+ tokens[index] = token.replace(escape, ' ')
385
+
386
+ return tokens
387
+
388
+
389
+ def parse(
390
+ string: str, language: str = None, stopwords: set[str] = None
391
+ ) -> Union[int, float, str, Decimal]:
392
+ """
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
449
+ """
450
+ if language:
451
+ string = replace_word_tokens(string, language, stopwords)
452
+
453
+ tokens = tokenize(string)
454
+ postfix = to_postfix(tokens)
455
+
456
+ return evaluate_postfix(postfix)
457
+
458
+
459
+ def extract_expression(dirty_string: str, language: str) -> str:
460
+ """
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
495
+ """
496
+ tokens = tokenize(dirty_string, language)
497
+
498
+ start_index = 0
499
+ end_index = len(tokens)
500
+
501
+ for part in tokens:
502
+ if is_symbol(part) or is_word(part, language):
503
+ break
504
+ else:
505
+ start_index += 1
506
+
507
+ for part in reversed(tokens):
508
+ if is_symbol(part) or is_word(part, language):
509
+ break
510
+ else:
511
+ end_index -= 1
512
+
513
+ return ' '.join(tokens[start_index:end_index])
@@ -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
- 'unary_operators': {
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
- 'unary_operators': {
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
- 'unary_operators': {
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 given that
446
- is not a part of the ISO 639-2 standard.
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.extend(word_groups[group].keys())
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.1
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', mathparse.codes.ENG)
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
- ## Language support
63
+ ## Documentation
53
64
 
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.
65
+ See the full documentation at https://mathparse.chatterbot.us
56
66
 
57
- ## History
67
+ ## Changelog
58
68
 
59
- See [release notes](https://github.com/gunthercox/ChatterBot/releases) for changes.
69
+ See [release notes](https://github.com/gunthercox/mathparse/releases) for changes.
@@ -10,6 +10,8 @@ mathparse.egg-info/dependency_links.txt
10
10
  mathparse.egg-info/requires.txt
11
11
  mathparse.egg-info/top_level.txt
12
12
  tests/test_binary_operations.py
13
+ tests/test_complex_statements.py
14
+ tests/test_language_support.py
13
15
  tests/test_replace_word_tokens.py
14
16
  tests/test_unary_operations.py
15
17
  tests/test_utils.py
@@ -0,0 +1,54 @@
1
+ from unittest import TestCase
2
+ from mathparse import mathparse
3
+
4
+
5
+ class ComplexStatementsTestCase(TestCase):
6
+ """
7
+ Test cases for complex mathematical expressions.
8
+ """
9
+
10
+ def test_numbers_with_spaces(self):
11
+ result = mathparse.parse(
12
+ 'one hundred times fifty four', language='ENG'
13
+ )
14
+ self.assertEqual(result, 100 * 54)
15
+
16
+ def test_numeric_values_with_squared_operator(self):
17
+ result = mathparse.parse(
18
+ '10 plus 2 squared times 3', language='ENG'
19
+ )
20
+ self.assertEqual(result, 10 + 2 ** 2 * 3)
21
+
22
+ def test_numeric_values_with_square_root_operator(self):
23
+ result = mathparse.parse(
24
+ '10 plus the square root of 4 times 3',
25
+ language='ENG',
26
+ stopwords={'the'}
27
+ )
28
+ self.assertEqual(result, 10 + 2 * 3)
29
+
30
+ def test_long_worded_number(self):
31
+ result = mathparse.parse(
32
+ 'one thousand two hundred thirty four plus '
33
+ 'five thousand six hundred seventy eight',
34
+ language='ENG'
35
+ )
36
+ self.assertEqual(result, 1234 + 5678)
37
+
38
+ def test_invalid_expression(self):
39
+ with self.assertRaises(mathparse.PostfixTokenEvaluationException) as e:
40
+ mathparse.parse('two plus times three', language='ENG')
41
+
42
+ self.assertEqual(
43
+ str(e.exception),
44
+ 'Insufficient values in expression for operator "+"'
45
+ )
46
+
47
+ def test_unsupported_mathematical_term(self):
48
+ with self.assertRaises(mathparse.PostfixTokenEvaluationException) as e:
49
+ mathparse.parse('two squiggle eight', language='ENG')
50
+
51
+ self.assertEqual(
52
+ str(e.exception),
53
+ 'Unsupported mathematical term: "squiggle"'
54
+ )
@@ -0,0 +1,34 @@
1
+ from unittest import TestCase
2
+ from mathparse import mathparse
3
+ from mathparse.mathwords import InvalidLanguageCodeException
4
+
5
+
6
+ class LanguageSupportTestCase(TestCase):
7
+
8
+ def test_unsupported_language_code(self):
9
+ with self.assertRaises(InvalidLanguageCodeException) as e:
10
+ mathparse.parse('one plus one', language='123')
11
+
12
+ self.assertEqual(
13
+ str(e.exception),
14
+ '123 is not an available language code'
15
+ )
16
+
17
+ def test_parsing_with_unsupported_operator(self):
18
+ """
19
+ NOTE: The French language currently is missing support for
20
+ unary_operators. This test will start failing when those are
21
+ added to the language support.
22
+ """
23
+ with self.assertRaises(mathparse.PostfixTokenEvaluationException) as e:
24
+ mathparse.parse(
25
+ # Three times the square root of 4
26
+ 'trois fois la racine carrée de 4',
27
+ language='FRE',
28
+ stopwords={'la', 'de'}
29
+ )
30
+
31
+ self.assertEqual(
32
+ str(e.exception),
33
+ 'Unsupported mathematical term: "racine"'
34
+ )
@@ -0,0 +1,65 @@
1
+ from unittest import TestCase
2
+ from mathparse import mathparse
3
+
4
+
5
+ class EnglishWordTokenTestCase(TestCase):
6
+
7
+ def test_addition(self):
8
+ result = mathparse.replace_word_tokens('1 plus 1', language='ENG')
9
+
10
+ self.assertEqual(result, '1 + 1')
11
+
12
+ def test_thirty(self):
13
+ result = mathparse.replace_word_tokens(
14
+ 'thirty + thirty', language='ENG'
15
+ )
16
+
17
+ self.assertEqual(result, '30 + 30')
18
+
19
+ def test_thousand(self):
20
+ result = mathparse.replace_word_tokens(
21
+ 'five thousand + 30', language='ENG'
22
+ )
23
+
24
+ # Note: this ends up with double parentheses because it is both a
25
+ # scaled number ("thousand") and a word group ("five thousand")
26
+ self.assertEqual(result, '(5 * 1000) + 30')
27
+
28
+ def test_double_digit_multiplier_for_scale(self):
29
+ result = mathparse.replace_word_tokens(
30
+ 'fifty thousand + 1', language='ENG'
31
+ )
32
+
33
+ self.assertEqual(result, '(50 * 1000) + 1')
34
+
35
+ def test_numeric_values_with_squared_word_operator(self):
36
+ result = mathparse.replace_word_tokens(
37
+ '10 plus 2 squared times 3', language='ENG'
38
+ )
39
+ self.assertEqual(result, '10 + (2 ^ 2) * 3')
40
+
41
+ def test_numeric_values_with_squared_unary_operator(self):
42
+ result = mathparse.replace_word_tokens(
43
+ '10 plus the square root of 2 times 3',
44
+ language='ENG',
45
+ stopwords={'the'}
46
+ )
47
+ self.assertEqual(result, '10 + sqrt 2 * 3')
48
+
49
+ def test_tens_with_and_without_spaces(self):
50
+ result = mathparse.replace_word_tokens(
51
+ 'nineteen times fifty four', language='ENG'
52
+ )
53
+ self.assertEqual(result, '19 * (50 + 4)')
54
+
55
+ def test_tens_and_hundreds_with_spaces(self):
56
+ result = mathparse.replace_word_tokens(
57
+ 'one hundred times fifty four', language='ENG'
58
+ )
59
+ self.assertEqual(result, '(1 * 100) * (50 + 4)')
60
+
61
+ def test_hyphenated_numbers(self):
62
+ result = mathparse.replace_word_tokens(
63
+ 'one hundred times fifty-four', language='ENG'
64
+ )
65
+ self.assertEqual(result, '(1 * 100) * (50 + 4)')
@@ -1,5 +1,6 @@
1
1
  from unittest import TestCase
2
2
  from mathparse import mathparse
3
+ from mathparse.mathwords import InvalidLanguageCodeException
3
4
 
4
5
 
5
6
  class BooleanChecksTestCase(TestCase):
@@ -48,6 +49,10 @@ class TokenizationTestCase(TestCase):
48
49
 
49
50
  self.assertEqual(result, ['three', 'plus', 'five'])
50
51
 
52
+ def test_tokenize_invalid_language(self):
53
+ with self.assertRaises(InvalidLanguageCodeException):
54
+ mathparse.tokenize('Three PLUS five', language='123')
55
+
51
56
  def test_load_english_words(self):
52
57
  from mathparse import mathwords
53
58
 
mathparse-0.2.1/README.md DELETED
@@ -1,33 +0,0 @@
1
- # mathparse
2
-
3
- The `mathparse` library is a Python module designed to evaluate mathematical equations contained in strings.
4
-
5
- Here are a few examples:
6
-
7
- ```python
8
- from mathparse import mathparse
9
-
10
- mathparse.parse('50 * (85 / 100)')
11
- >>> 42.5
12
-
13
- mathparse.parse('one hundred times fifty four', mathparse.codes.ENG)
14
- >>> 5400
15
-
16
- mathparse.parse('(seven * nine) + 8 - (45 plus two)', language='ENG')
17
- >>> 24
18
- ```
19
-
20
- ## Installation
21
-
22
- ```bash
23
- pip install mathparse
24
- ```
25
-
26
- ## Language support
27
-
28
- The language parameter must be set in order to evaluate an equation that uses word operators.
29
- The language code should be a valid [ISO 639-2](https://www.loc.gov/standards/iso639-2/php/code_list.php) language code.
30
-
31
- ## History
32
-
33
- See [release notes](https://github.com/gunthercox/ChatterBot/releases) for changes.
@@ -1,319 +0,0 @@
1
- """
2
- Methods for evaluating mathematical equations in strings.
3
- """
4
- from decimal import Decimal
5
- from . import mathwords
6
- import re
7
-
8
-
9
- class PostfixTokenEvaluationException(Exception):
10
- """
11
- Exception to be raised when a language code is given that
12
- is not a part of the ISO 639-2 standard.
13
- """
14
- pass
15
-
16
-
17
- def is_int(string):
18
- """
19
- Return true if string is an integer.
20
- """
21
- try:
22
- int(string)
23
- return True
24
- except ValueError:
25
- return False
26
-
27
-
28
- def is_float(string):
29
- """
30
- Return true if the string is a float.
31
- """
32
- try:
33
- float(string)
34
- return '.' in string
35
- except ValueError:
36
- return False
37
-
38
-
39
- def is_constant(string):
40
- """
41
- Return true if the string is a mathematical constant.
42
- """
43
- return mathwords.CONSTANTS.get(string, False)
44
-
45
-
46
- def is_unary(string):
47
- """
48
- Return true if the string is a defined unary mathematical
49
- operator function.
50
- """
51
- return string in mathwords.UNARY_FUNCTIONS
52
-
53
-
54
- def is_binary(string):
55
- """
56
- Return true if the string is a defined binary operator.
57
- """
58
- return string in mathwords.BINARY_OPERATORS
59
-
60
-
61
- def is_symbol(string):
62
- """
63
- Return true if the string is a mathematical symbol.
64
- """
65
- return (
66
- is_int(string) or is_float(string) or
67
- is_constant(string) or is_unary(string) or
68
- is_binary(string) or
69
- (string == '(') or (string == ')')
70
- )
71
-
72
-
73
- def is_word(word, language):
74
- """
75
- Return true if the word is a math word for the specified language.
76
- """
77
- words = mathwords.words_for_language(language)
78
-
79
- return word in words
80
-
81
-
82
- def find_word_groups(string, words):
83
- """
84
- Find matches for words in the format "3 thousand 6 hundred 2".
85
- The words parameter should be the list of words to check for
86
- such as "hundred".
87
- """
88
- scale_pattern = '|'.join(words)
89
- # For example:
90
- # (?:(?:\d+)\s+(?:hundred|thousand)*\s*)+(?:\d+|hundred|thousand)+
91
- regex = re.compile(
92
- r'(?:(?:\d+)\s+(?:' +
93
- scale_pattern +
94
- r')*\s*)+(?:\d+|' +
95
- scale_pattern + r')+'
96
- )
97
- result = regex.findall(string)
98
- return result
99
-
100
-
101
- def replace_word_tokens(string, language):
102
- """
103
- Given a string and an ISO 639-2 language code,
104
- return the string with the words replaced with
105
- an operational equivalent.
106
- """
107
- words = mathwords.word_groups_for_language(language)
108
-
109
- # Replace operator words with numeric operators
110
- operators = words['binary_operators'].copy()
111
- if 'unary_operators' in words:
112
- operators.update(words['unary_operators'])
113
-
114
- for operator in list(operators.keys()):
115
- if operator in string:
116
- string = string.replace(operator, operators[operator])
117
-
118
- # Replace number words with numeric values
119
- numbers = words['numbers']
120
- for number in list(numbers.keys()):
121
- if number in string:
122
- string = string.replace(number, str(numbers[number]))
123
-
124
- # Replace scaling multipliers with numeric values
125
- scales = words['scales']
126
- end_index_characters = mathwords.BINARY_OPERATORS
127
- end_index_characters.add('(')
128
-
129
- word_matches = find_word_groups(string, list(scales.keys()))
130
-
131
- for match in word_matches:
132
- string = string.replace(match, '(' + match + ')')
133
-
134
- for scale in list(scales.keys()):
135
- for _ in range(0, string.count(scale)):
136
- start_index = string.find(scale) - 1
137
- end_index = len(string)
138
-
139
- while is_int(string[start_index - 1]) and start_index > 0:
140
- start_index -= 1
141
-
142
- end_index = string.find(' ', start_index) + 1
143
- end_index = string.find(' ', end_index) + 1
144
-
145
- add = ' + '
146
- if string[end_index] in end_index_characters:
147
- add = ''
148
-
149
- string = string[:start_index] + '(' + string[start_index:]
150
- string = string.replace(
151
- scale, '* ' + str(scales[scale]) + ')' + add,
152
- 1
153
- )
154
-
155
- string = string.replace(') (', ') + (')
156
-
157
- return string
158
-
159
-
160
- def to_postfix(tokens):
161
- """
162
- Convert a list of evaluatable tokens to postfix format.
163
- """
164
- precedence = {
165
- '/': 4,
166
- '*': 4,
167
- '+': 3,
168
- '-': 3,
169
- '^': 2,
170
- '(': 1
171
- }
172
-
173
- postfix = []
174
- opstack = []
175
-
176
- for token in tokens:
177
- if is_int(token):
178
- postfix.append(int(token))
179
- elif is_float(token):
180
- postfix.append(float(token))
181
- elif token in mathwords.CONSTANTS:
182
- postfix.append(mathwords.CONSTANTS[token])
183
- elif is_unary(token):
184
- opstack.append(token)
185
- elif token == '(':
186
- opstack.append(token)
187
- elif token == ')':
188
- top_token = opstack.pop()
189
- while top_token != '(':
190
- postfix.append(top_token)
191
- top_token = opstack.pop()
192
- else:
193
- while (opstack != []) and (
194
- precedence[opstack[-1]] >= precedence[token]
195
- ):
196
- postfix.append(opstack.pop())
197
- opstack.append(token)
198
-
199
- while opstack != []:
200
- postfix.append(opstack.pop())
201
-
202
- return postfix
203
-
204
-
205
- def evaluate_postfix(tokens):
206
- """
207
- Given a list of evaluatable tokens in postfix format,
208
- calculate a solution.
209
- """
210
- stack = []
211
-
212
- for token in tokens:
213
- total = None
214
-
215
- if is_int(token) or is_float(token) or is_constant(token):
216
- stack.append(token)
217
- elif is_unary(token):
218
- a = stack.pop()
219
- total = mathwords.UNARY_FUNCTIONS[token](a)
220
- elif len(stack):
221
- b = stack.pop()
222
- a = stack.pop()
223
- if token == '+':
224
- total = a + b
225
- elif token == '-':
226
- total = a - b
227
- elif token == '*':
228
- total = a * b
229
- elif token == '^':
230
- total = a ** b
231
- elif token == '/':
232
- if Decimal(str(b)) == 0:
233
- total = 'undefined'
234
- else:
235
- total = Decimal(str(a)) / Decimal(str(b))
236
- else:
237
- raise PostfixTokenEvaluationException(
238
- 'Unknown token {}'.format(token)
239
- )
240
-
241
- if total is not None:
242
- stack.append(total)
243
-
244
- # If the stack is empty the tokens could not be evaluated
245
- if not stack:
246
- raise PostfixTokenEvaluationException(
247
- 'The postfix expression resulted in an empty stack'
248
- )
249
-
250
- return stack.pop()
251
-
252
-
253
- def tokenize(string, language=None, escape='___'):
254
- """
255
- Given a string, return a list of math symbol tokens
256
- """
257
- # Set all words to lowercase
258
- string = string.lower()
259
-
260
- # Ignore punctuation
261
- if len(string) and not string[-1].isalnum():
262
- character = string[-1]
263
- string = string[:-1] + ' ' + character
264
-
265
- # Parenthesis must have space around them to be tokenized properly
266
- string = string.replace('(', ' ( ')
267
- string = string.replace(')', ' ) ')
268
-
269
- if language:
270
- words = mathwords.words_for_language(language)
271
-
272
- for phrase in words:
273
- escaped_phrase = phrase.replace(' ', escape)
274
- string = string.replace(phrase, escaped_phrase)
275
-
276
- tokens = string.split()
277
-
278
- for index, token in enumerate(tokens):
279
- tokens[index] = token.replace(escape, ' ')
280
-
281
- return tokens
282
-
283
-
284
- def parse(string, language=None):
285
- """
286
- Return a solution to the equation in the input string.
287
- """
288
- if language:
289
- string = replace_word_tokens(string, language)
290
-
291
- tokens = tokenize(string)
292
- postfix = to_postfix(tokens)
293
-
294
- return evaluate_postfix(postfix)
295
-
296
-
297
- def extract_expression(dirty_string, language):
298
- """
299
- Give a string such as: "What is 4 + 4?"
300
- Return the string "4 + 4"
301
- """
302
- tokens = tokenize(dirty_string, language)
303
-
304
- start_index = 0
305
- end_index = len(tokens)
306
-
307
- for part in tokens:
308
- if is_symbol(part) or is_word(part, language):
309
- break
310
- else:
311
- start_index += 1
312
-
313
- for part in reversed(tokens):
314
- if is_symbol(part) or is_word(part, language):
315
- break
316
- else:
317
- end_index -= 1
318
-
319
- return ' '.join(tokens[start_index:end_index])
@@ -1,33 +0,0 @@
1
- from unittest import TestCase
2
- from mathparse import mathparse
3
-
4
-
5
- class EnglishWordTokenTestCase(TestCase):
6
-
7
- def test_addition(self):
8
- result = mathparse.replace_word_tokens('1 plus 1', language='ENG')
9
-
10
- self.assertEqual(result, '1 + 1')
11
-
12
- def test_thirty(self):
13
- result = mathparse.replace_word_tokens(
14
- 'thirty + thirty', language='ENG'
15
- )
16
-
17
- self.assertEqual(result, '30 + 30')
18
-
19
- def test_thousand(self):
20
- result = mathparse.replace_word_tokens(
21
- 'five thousand + 30', language='ENG'
22
- )
23
-
24
- # Note: this ends up with double parentheses because it is both a
25
- # scaled number ("thousand") and a word group ("five thousand")
26
- self.assertEqual(result, '((5 * 1000)) + 30')
27
-
28
- def test_double_digit_multiplier_for_scale(self):
29
- result = mathparse.replace_word_tokens(
30
- 'fifty thousand + 1', language='ENG'
31
- )
32
-
33
- self.assertEqual(result, '((50 * 1000)) + 1')
File without changes
File without changes
File without changes