mathparse 0.2.3__tar.gz → 0.2.5__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.
- {mathparse-0.2.3 → mathparse-0.2.5}/PKG-INFO +1 -1
- {mathparse-0.2.3 → mathparse-0.2.5}/mathparse/__init__.py +1 -1
- {mathparse-0.2.3 → mathparse-0.2.5}/mathparse/mathparse.py +117 -11
- mathparse-0.2.5/mathparse/mathwords.py +1723 -0
- {mathparse-0.2.3 → mathparse-0.2.5}/mathparse.egg-info/PKG-INFO +1 -1
- {mathparse-0.2.3 → mathparse-0.2.5}/mathparse.egg-info/SOURCES.txt +1 -0
- {mathparse-0.2.3 → mathparse-0.2.5}/tests/test_binary_operations.py +53 -0
- {mathparse-0.2.3 → mathparse-0.2.5}/tests/test_complex_statements.py +14 -0
- mathparse-0.2.5/tests/test_prefix_unary_operations.py +243 -0
- mathparse-0.2.5/tests/test_unary_operations.py +138 -0
- {mathparse-0.2.3 → mathparse-0.2.5}/tests/test_utils.py +15 -0
- mathparse-0.2.3/mathparse/mathwords.py +0 -839
- mathparse-0.2.3/tests/test_unary_operations.py +0 -43
- {mathparse-0.2.3 → mathparse-0.2.5}/LICENSE +0 -0
- {mathparse-0.2.3 → mathparse-0.2.5}/README.md +0 -0
- {mathparse-0.2.3 → mathparse-0.2.5}/mathparse.egg-info/dependency_links.txt +0 -0
- {mathparse-0.2.3 → mathparse-0.2.5}/mathparse.egg-info/requires.txt +0 -0
- {mathparse-0.2.3 → mathparse-0.2.5}/mathparse.egg-info/top_level.txt +0 -0
- {mathparse-0.2.3 → mathparse-0.2.5}/pyproject.toml +0 -0
- {mathparse-0.2.3 → mathparse-0.2.5}/setup.cfg +0 -0
- {mathparse-0.2.3 → mathparse-0.2.5}/tests/test_language_support.py +0 -0
- {mathparse-0.2.3 → mathparse-0.2.5}/tests/test_replace_word_tokens.py +0 -0
- {mathparse-0.2.3 → mathparse-0.2.5}/tests/test_simplified_chinese.py +0 -0
|
@@ -79,6 +79,41 @@ def is_word(word: str, language: str) -> bool:
|
|
|
79
79
|
return word in words
|
|
80
80
|
|
|
81
81
|
|
|
82
|
+
def create_unicode_word_boundary_pattern(word: str) -> str:
|
|
83
|
+
"""
|
|
84
|
+
Create a regex pattern with Unicode-aware word boundaries.
|
|
85
|
+
|
|
86
|
+
Standard regex \\b word boundaries don't work with non-ASCII characters
|
|
87
|
+
(e.g., Devanagari, Arabic, Hebrew, Chinese, Thai, etc.). This function
|
|
88
|
+
creates a pattern that works across all Unicode scripts.
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
word (str): The word to create a boundary pattern for
|
|
92
|
+
|
|
93
|
+
Returns:
|
|
94
|
+
str: A regex pattern string with proper Unicode boundaries
|
|
95
|
+
|
|
96
|
+
Examples:
|
|
97
|
+
>>> create_unicode_word_boundary_pattern("two")
|
|
98
|
+
'(?<![\\w])two(?![\\w])'
|
|
99
|
+
>>> create_unicode_word_boundary_pattern("दो") # Hindi "two"
|
|
100
|
+
'(?:^|(?<=[\\s+\\-*/^()]))दो(?:$|(?=[\\s+\\-*/^()]))'
|
|
101
|
+
"""
|
|
102
|
+
# Check if word contains only ASCII alphanumeric characters
|
|
103
|
+
if word.isascii() and word.replace('-', '').replace("'", '').isalnum():
|
|
104
|
+
# For ASCII words, use standard word boundaries
|
|
105
|
+
return r'\b' + re.escape(word) + r'\b'
|
|
106
|
+
else:
|
|
107
|
+
# For non-ASCII (Unicode) words, use space/operator boundaries
|
|
108
|
+
# This pattern matches the word when it's:
|
|
109
|
+
# - At the start of string OR preceded by whitespace/operator
|
|
110
|
+
# - At the end of string OR followed by whitespace/operator
|
|
111
|
+
escaped_word = re.escape(word)
|
|
112
|
+
# Boundary characters: whitespace, operators, parentheses
|
|
113
|
+
boundary = r'[\s+\-*/^()]'
|
|
114
|
+
return f'(?:^|(?<={boundary})){escaped_word}(?:$|(?={boundary}))'
|
|
115
|
+
|
|
116
|
+
|
|
82
117
|
def find_word_groups(string: str, words: list) -> list:
|
|
83
118
|
"""
|
|
84
119
|
Find matches for words in the format "3 thousand 6 hundred 2".
|
|
@@ -302,9 +337,10 @@ def replace_word_tokens(
|
|
|
302
337
|
|
|
303
338
|
# Replace number words with numeric values
|
|
304
339
|
for number in frozenset(numbers.keys()):
|
|
305
|
-
# Use word boundaries to prevent partial matches
|
|
306
|
-
# (e.g., "nine" in "nineteen")
|
|
307
|
-
|
|
340
|
+
# Use Unicode-aware word boundaries to prevent partial matches
|
|
341
|
+
# (e.g., "nine" in "nineteen") and support non-ASCII scripts
|
|
342
|
+
# (e.g., Devanagari, Arabic, Hebrew, Chinese, etc.)
|
|
343
|
+
pattern = create_unicode_word_boundary_pattern(number)
|
|
308
344
|
if re.search(pattern, string):
|
|
309
345
|
string = re.sub(pattern, str(numbers[number]), string)
|
|
310
346
|
|
|
@@ -364,13 +400,15 @@ def preprocess_unary_operators(tokens: list) -> list:
|
|
|
364
400
|
* At the beginning of the expression
|
|
365
401
|
* After an opening parenthesis '('
|
|
366
402
|
* After a binary operator `(+, -, *, /, ^)`
|
|
403
|
+
* After a unary function `(sqrt, log, neg)`
|
|
367
404
|
"""
|
|
368
405
|
if not tokens:
|
|
369
406
|
return tokens
|
|
370
407
|
|
|
371
408
|
processed_tokens = []
|
|
372
409
|
|
|
373
|
-
|
|
410
|
+
# A following minus sign should be treated as unary (negative)
|
|
411
|
+
unary_contexts = mathwords.BINARY_OPERATORS | {'('}
|
|
374
412
|
|
|
375
413
|
for i, token in enumerate(tokens):
|
|
376
414
|
if token == '-':
|
|
@@ -382,8 +420,9 @@ def preprocess_unary_operators(tokens: list) -> list:
|
|
|
382
420
|
is_unary_minus = True
|
|
383
421
|
elif i > 0:
|
|
384
422
|
prev_token = tokens[i - 1]
|
|
385
|
-
# A unary minus after opening parenthesis
|
|
386
|
-
|
|
423
|
+
# A unary minus after opening parenthesis, binary operators,
|
|
424
|
+
# or unary functions
|
|
425
|
+
if prev_token in unary_contexts or is_unary(prev_token):
|
|
387
426
|
is_unary_minus = True
|
|
388
427
|
|
|
389
428
|
if is_unary_minus:
|
|
@@ -403,6 +442,7 @@ def to_postfix(tokens: list) -> list:
|
|
|
403
442
|
Convert a list of evaluatable tokens to postfix format.
|
|
404
443
|
"""
|
|
405
444
|
precedence = {
|
|
445
|
+
'.': 5,
|
|
406
446
|
'/': 4,
|
|
407
447
|
'*': 4,
|
|
408
448
|
'+': 3,
|
|
@@ -499,6 +539,23 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
|
499
539
|
total = 'undefined'
|
|
500
540
|
else:
|
|
501
541
|
total = Decimal(str(a)) / Decimal(str(b))
|
|
542
|
+
elif token == '.':
|
|
543
|
+
# Treat decimal points as a binary operator that combines the
|
|
544
|
+
# integer and fractional part of two numbers
|
|
545
|
+
# Example: 53 . 25 = 53.25, -3 . 5 = -3.5
|
|
546
|
+
if b == 0:
|
|
547
|
+
total = Decimal(a)
|
|
548
|
+
else:
|
|
549
|
+
# Count the digits in b to determine the divisor
|
|
550
|
+
digits = len(str(int(b)))
|
|
551
|
+
divisor = 10 ** digits
|
|
552
|
+
fractional_part = b / divisor
|
|
553
|
+
# Handle negative numbers correctly: -3 . 5 should be -3.5,
|
|
554
|
+
# not -2.5
|
|
555
|
+
if a < 0:
|
|
556
|
+
total = a - fractional_part
|
|
557
|
+
else:
|
|
558
|
+
total = a + fractional_part
|
|
502
559
|
else:
|
|
503
560
|
raise PostfixTokenEvaluationException(
|
|
504
561
|
'Unknown token "{}"'.format(token)
|
|
@@ -564,8 +621,22 @@ def tokenize(string: str, language: str = None, escape: str = '___') -> list:
|
|
|
564
621
|
string = string.replace(spaced_phrase, phrase)
|
|
565
622
|
|
|
566
623
|
# Binary operators must have space around them to be tokenized properly
|
|
624
|
+
# Special handling for minus sign: preserve leading negatives
|
|
567
625
|
for operator in mathwords.BINARY_OPERATORS:
|
|
568
|
-
|
|
626
|
+
if operator == '-':
|
|
627
|
+
# For minus sign, use a pattern that only spaces it when it's
|
|
628
|
+
# clearly a binary operator (after digits or closing parenthesis).
|
|
629
|
+
# This preserves leading negatives like "-3" and distinguishes
|
|
630
|
+
# between:
|
|
631
|
+
# - "What is -3 + 3" --> "-3 + 3" (minus is part of number)
|
|
632
|
+
# - "5 - 3" --> "5 - 3" (minus is binary operator, needs spacing)
|
|
633
|
+
# - "math - 4" --> "- 4" (minus after letters is not a binary
|
|
634
|
+
# operator)
|
|
635
|
+
# Only add spaces around minus when preceded by a digit or closing
|
|
636
|
+
# parenthesis
|
|
637
|
+
string = re.sub(r'([\d)])\s*-\s*', r'\1 - ', string)
|
|
638
|
+
else:
|
|
639
|
+
string = string.replace(operator, f' {operator} ')
|
|
569
640
|
|
|
570
641
|
# Parenthesis must have space around them to be tokenized properly
|
|
571
642
|
string = string.replace('(', ' ( ')
|
|
@@ -704,11 +775,46 @@ def extract_expression(dirty_string: str, language: str) -> str:
|
|
|
704
775
|
start_index = 0
|
|
705
776
|
end_index = len(tokens)
|
|
706
777
|
|
|
707
|
-
|
|
778
|
+
# Find the start of the mathematical expression
|
|
779
|
+
# Skip over non-mathematical tokens AND isolated binary operators
|
|
780
|
+
for i, part in enumerate(tokens):
|
|
708
781
|
if is_symbol(part) or is_word(part, language):
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
782
|
+
# A potential start was found, so check if it's a standalone binary
|
|
783
|
+
# operator. Binary operators (except '(') are only valid at the
|
|
784
|
+
# start if they are unary (such as a leading minus for a negative
|
|
785
|
+
# number)
|
|
786
|
+
if part in mathwords.BINARY_OPERATORS and part != '(':
|
|
787
|
+
# For a binary operator to be the start of an expression, it
|
|
788
|
+
# must be:
|
|
789
|
+
# 1. At the very beginning (position 0) - could be unary minus
|
|
790
|
+
# 2. OR all previous tokens were non-mathematical - also could
|
|
791
|
+
# be unary
|
|
792
|
+
# If there were non-math tokens before it, it's likely a
|
|
793
|
+
# separator
|
|
794
|
+
|
|
795
|
+
# Check if all previous tokens are non-mathematical
|
|
796
|
+
all_prev_non_math = True
|
|
797
|
+
for j in range(i):
|
|
798
|
+
if is_symbol(tokens[j]) or is_word(tokens[j], language):
|
|
799
|
+
all_prev_non_math = False
|
|
800
|
+
break
|
|
801
|
+
|
|
802
|
+
# Only include this operator if at start OR all previous
|
|
803
|
+
# non-math AND followed by a mathematical token
|
|
804
|
+
if all_prev_non_math and i + 1 < len(tokens):
|
|
805
|
+
next_token = tokens[i + 1]
|
|
806
|
+
if (
|
|
807
|
+
is_int(next_token) or is_float(next_token) or
|
|
808
|
+
is_constant(next_token) or is_unary(next_token) or
|
|
809
|
+
next_token == '(' or is_word(next_token, language)
|
|
810
|
+
):
|
|
811
|
+
start_index = i
|
|
812
|
+
break
|
|
813
|
+
else:
|
|
814
|
+
# Start indexes can be an opening parenthesis, or a non-binary
|
|
815
|
+
# operator
|
|
816
|
+
start_index = i
|
|
817
|
+
break
|
|
712
818
|
|
|
713
819
|
for part in reversed(tokens):
|
|
714
820
|
if is_symbol(part) or is_word(part, language):
|