mathparse 0.2.4__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.4 → mathparse-0.2.5}/PKG-INFO +1 -1
- {mathparse-0.2.4 → mathparse-0.2.5}/mathparse/__init__.py +1 -1
- {mathparse-0.2.4 → mathparse-0.2.5}/mathparse/mathparse.py +68 -10
- {mathparse-0.2.4 → mathparse-0.2.5}/mathparse.egg-info/PKG-INFO +1 -1
- {mathparse-0.2.4 → mathparse-0.2.5}/mathparse.egg-info/SOURCES.txt +1 -0
- mathparse-0.2.5/tests/test_prefix_unary_operations.py +243 -0
- {mathparse-0.2.4 → mathparse-0.2.5}/tests/test_utils.py +12 -0
- {mathparse-0.2.4 → mathparse-0.2.5}/LICENSE +0 -0
- {mathparse-0.2.4 → mathparse-0.2.5}/README.md +0 -0
- {mathparse-0.2.4 → mathparse-0.2.5}/mathparse/mathwords.py +0 -0
- {mathparse-0.2.4 → mathparse-0.2.5}/mathparse.egg-info/dependency_links.txt +0 -0
- {mathparse-0.2.4 → mathparse-0.2.5}/mathparse.egg-info/requires.txt +0 -0
- {mathparse-0.2.4 → mathparse-0.2.5}/mathparse.egg-info/top_level.txt +0 -0
- {mathparse-0.2.4 → mathparse-0.2.5}/pyproject.toml +0 -0
- {mathparse-0.2.4 → mathparse-0.2.5}/setup.cfg +0 -0
- {mathparse-0.2.4 → mathparse-0.2.5}/tests/test_binary_operations.py +0 -0
- {mathparse-0.2.4 → mathparse-0.2.5}/tests/test_complex_statements.py +0 -0
- {mathparse-0.2.4 → mathparse-0.2.5}/tests/test_language_support.py +0 -0
- {mathparse-0.2.4 → mathparse-0.2.5}/tests/test_replace_word_tokens.py +0 -0
- {mathparse-0.2.4 → mathparse-0.2.5}/tests/test_simplified_chinese.py +0 -0
- {mathparse-0.2.4 → mathparse-0.2.5}/tests/test_unary_operations.py +0 -0
|
@@ -400,13 +400,15 @@ def preprocess_unary_operators(tokens: list) -> list:
|
|
|
400
400
|
* At the beginning of the expression
|
|
401
401
|
* After an opening parenthesis '('
|
|
402
402
|
* After a binary operator `(+, -, *, /, ^)`
|
|
403
|
+
* After a unary function `(sqrt, log, neg)`
|
|
403
404
|
"""
|
|
404
405
|
if not tokens:
|
|
405
406
|
return tokens
|
|
406
407
|
|
|
407
408
|
processed_tokens = []
|
|
408
409
|
|
|
409
|
-
|
|
410
|
+
# A following minus sign should be treated as unary (negative)
|
|
411
|
+
unary_contexts = mathwords.BINARY_OPERATORS | {'('}
|
|
410
412
|
|
|
411
413
|
for i, token in enumerate(tokens):
|
|
412
414
|
if token == '-':
|
|
@@ -418,8 +420,9 @@ def preprocess_unary_operators(tokens: list) -> list:
|
|
|
418
420
|
is_unary_minus = True
|
|
419
421
|
elif i > 0:
|
|
420
422
|
prev_token = tokens[i - 1]
|
|
421
|
-
# A unary minus after opening parenthesis
|
|
422
|
-
|
|
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):
|
|
423
426
|
is_unary_minus = True
|
|
424
427
|
|
|
425
428
|
if is_unary_minus:
|
|
@@ -539,14 +542,20 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
|
539
542
|
elif token == '.':
|
|
540
543
|
# Treat decimal points as a binary operator that combines the
|
|
541
544
|
# integer and fractional part of two numbers
|
|
542
|
-
# Example: 53 . 25 = 53.25
|
|
545
|
+
# Example: 53 . 25 = 53.25, -3 . 5 = -3.5
|
|
543
546
|
if b == 0:
|
|
544
547
|
total = Decimal(a)
|
|
545
548
|
else:
|
|
546
549
|
# Count the digits in b to determine the divisor
|
|
547
550
|
digits = len(str(int(b)))
|
|
548
551
|
divisor = 10 ** digits
|
|
549
|
-
|
|
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
|
|
550
559
|
else:
|
|
551
560
|
raise PostfixTokenEvaluationException(
|
|
552
561
|
'Unknown token "{}"'.format(token)
|
|
@@ -612,8 +621,22 @@ def tokenize(string: str, language: str = None, escape: str = '___') -> list:
|
|
|
612
621
|
string = string.replace(spaced_phrase, phrase)
|
|
613
622
|
|
|
614
623
|
# Binary operators must have space around them to be tokenized properly
|
|
624
|
+
# Special handling for minus sign: preserve leading negatives
|
|
615
625
|
for operator in mathwords.BINARY_OPERATORS:
|
|
616
|
-
|
|
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} ')
|
|
617
640
|
|
|
618
641
|
# Parenthesis must have space around them to be tokenized properly
|
|
619
642
|
string = string.replace('(', ' ( ')
|
|
@@ -752,11 +775,46 @@ def extract_expression(dirty_string: str, language: str) -> str:
|
|
|
752
775
|
start_index = 0
|
|
753
776
|
end_index = len(tokens)
|
|
754
777
|
|
|
755
|
-
|
|
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):
|
|
756
781
|
if is_symbol(part) or is_word(part, language):
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
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
|
|
760
818
|
|
|
761
819
|
for part in reversed(tokens):
|
|
762
820
|
if is_symbol(part) or is_word(part, language):
|
|
@@ -12,6 +12,7 @@ mathparse.egg-info/top_level.txt
|
|
|
12
12
|
tests/test_binary_operations.py
|
|
13
13
|
tests/test_complex_statements.py
|
|
14
14
|
tests/test_language_support.py
|
|
15
|
+
tests/test_prefix_unary_operations.py
|
|
15
16
|
tests/test_replace_word_tokens.py
|
|
16
17
|
tests/test_simplified_chinese.py
|
|
17
18
|
tests/test_unary_operations.py
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
from unittest import TestCase
|
|
2
|
+
from mathparse import mathparse
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class NegativeNumberTestCase(TestCase):
|
|
6
|
+
"""
|
|
7
|
+
Test cases for basic negative number operations.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
def test_leading_negative_with_addition(self):
|
|
11
|
+
"""
|
|
12
|
+
Test: -3 + 3 should equal 0
|
|
13
|
+
"""
|
|
14
|
+
result = mathparse.parse('-3 + 3')
|
|
15
|
+
self.assertEqual(result, 0)
|
|
16
|
+
|
|
17
|
+
def test_leading_negative_with_subtraction(self):
|
|
18
|
+
"""
|
|
19
|
+
Test: -3 - 5 should equal -8
|
|
20
|
+
"""
|
|
21
|
+
result = mathparse.parse('-3 - 5')
|
|
22
|
+
self.assertEqual(result, -8)
|
|
23
|
+
|
|
24
|
+
def test_leading_negative_with_multiplication(self):
|
|
25
|
+
"""
|
|
26
|
+
Test: -10 * 2 should equal -20
|
|
27
|
+
"""
|
|
28
|
+
result = mathparse.parse('-10 * 2')
|
|
29
|
+
self.assertEqual(result, -20)
|
|
30
|
+
|
|
31
|
+
def test_negative_in_parentheses(self):
|
|
32
|
+
"""
|
|
33
|
+
Test: (-3) should equal -3
|
|
34
|
+
"""
|
|
35
|
+
result = mathparse.parse('(-3)')
|
|
36
|
+
self.assertEqual(result, -3)
|
|
37
|
+
|
|
38
|
+
def test_negative_in_parentheses_with_addition(self):
|
|
39
|
+
"""
|
|
40
|
+
Test: (-3) + 5 should equal 2
|
|
41
|
+
"""
|
|
42
|
+
result = mathparse.parse('(-3) + 5')
|
|
43
|
+
self.assertEqual(result, 2)
|
|
44
|
+
|
|
45
|
+
def test_addition_with_negative_in_parentheses(self):
|
|
46
|
+
"""
|
|
47
|
+
Test: 5 + (-3) should equal 2
|
|
48
|
+
"""
|
|
49
|
+
result = mathparse.parse('5 + (-3)')
|
|
50
|
+
self.assertEqual(result, 2)
|
|
51
|
+
|
|
52
|
+
def test_two_negatives_in_parentheses(self):
|
|
53
|
+
"""
|
|
54
|
+
Test: (-3) * (-2) should equal 6
|
|
55
|
+
"""
|
|
56
|
+
result = mathparse.parse('(-3) * (-2)')
|
|
57
|
+
self.assertEqual(result, 6)
|
|
58
|
+
|
|
59
|
+
def test_multiplication_by_negative(self):
|
|
60
|
+
"""
|
|
61
|
+
Test: 3 * -2 should equal -6
|
|
62
|
+
"""
|
|
63
|
+
result = mathparse.parse('3 * -2')
|
|
64
|
+
self.assertEqual(result, -6)
|
|
65
|
+
|
|
66
|
+
def test_subtraction_of_negative(self):
|
|
67
|
+
"""
|
|
68
|
+
Test: 3 - -2 should equal 5 (subtracting a negative)
|
|
69
|
+
"""
|
|
70
|
+
result = mathparse.parse('3--2')
|
|
71
|
+
self.assertEqual(result, 5)
|
|
72
|
+
|
|
73
|
+
def test_two_separate_negatives_with_addition(self):
|
|
74
|
+
"""
|
|
75
|
+
Test: -3 + -5 should equal -8
|
|
76
|
+
"""
|
|
77
|
+
result = mathparse.parse('-3 + -5')
|
|
78
|
+
self.assertEqual(result, -8)
|
|
79
|
+
|
|
80
|
+
def test_complex_expression_with_leading_negative(self):
|
|
81
|
+
"""
|
|
82
|
+
Test: (-3 + 5) * 2 should equal 4
|
|
83
|
+
"""
|
|
84
|
+
result = mathparse.parse('(-3 + 5) * 2')
|
|
85
|
+
self.assertEqual(result, 4)
|
|
86
|
+
|
|
87
|
+
def test_negative_after_operator_in_expression(self):
|
|
88
|
+
"""
|
|
89
|
+
Test: 2 * (-3 + 5) should equal 4
|
|
90
|
+
"""
|
|
91
|
+
result = mathparse.parse('2 * (-3 + 5)')
|
|
92
|
+
self.assertEqual(result, 4)
|
|
93
|
+
|
|
94
|
+
def test_negative_of_expression(self):
|
|
95
|
+
"""
|
|
96
|
+
Test: -(3 + 5) should equal -8
|
|
97
|
+
"""
|
|
98
|
+
result = mathparse.parse('-(3 + 5)')
|
|
99
|
+
self.assertEqual(result, -8)
|
|
100
|
+
|
|
101
|
+
def test_negative_decimal(self):
|
|
102
|
+
"""
|
|
103
|
+
Test: -3.5 should equal -3.5 (parsed as -3 . 5)
|
|
104
|
+
"""
|
|
105
|
+
result = mathparse.parse('-3.5')
|
|
106
|
+
self.assertEqual(float(result), -3.5)
|
|
107
|
+
|
|
108
|
+
def test_negative_decimal_in_expression(self):
|
|
109
|
+
"""
|
|
110
|
+
Test: -10.25 + 5 should work correctly
|
|
111
|
+
"""
|
|
112
|
+
result = mathparse.parse('-10.25 + 5')
|
|
113
|
+
# Parses as (-10) . 25 + 5 = -10.25 + 5 = -5.25
|
|
114
|
+
self.assertEqual(float(result), -5.25)
|
|
115
|
+
|
|
116
|
+
def test_positive_decimal(self):
|
|
117
|
+
"""
|
|
118
|
+
Test: 3.5 should equal 3.5
|
|
119
|
+
"""
|
|
120
|
+
result = mathparse.parse('3.5')
|
|
121
|
+
self.assertEqual(float(result), 3.5)
|
|
122
|
+
|
|
123
|
+
def test_negative_decimal_with_multiple_digits(self):
|
|
124
|
+
"""
|
|
125
|
+
Test: -53.25 should equal -53.25
|
|
126
|
+
"""
|
|
127
|
+
result = mathparse.parse('-53.25')
|
|
128
|
+
self.assertEqual(float(result), -53.25)
|
|
129
|
+
|
|
130
|
+
def test_negative_decimal_in_multiplication(self):
|
|
131
|
+
"""
|
|
132
|
+
Test: -2.5 * 4 should equal -10.0
|
|
133
|
+
"""
|
|
134
|
+
result = mathparse.parse('-2.5 * 4')
|
|
135
|
+
self.assertEqual(float(result), -10.0)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class NegativeWithUnaryFunctionsTestCase(TestCase):
|
|
139
|
+
"""
|
|
140
|
+
Test cases for negative numbers with unary functions.
|
|
141
|
+
"""
|
|
142
|
+
|
|
143
|
+
def test_sqrt_with_negative_causes_math_error(self):
|
|
144
|
+
"""
|
|
145
|
+
Test: sqrt -16 should parse correctly but give a math domain error.
|
|
146
|
+
|
|
147
|
+
The tokenizer keeps -16 as a single token '-16', which is correctly
|
|
148
|
+
identified as an integer and passed to sqrt, causing the expected
|
|
149
|
+
math domain error.
|
|
150
|
+
"""
|
|
151
|
+
# Verify tokenization
|
|
152
|
+
tokens = mathparse.tokenize('sqrt -16')
|
|
153
|
+
self.assertEqual(tokens, ['sqrt', '-16'])
|
|
154
|
+
|
|
155
|
+
# Verify it causes the expected math error
|
|
156
|
+
with self.assertRaises(ValueError) as context:
|
|
157
|
+
mathparse.parse('sqrt -16')
|
|
158
|
+
self.assertIn("math domain error", str(context.exception).lower())
|
|
159
|
+
|
|
160
|
+
def test_log_with_negative_causes_math_error(self):
|
|
161
|
+
"""
|
|
162
|
+
Test: log -10 should parse correctly but give a math domain error.
|
|
163
|
+
"""
|
|
164
|
+
# Verify tokenization
|
|
165
|
+
tokens = mathparse.tokenize('log -10')
|
|
166
|
+
self.assertEqual(tokens, ['log', '-10'])
|
|
167
|
+
|
|
168
|
+
# Verify it causes the expected math error
|
|
169
|
+
with self.assertRaises(ValueError) as context:
|
|
170
|
+
mathparse.parse('log -10')
|
|
171
|
+
self.assertIn("math domain error", str(context.exception).lower())
|
|
172
|
+
|
|
173
|
+
def test_sqrt_with_positive_after_operator(self):
|
|
174
|
+
"""
|
|
175
|
+
Test: 3 + sqrt 16 should equal 7.0
|
|
176
|
+
"""
|
|
177
|
+
result = mathparse.parse('3 + sqrt 16')
|
|
178
|
+
self.assertEqual(result, 7.0)
|
|
179
|
+
|
|
180
|
+
def test_multiple_negatives_with_operations(self):
|
|
181
|
+
"""
|
|
182
|
+
Test: -2 * -3 + -1 should equal 5
|
|
183
|
+
"""
|
|
184
|
+
result = mathparse.parse('-2 * -3 + -1')
|
|
185
|
+
self.assertEqual(result, 5)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
class ExtractExpressionNegativeTestCase(TestCase):
|
|
189
|
+
"""
|
|
190
|
+
Test cases for extract_expression with negative numbers.
|
|
191
|
+
"""
|
|
192
|
+
|
|
193
|
+
def test_extract_leading_negative(self):
|
|
194
|
+
"""
|
|
195
|
+
Test: 'What is -3 + 3?' should extract '-3 + 3'
|
|
196
|
+
"""
|
|
197
|
+
result = mathparse.extract_expression(
|
|
198
|
+
'What is -3 + 3?', language='ENG'
|
|
199
|
+
)
|
|
200
|
+
self.assertEqual(result, '-3 + 3')
|
|
201
|
+
|
|
202
|
+
def test_extract_negative_with_multiplication(self):
|
|
203
|
+
"""
|
|
204
|
+
Test: 'Calculate -5 * 2' should extract '-5 * 2'
|
|
205
|
+
"""
|
|
206
|
+
result = mathparse.extract_expression(
|
|
207
|
+
'Calculate -5 * 2', language='ENG'
|
|
208
|
+
)
|
|
209
|
+
self.assertEqual(result, '-5 * 2')
|
|
210
|
+
|
|
211
|
+
def test_extract_binary_subtraction(self):
|
|
212
|
+
"""
|
|
213
|
+
Test: 'The answer to 10 - 5 is' should extract '10 - 5'
|
|
214
|
+
"""
|
|
215
|
+
result = mathparse.extract_expression(
|
|
216
|
+
'The answer to 10 - 5 is', language='ENG'
|
|
217
|
+
)
|
|
218
|
+
self.assertEqual(result, '10 - 5')
|
|
219
|
+
|
|
220
|
+
def test_extract_pure_negative_expression(self):
|
|
221
|
+
"""
|
|
222
|
+
Test: '-3 + 3' should extract '-3 + 3' (with no extra text)
|
|
223
|
+
"""
|
|
224
|
+
result = mathparse.extract_expression('-3 + 3', language='ENG')
|
|
225
|
+
self.assertEqual(result, '-3 + 3')
|
|
226
|
+
|
|
227
|
+
def test_extract_pure_subtraction(self):
|
|
228
|
+
"""
|
|
229
|
+
Test: '5 - 3' should extract '5 - 3' (with no extra text)
|
|
230
|
+
"""
|
|
231
|
+
result = mathparse.extract_expression('5 - 3', language='ENG')
|
|
232
|
+
self.assertEqual(result, '5 - 3')
|
|
233
|
+
|
|
234
|
+
def test_extract_negative_in_parentheses_with_words(self):
|
|
235
|
+
"""
|
|
236
|
+
Test: Extract expression with negative in parentheses from sentence
|
|
237
|
+
"""
|
|
238
|
+
result = mathparse.extract_expression(
|
|
239
|
+
'Compute (-3) + 5', language='ENG'
|
|
240
|
+
)
|
|
241
|
+
# NOTE: Spaces are currently added, but ideally these will be removed
|
|
242
|
+
# in the future
|
|
243
|
+
self.assertEqual(result, '( -3 ) + 5')
|
|
@@ -81,6 +81,18 @@ class ExtractExpressionTestCase(TestCase):
|
|
|
81
81
|
|
|
82
82
|
self.assertEqual(result, '3 + 3')
|
|
83
83
|
|
|
84
|
+
def test_extract_expression_with_negative(self):
|
|
85
|
+
result = mathparse.extract_expression('-3 + 3', language='ENG')
|
|
86
|
+
|
|
87
|
+
self.assertEqual(result, '-3 + 3')
|
|
88
|
+
|
|
89
|
+
def test_extract_expression_with_words(self):
|
|
90
|
+
result = mathparse.extract_expression(
|
|
91
|
+
'three plus three', language='ENG'
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
self.assertEqual(result, 'three plus three')
|
|
95
|
+
|
|
84
96
|
def test_ignore_punctuation(self):
|
|
85
97
|
result = mathparse.extract_expression('3?', language='ENG')
|
|
86
98
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|