mathparse 0.2.4__py3-none-any.whl → 0.2.6__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 +141 -28
- {mathparse-0.2.4.dist-info → mathparse-0.2.6.dist-info}/METADATA +1 -1
- mathparse-0.2.6.dist-info/RECORD +8 -0
- mathparse-0.2.4.dist-info/RECORD +0 -8
- {mathparse-0.2.4.dist-info → mathparse-0.2.6.dist-info}/WHEEL +0 -0
- {mathparse-0.2.4.dist-info → mathparse-0.2.6.dist-info}/licenses/LICENSE +0 -0
- {mathparse-0.2.4.dist-info → mathparse-0.2.6.dist-info}/top_level.txt +0 -0
mathparse/__init__.py
CHANGED
mathparse/mathparse.py
CHANGED
|
@@ -18,11 +18,13 @@ def is_int(string: str) -> bool:
|
|
|
18
18
|
"""
|
|
19
19
|
Return true if string is an integer.
|
|
20
20
|
"""
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
# Allow leading minus sign
|
|
22
|
+
if string[0] == '-':
|
|
23
|
+
# Must have at least one digit after the minus
|
|
24
|
+
return len(string) > 1 and string[1:].isdigit()
|
|
25
|
+
else:
|
|
26
|
+
# Must be all digits
|
|
27
|
+
return string.isdigit()
|
|
26
28
|
|
|
27
29
|
|
|
28
30
|
def is_float(string: str) -> bool:
|
|
@@ -79,6 +81,27 @@ def is_word(word: str, language: str) -> bool:
|
|
|
79
81
|
return word in words
|
|
80
82
|
|
|
81
83
|
|
|
84
|
+
def to_number(val) -> Union[int, float, str, Decimal]:
|
|
85
|
+
"""
|
|
86
|
+
Convert a string to an int or float if possible.
|
|
87
|
+
"""
|
|
88
|
+
# If already a number (int, float, Decimal), return as-is
|
|
89
|
+
if isinstance(val, (int, float, Decimal)):
|
|
90
|
+
return val
|
|
91
|
+
|
|
92
|
+
# Check if it's a constant and convert
|
|
93
|
+
if is_constant(val):
|
|
94
|
+
return mathwords.CONSTANTS[val]
|
|
95
|
+
|
|
96
|
+
# Otherwise, try to convert string to number
|
|
97
|
+
if is_int(val):
|
|
98
|
+
return int(val)
|
|
99
|
+
elif is_float(val):
|
|
100
|
+
return float(val)
|
|
101
|
+
|
|
102
|
+
return val
|
|
103
|
+
|
|
104
|
+
|
|
82
105
|
def create_unicode_word_boundary_pattern(word: str) -> str:
|
|
83
106
|
"""
|
|
84
107
|
Create a regex pattern with Unicode-aware word boundaries.
|
|
@@ -400,13 +423,15 @@ def preprocess_unary_operators(tokens: list) -> list:
|
|
|
400
423
|
* At the beginning of the expression
|
|
401
424
|
* After an opening parenthesis '('
|
|
402
425
|
* After a binary operator `(+, -, *, /, ^)`
|
|
426
|
+
* After a unary function `(sqrt, log, neg)`
|
|
403
427
|
"""
|
|
404
428
|
if not tokens:
|
|
405
429
|
return tokens
|
|
406
430
|
|
|
407
431
|
processed_tokens = []
|
|
408
432
|
|
|
409
|
-
|
|
433
|
+
# A following minus sign should be treated as unary (negative)
|
|
434
|
+
unary_contexts = mathwords.BINARY_OPERATORS | {'('}
|
|
410
435
|
|
|
411
436
|
for i, token in enumerate(tokens):
|
|
412
437
|
if token == '-':
|
|
@@ -418,8 +443,9 @@ def preprocess_unary_operators(tokens: list) -> list:
|
|
|
418
443
|
is_unary_minus = True
|
|
419
444
|
elif i > 0:
|
|
420
445
|
prev_token = tokens[i - 1]
|
|
421
|
-
# A unary minus after opening parenthesis
|
|
422
|
-
|
|
446
|
+
# A unary minus after opening parenthesis, binary operators,
|
|
447
|
+
# or unary functions
|
|
448
|
+
if prev_token in unary_contexts or is_unary(prev_token):
|
|
423
449
|
is_unary_minus = True
|
|
424
450
|
|
|
425
451
|
if is_unary_minus:
|
|
@@ -456,11 +482,11 @@ def to_postfix(tokens: list) -> list:
|
|
|
456
482
|
|
|
457
483
|
for token in tokens:
|
|
458
484
|
if is_int(token):
|
|
459
|
-
postfix.append(
|
|
485
|
+
postfix.append(token)
|
|
460
486
|
elif is_float(token):
|
|
461
|
-
postfix.append(
|
|
487
|
+
postfix.append(token)
|
|
462
488
|
elif token in mathwords.CONSTANTS:
|
|
463
|
-
postfix.append(
|
|
489
|
+
postfix.append(token)
|
|
464
490
|
elif is_unary(token):
|
|
465
491
|
opstack.append(token)
|
|
466
492
|
elif token == '(':
|
|
@@ -513,6 +539,8 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
|
513
539
|
stack.append(token)
|
|
514
540
|
elif is_unary(token):
|
|
515
541
|
a = stack.pop()
|
|
542
|
+
# Convert token (string) to number for unary function evaluation
|
|
543
|
+
a = to_number(a)
|
|
516
544
|
total = mathwords.UNARY_FUNCTIONS[token](a)
|
|
517
545
|
elif len(stack) == 1:
|
|
518
546
|
raise PostfixTokenEvaluationException(
|
|
@@ -523,14 +551,15 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
|
523
551
|
elif len(stack) > 1:
|
|
524
552
|
b = stack.pop()
|
|
525
553
|
a = stack.pop()
|
|
554
|
+
|
|
526
555
|
if token == '+':
|
|
527
|
-
total = a + b
|
|
556
|
+
total = to_number(a) + to_number(b)
|
|
528
557
|
elif token == '-':
|
|
529
|
-
total = a - b
|
|
558
|
+
total = to_number(a) - to_number(b)
|
|
530
559
|
elif token == '*':
|
|
531
|
-
total = a * b
|
|
560
|
+
total = to_number(a) * to_number(b)
|
|
532
561
|
elif token == '^':
|
|
533
|
-
total = a ** b
|
|
562
|
+
total = to_number(a) ** to_number(b)
|
|
534
563
|
elif token == '/':
|
|
535
564
|
if Decimal(str(b)) == 0:
|
|
536
565
|
total = 'undefined'
|
|
@@ -539,14 +568,34 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
|
539
568
|
elif token == '.':
|
|
540
569
|
# Treat decimal points as a binary operator that combines the
|
|
541
570
|
# integer and fractional part of two numbers
|
|
542
|
-
# Example: 53 . 25 = 53.25
|
|
543
|
-
|
|
544
|
-
|
|
571
|
+
# Example: 53 . 25 = 53.25, -3 . 5 = -3.5
|
|
572
|
+
|
|
573
|
+
# Convert b to number
|
|
574
|
+
numeric_b = to_number(b)
|
|
575
|
+
|
|
576
|
+
if numeric_b == 0:
|
|
577
|
+
# Convert a to number
|
|
578
|
+
numeric_a = to_number(a)
|
|
579
|
+
total = Decimal(numeric_a)
|
|
545
580
|
else:
|
|
546
|
-
#
|
|
547
|
-
|
|
581
|
+
# Check if 'a' has a negative sign (handles -0 case)
|
|
582
|
+
is_negative = str(a).startswith('-')
|
|
583
|
+
|
|
584
|
+
# Convert a to number for calculation
|
|
585
|
+
numeric_a = to_number(a)
|
|
586
|
+
|
|
587
|
+
# Count the digits in the original string b to preserve
|
|
588
|
+
# leading zeros (e.g., "01" has 2 digits, not 1)
|
|
589
|
+
digits = len(str(b))
|
|
548
590
|
divisor = 10 ** digits
|
|
549
|
-
|
|
591
|
+
fractional_part = numeric_b / divisor
|
|
592
|
+
|
|
593
|
+
# Handle negatives: -3 . 5 = -3.5, not -2.5
|
|
594
|
+
# Also -0 . 5 = -0.5 (check string since -0 == 0)
|
|
595
|
+
if is_negative:
|
|
596
|
+
total = numeric_a - fractional_part
|
|
597
|
+
else:
|
|
598
|
+
total = numeric_a + fractional_part
|
|
550
599
|
else:
|
|
551
600
|
raise PostfixTokenEvaluationException(
|
|
552
601
|
'Unknown token "{}"'.format(token)
|
|
@@ -561,7 +610,16 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
|
561
610
|
'The postfix expression resulted in an empty stack'
|
|
562
611
|
)
|
|
563
612
|
|
|
564
|
-
|
|
613
|
+
result = stack.pop()
|
|
614
|
+
|
|
615
|
+
# Convert final result from string to number if needed
|
|
616
|
+
if isinstance(result, str):
|
|
617
|
+
if is_int(result):
|
|
618
|
+
result = int(result)
|
|
619
|
+
elif is_float(result):
|
|
620
|
+
result = float(result)
|
|
621
|
+
|
|
622
|
+
return result
|
|
565
623
|
|
|
566
624
|
|
|
567
625
|
def tokenize(string: str, language: str = None, escape: str = '___') -> list:
|
|
@@ -612,8 +670,22 @@ def tokenize(string: str, language: str = None, escape: str = '___') -> list:
|
|
|
612
670
|
string = string.replace(spaced_phrase, phrase)
|
|
613
671
|
|
|
614
672
|
# Binary operators must have space around them to be tokenized properly
|
|
673
|
+
# Special handling for minus sign: preserve leading negatives
|
|
615
674
|
for operator in mathwords.BINARY_OPERATORS:
|
|
616
|
-
|
|
675
|
+
if operator == '-':
|
|
676
|
+
# For minus sign, use a pattern that only spaces it when it's
|
|
677
|
+
# clearly a binary operator (after digits or closing parenthesis).
|
|
678
|
+
# This preserves leading negatives like "-3" and distinguishes
|
|
679
|
+
# between:
|
|
680
|
+
# - "What is -3 + 3" --> "-3 + 3" (minus is part of number)
|
|
681
|
+
# - "5 - 3" --> "5 - 3" (minus is binary operator, needs spacing)
|
|
682
|
+
# - "math - 4" --> "- 4" (minus after letters is not a binary
|
|
683
|
+
# operator)
|
|
684
|
+
# Only add spaces around minus when preceded by a digit or closing
|
|
685
|
+
# parenthesis
|
|
686
|
+
string = re.sub(r'([\d)])\s*-\s*', r'\1 - ', string)
|
|
687
|
+
else:
|
|
688
|
+
string = string.replace(operator, f' {operator} ')
|
|
617
689
|
|
|
618
690
|
# Parenthesis must have space around them to be tokenized properly
|
|
619
691
|
string = string.replace('(', ' ( ')
|
|
@@ -752,11 +824,46 @@ def extract_expression(dirty_string: str, language: str) -> str:
|
|
|
752
824
|
start_index = 0
|
|
753
825
|
end_index = len(tokens)
|
|
754
826
|
|
|
755
|
-
|
|
827
|
+
# Find the start of the mathematical expression
|
|
828
|
+
# Skip over non-mathematical tokens AND isolated binary operators
|
|
829
|
+
for i, part in enumerate(tokens):
|
|
756
830
|
if is_symbol(part) or is_word(part, language):
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
831
|
+
# A potential start was found, so check if it's a standalone binary
|
|
832
|
+
# operator. Binary operators (except '(') are only valid at the
|
|
833
|
+
# start if they are unary (such as a leading minus for a negative
|
|
834
|
+
# number)
|
|
835
|
+
if part in mathwords.BINARY_OPERATORS and part != '(':
|
|
836
|
+
# For a binary operator to be the start of an expression, it
|
|
837
|
+
# must be:
|
|
838
|
+
# 1. At the very beginning (position 0) - could be unary minus
|
|
839
|
+
# 2. OR all previous tokens were non-mathematical - also could
|
|
840
|
+
# be unary
|
|
841
|
+
# If there were non-math tokens before it, it's likely a
|
|
842
|
+
# separator
|
|
843
|
+
|
|
844
|
+
# Check if all previous tokens are non-mathematical
|
|
845
|
+
all_prev_non_math = True
|
|
846
|
+
for j in range(i):
|
|
847
|
+
if is_symbol(tokens[j]) or is_word(tokens[j], language):
|
|
848
|
+
all_prev_non_math = False
|
|
849
|
+
break
|
|
850
|
+
|
|
851
|
+
# Only include this operator if at start OR all previous
|
|
852
|
+
# non-math AND followed by a mathematical token
|
|
853
|
+
if all_prev_non_math and i + 1 < len(tokens):
|
|
854
|
+
next_token = tokens[i + 1]
|
|
855
|
+
if (
|
|
856
|
+
is_int(next_token) or is_float(next_token) or
|
|
857
|
+
is_constant(next_token) or is_unary(next_token) or
|
|
858
|
+
next_token == '(' or is_word(next_token, language)
|
|
859
|
+
):
|
|
860
|
+
start_index = i
|
|
861
|
+
break
|
|
862
|
+
else:
|
|
863
|
+
# Start indexes can be an opening parenthesis, or a non-binary
|
|
864
|
+
# operator
|
|
865
|
+
start_index = i
|
|
866
|
+
break
|
|
760
867
|
|
|
761
868
|
for part in reversed(tokens):
|
|
762
869
|
if is_symbol(part) or is_word(part, language):
|
|
@@ -764,4 +871,10 @@ def extract_expression(dirty_string: str, language: str) -> str:
|
|
|
764
871
|
else:
|
|
765
872
|
end_index -= 1
|
|
766
873
|
|
|
767
|
-
|
|
874
|
+
result = ' '.join(tokens[start_index:end_index])
|
|
875
|
+
|
|
876
|
+
# Remove spaces around decimal points for cleaner output
|
|
877
|
+
# Replace " . " with "." to convert "-100 . 5" to "-100.5"
|
|
878
|
+
result = result.replace(' . ', '.')
|
|
879
|
+
|
|
880
|
+
return result
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
mathparse/__init__.py,sha256=W_RR0JdfkLI6xTdLLMADVg-Qr4t0w4RRUr_vrHP5h90,110
|
|
2
|
+
mathparse/mathparse.py,sha256=KYuV1DIYxL4p7yscUZyQhOt_ucgH37icVvzmSfD73CM,31136
|
|
3
|
+
mathparse/mathwords.py,sha256=0SA6rG4X7lSRkhaHr-oIC1g6HFKCFJysqeyLASbALLo,48306
|
|
4
|
+
mathparse-0.2.6.dist-info/licenses/LICENSE,sha256=_buf4D_BxjT3x8SBhP0Rh966qP3zpbButcLI4bPq_TY,1068
|
|
5
|
+
mathparse-0.2.6.dist-info/METADATA,sha256=MUkys02qKEdCGTjY_KclV1oDXBTBeJs_Ij7JWOV-Nvg,2325
|
|
6
|
+
mathparse-0.2.6.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
7
|
+
mathparse-0.2.6.dist-info/top_level.txt,sha256=rAQj3bfKyZhbFL6WxVLRCqnvwQPI2ZCc_FNrwx6Hlmc,10
|
|
8
|
+
mathparse-0.2.6.dist-info/RECORD,,
|
mathparse-0.2.4.dist-info/RECORD
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
mathparse/__init__.py,sha256=8ezmwPLTfjAFn-5dEizcN50r1djK_RTFEwQjzZ8J4IE,110
|
|
2
|
-
mathparse/mathparse.py,sha256=QaCuSQGptxx7C88ZZNDmauuvqpc-amrfDJoTDpBl0lU,26237
|
|
3
|
-
mathparse/mathwords.py,sha256=0SA6rG4X7lSRkhaHr-oIC1g6HFKCFJysqeyLASbALLo,48306
|
|
4
|
-
mathparse-0.2.4.dist-info/licenses/LICENSE,sha256=_buf4D_BxjT3x8SBhP0Rh966qP3zpbButcLI4bPq_TY,1068
|
|
5
|
-
mathparse-0.2.4.dist-info/METADATA,sha256=H3MXoDbR7HtsNgbx-xqj9rV7Jf5_5aKMZqi09TWrqBM,2325
|
|
6
|
-
mathparse-0.2.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
7
|
-
mathparse-0.2.4.dist-info/top_level.txt,sha256=rAQj3bfKyZhbFL6WxVLRCqnvwQPI2ZCc_FNrwx6Hlmc,10
|
|
8
|
-
mathparse-0.2.4.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|