mathparse 0.1.2__py3-none-any.whl → 0.2.0__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 -4
- mathparse/mathparse.py +30 -15
- mathparse/mathwords.py +55 -0
- mathparse-0.2.0.dist-info/METADATA +59 -0
- mathparse-0.2.0.dist-info/RECORD +8 -0
- {mathparse-0.1.2.dist-info → mathparse-0.2.0.dist-info}/WHEEL +1 -1
- mathparse-0.2.0.dist-info/licenses/LICENSE +21 -0
- mathparse-0.1.2.dist-info/METADATA +0 -23
- mathparse-0.1.2.dist-info/RECORD +0 -8
- mathparse-0.1.2.dist-info/zip-safe +0 -1
- {mathparse-0.1.2.dist-info → mathparse-0.2.0.dist-info}/top_level.txt +0 -0
mathparse/__init__.py
CHANGED
mathparse/mathparse.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Methods for evaluating mathematical equations in strings.
|
|
3
3
|
"""
|
|
4
|
-
from __future__ import division
|
|
5
4
|
from decimal import Decimal
|
|
6
5
|
from . import mathwords
|
|
7
6
|
import re
|
|
8
7
|
|
|
8
|
+
|
|
9
9
|
class PostfixTokenEvaluationException(Exception):
|
|
10
10
|
"""
|
|
11
11
|
Exception to be raised when a language code is given that
|
|
@@ -45,7 +45,8 @@ def is_constant(string):
|
|
|
45
45
|
|
|
46
46
|
def is_unary(string):
|
|
47
47
|
"""
|
|
48
|
-
Return true if the string is a defined unary mathematical
|
|
48
|
+
Return true if the string is a defined unary mathematical
|
|
49
|
+
operator function.
|
|
49
50
|
"""
|
|
50
51
|
return string in mathwords.UNARY_FUNCTIONS
|
|
51
52
|
|
|
@@ -56,6 +57,7 @@ def is_binary(string):
|
|
|
56
57
|
"""
|
|
57
58
|
return string in mathwords.BINARY_OPERATORS
|
|
58
59
|
|
|
60
|
+
|
|
59
61
|
def is_symbol(string):
|
|
60
62
|
"""
|
|
61
63
|
Return true if the string is a mathematical symbol.
|
|
@@ -67,6 +69,7 @@ def is_symbol(string):
|
|
|
67
69
|
(string == '(') or (string == ')')
|
|
68
70
|
)
|
|
69
71
|
|
|
72
|
+
|
|
70
73
|
def is_word(word, language):
|
|
71
74
|
"""
|
|
72
75
|
Return true if the word is a math word for the specified language.
|
|
@@ -75,17 +78,26 @@ def is_word(word, language):
|
|
|
75
78
|
|
|
76
79
|
return word in words
|
|
77
80
|
|
|
81
|
+
|
|
78
82
|
def find_word_groups(string, words):
|
|
79
83
|
"""
|
|
80
84
|
Find matches for words in the format "3 thousand 6 hundred 2".
|
|
81
|
-
The words parameter should be the list of words to check for
|
|
85
|
+
The words parameter should be the list of words to check for
|
|
86
|
+
such as "hundred".
|
|
82
87
|
"""
|
|
83
88
|
scale_pattern = '|'.join(words)
|
|
84
|
-
# For example:
|
|
85
|
-
|
|
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
|
+
)
|
|
86
97
|
result = regex.findall(string)
|
|
87
98
|
return result
|
|
88
99
|
|
|
100
|
+
|
|
89
101
|
def replace_word_tokens(string, language):
|
|
90
102
|
"""
|
|
91
103
|
Given a string and an ISO 639-2 language code,
|
|
@@ -99,12 +111,6 @@ def replace_word_tokens(string, language):
|
|
|
99
111
|
if 'unary_operators' in words:
|
|
100
112
|
operators.update(words['unary_operators'])
|
|
101
113
|
|
|
102
|
-
# Ensures unicode string processing
|
|
103
|
-
import sys
|
|
104
|
-
if sys.version_info.major < 3:
|
|
105
|
-
if not isinstance(string, unicode):
|
|
106
|
-
string = unicode(string, "utf-8")
|
|
107
|
-
|
|
108
114
|
for operator in list(operators.keys()):
|
|
109
115
|
if operator in string:
|
|
110
116
|
string = string.replace(operator, operators[operator])
|
|
@@ -141,7 +147,10 @@ def replace_word_tokens(string, language):
|
|
|
141
147
|
add = ''
|
|
142
148
|
|
|
143
149
|
string = string[:start_index] + '(' + string[start_index:]
|
|
144
|
-
string = string.replace(
|
|
150
|
+
string = string.replace(
|
|
151
|
+
scale, '* ' + str(scales[scale]) + ')' + add,
|
|
152
|
+
1
|
|
153
|
+
)
|
|
145
154
|
|
|
146
155
|
string = string.replace(') (', ') + (')
|
|
147
156
|
|
|
@@ -181,7 +190,9 @@ def to_postfix(tokens):
|
|
|
181
190
|
postfix.append(top_token)
|
|
182
191
|
top_token = opstack.pop()
|
|
183
192
|
else:
|
|
184
|
-
while (opstack != []) and (
|
|
193
|
+
while (opstack != []) and (
|
|
194
|
+
precedence[opstack[-1]] >= precedence[token]
|
|
195
|
+
):
|
|
185
196
|
postfix.append(opstack.pop())
|
|
186
197
|
opstack.append(token)
|
|
187
198
|
|
|
@@ -223,14 +234,18 @@ def evaluate_postfix(tokens):
|
|
|
223
234
|
else:
|
|
224
235
|
total = Decimal(str(a)) / Decimal(str(b))
|
|
225
236
|
else:
|
|
226
|
-
raise PostfixTokenEvaluationException(
|
|
237
|
+
raise PostfixTokenEvaluationException(
|
|
238
|
+
'Unknown token {}'.format(token)
|
|
239
|
+
)
|
|
227
240
|
|
|
228
241
|
if total is not None:
|
|
229
242
|
stack.append(total)
|
|
230
243
|
|
|
231
244
|
# If the stack is empty the tokens could not be evaluated
|
|
232
245
|
if not stack:
|
|
233
|
-
raise PostfixTokenEvaluationException(
|
|
246
|
+
raise PostfixTokenEvaluationException(
|
|
247
|
+
'The postfix expression resulted in an empty stack'
|
|
248
|
+
)
|
|
234
249
|
|
|
235
250
|
return stack.pop()
|
|
236
251
|
|
mathparse/mathwords.py
CHANGED
|
@@ -164,6 +164,60 @@ MATH_WORDS = {
|
|
|
164
164
|
'billion': 1000000000000
|
|
165
165
|
}
|
|
166
166
|
},
|
|
167
|
+
'GRE': {
|
|
168
|
+
'unary_operators': {
|
|
169
|
+
'στο τετράγωνο': '^ 2',
|
|
170
|
+
'στον κύβο': '^ 3',
|
|
171
|
+
'τετραγωνική ρίζα του': 'sqrt'
|
|
172
|
+
},
|
|
173
|
+
'binary_operators': {
|
|
174
|
+
'συν': '+', 'και': '+',
|
|
175
|
+
'διά': '/',
|
|
176
|
+
'πλην': '-',
|
|
177
|
+
'επί': '*',
|
|
178
|
+
'στην δύναμη του': '^',
|
|
179
|
+
'εις την': '^'
|
|
180
|
+
},
|
|
181
|
+
'numbers': {
|
|
182
|
+
'μηδέν': 0,
|
|
183
|
+
'ένα': 1,
|
|
184
|
+
'δύο': 2,
|
|
185
|
+
'τρία': 3,
|
|
186
|
+
'τέσσερα': 4,
|
|
187
|
+
'πέντε': 5,
|
|
188
|
+
'έξι': 6,
|
|
189
|
+
'εφτά': 7,
|
|
190
|
+
'οκτώ': 8, 'οχτώ': 8,
|
|
191
|
+
'εννιά': 9, 'εννέα': 9,
|
|
192
|
+
'δέκα': 10,
|
|
193
|
+
'έντεκα': 11,
|
|
194
|
+
'δώδεκα': 12,
|
|
195
|
+
'δεκατρία': 13,
|
|
196
|
+
'δεκατέσσερα': 14,
|
|
197
|
+
'δεκαπέντε': 15,
|
|
198
|
+
'δεκαέξι': 16,
|
|
199
|
+
'δεκαεφτά': 17,
|
|
200
|
+
'δεκαοκτώ': 18, 'δεκαοχτώ': 18,
|
|
201
|
+
'δεκαεννιά': 19, 'δεκαεννέα': 19,
|
|
202
|
+
'είκοσι': 20,
|
|
203
|
+
'τριάντα': 30,
|
|
204
|
+
'σαράντα': 40,
|
|
205
|
+
'πενήντα': 50,
|
|
206
|
+
'εξήντα': 60,
|
|
207
|
+
'εβδομήντα': 70,
|
|
208
|
+
'ογδόντα': 80,
|
|
209
|
+
'ενενήντα': 90
|
|
210
|
+
},
|
|
211
|
+
'scales': {
|
|
212
|
+
'εκατό': 100,
|
|
213
|
+
'χίλια': 1000,
|
|
214
|
+
'εκατομμύρια': 1000000, 'εκ.': 1000000,
|
|
215
|
+
'δισεκατομμύρια': 1000000000,
|
|
216
|
+
'δισ.': 1000000000, 'δις': 1000000000,
|
|
217
|
+
'τρισεκατομμύρια': 1000000000000,
|
|
218
|
+
'τρισ.': 1000000000000, 'τρις': 1000000000000
|
|
219
|
+
}
|
|
220
|
+
},
|
|
167
221
|
'ITA': {
|
|
168
222
|
'binary_operators': {
|
|
169
223
|
'più': '+',
|
|
@@ -407,6 +461,7 @@ def word_groups_for_language(language_code):
|
|
|
407
461
|
|
|
408
462
|
return MATH_WORDS[language_code]
|
|
409
463
|
|
|
464
|
+
|
|
410
465
|
def words_for_language(language_code):
|
|
411
466
|
"""
|
|
412
467
|
Return the math words for a language code.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mathparse
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: mathparse is a library for solving mathematical equations contained in strings
|
|
5
|
+
Author: Gunther Cox
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Documentation, https://docs.chatterbot.us
|
|
8
|
+
Project-URL: Repository, https://github.com/gunthercox/ChatterBot
|
|
9
|
+
Project-URL: Changelog, https://github.com/gunthercox/ChatterBot/releases
|
|
10
|
+
Keywords: mathparse,mathematics,math,nlp
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
16
|
+
Classifier: Topic :: Text Processing :: General
|
|
17
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
18
|
+
Classifier: Programming Language :: Python
|
|
19
|
+
Requires-Python: <3.14,>=3.9
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Provides-Extra: test
|
|
23
|
+
Requires-Dist: flake8; extra == "test"
|
|
24
|
+
Requires-Dist: sphinx<9.0,>=7.4; extra == "test"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# mathparse
|
|
28
|
+
|
|
29
|
+
The `mathparse` library is a Python module designed to evaluate mathematical equations contained in strings.
|
|
30
|
+
|
|
31
|
+
Here are a few examples:
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from mathparse import mathparse
|
|
35
|
+
|
|
36
|
+
mathparse.parse('50 * (85 / 100)')
|
|
37
|
+
>>> 42.5
|
|
38
|
+
|
|
39
|
+
mathparse.parse('one hundred times fifty four', mathparse.codes.ENG)
|
|
40
|
+
>>> 5400
|
|
41
|
+
|
|
42
|
+
mathparse.parse('(seven * nine) + 8 - (45 plus two)', language='ENG')
|
|
43
|
+
>>> 24
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Installation
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install mathparse
|
|
50
|
+
```
|
|
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
|
+
|
|
57
|
+
## History
|
|
58
|
+
|
|
59
|
+
See [release notes](https://github.com/gunthercox/ChatterBot/releases) for changes.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
mathparse/__init__.py,sha256=mDQatAlLJHwQsgh4sSC9Mebl5EDpeIsyBXKH_K-OTAo,110
|
|
2
|
+
mathparse/mathparse.py,sha256=_tmr746_Ofx1_CWfoKOlKc4phBNHLtiR_w-JhVbnexo,8242
|
|
3
|
+
mathparse/mathwords.py,sha256=p7wMTdSUk3DiQ8SgW8U2AOMjVHXyqMKGNNKKxzWlr4o,13231
|
|
4
|
+
mathparse-0.2.0.dist-info/licenses/LICENSE,sha256=_buf4D_BxjT3x8SBhP0Rh966qP3zpbButcLI4bPq_TY,1068
|
|
5
|
+
mathparse-0.2.0.dist-info/METADATA,sha256=SKXbHsxgoUssocBVNL2XOGX_vcL6nn0aCvo8xQesOoQ,1835
|
|
6
|
+
mathparse-0.2.0.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
|
|
7
|
+
mathparse-0.2.0.dist-info/top_level.txt,sha256=rAQj3bfKyZhbFL6WxVLRCqnvwQPI2ZCc_FNrwx6Hlmc,10
|
|
8
|
+
mathparse-0.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Gunther Cox
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: mathparse
|
|
3
|
-
Version: 0.1.2
|
|
4
|
-
Summary:
|
|
5
|
-
|
|
6
|
-
mathparse is a library for solving mathematical equations contained in strings
|
|
7
|
-
|
|
8
|
-
Home-page: https://github.com/gunthercox/mathparse
|
|
9
|
-
Author: Gunther Cox
|
|
10
|
-
Author-email: gunthercx@gmail.com
|
|
11
|
-
License: MIT
|
|
12
|
-
Description: UNKNOWN
|
|
13
|
-
Keywords: mathparse
|
|
14
|
-
Platform: any
|
|
15
|
-
Classifier: Development Status :: 4 - Beta
|
|
16
|
-
Classifier: Intended Audience :: Developers
|
|
17
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
18
|
-
Classifier: Operating System :: OS Independent
|
|
19
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
-
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
21
|
-
Classifier: Topic :: Text Processing :: General
|
|
22
|
-
Classifier: Topic :: Text Processing :: Linguistic
|
|
23
|
-
Classifier: Programming Language :: Python
|
mathparse-0.1.2.dist-info/RECORD
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
mathparse/__init__.py,sha256=l07wxGB9DPLlNKlKEKgixVhKp2aaWvlfZRRjhJOa6mw,223
|
|
2
|
-
mathparse/mathparse.py,sha256=cxfsNOzOl_yG_7wS-RIPmOf7ge3VZIdg4JY-Iz6o4cY,8278
|
|
3
|
-
mathparse/mathwords.py,sha256=s4vOzNOlKX3X4pl3iKPIB79V4yUrGC3oyZUizKa14iA,11360
|
|
4
|
-
mathparse-0.1.2.dist-info/METADATA,sha256=YaZG8iuOeHU3ze7E4tYJucksQrFgb1bbnIkdxNjvanU,776
|
|
5
|
-
mathparse-0.1.2.dist-info/RECORD,,
|
|
6
|
-
mathparse-0.1.2.dist-info/WHEEL,sha256=NzFAKnL7g-U64xnS1s5e3mJnxKpOTeOtlXdFwS9yNXI,92
|
|
7
|
-
mathparse-0.1.2.dist-info/top_level.txt,sha256=rAQj3bfKyZhbFL6WxVLRCqnvwQPI2ZCc_FNrwx6Hlmc,10
|
|
8
|
-
mathparse-0.1.2.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|
|
File without changes
|