mathparse 0.1.2__tar.gz → 0.1.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.
@@ -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.
@@ -0,0 +1,67 @@
1
+ Metadata-Version: 2.4
2
+ Name: mathparse
3
+ Version: 0.1.5
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.13,>=3.9
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Requires-Dist: mathparse<0.2,>=0.1
23
+ Requires-Dist: python-dateutil<2.10,>=2.9
24
+ Requires-Dist: sqlalchemy<2.1,>=2.0
25
+ Requires-Dist: spacy<3.9,>=3.8
26
+ Requires-Dist: tqdm
27
+ Provides-Extra: test
28
+ Requires-Dist: flake8; extra == "test"
29
+ Requires-Dist: sphinx<9.0,>=8.2; extra == "test"
30
+ Dynamic: license-file
31
+
32
+ # mathparse
33
+
34
+ The `mathparse` library is a Python module designed to evaluate mathematical equations contained in strings.
35
+
36
+ Here are a few examples:
37
+
38
+ ```python
39
+ from mathparse import mathparse
40
+
41
+ mathparse.parse('50 * (85 / 100)')
42
+ >>> 42.5
43
+
44
+ mathparse.parse('one hundred times fifty four', mathparse.codes.ENG)
45
+ >>> 5400
46
+
47
+ mathparse.parse('(seven * nine) + 8 - (45 plus two)', language='ENG')
48
+ >>> 24
49
+ ```
50
+
51
+ ## Installation
52
+
53
+ ```bash
54
+ pip install mathparse
55
+ ```
56
+
57
+ ## Language support
58
+
59
+ The language parameter must be set in order to evaluate an equation that uses word operators.
60
+ The language code should be a valid `ISO 639-2`_ language code.
61
+
62
+ ## History
63
+
64
+ See `release notes`_ for changes.
65
+
66
+ .. _`ISO 639-2`: https://www.loc.gov/standards/iso639-2/php/code_list.php
67
+ .. _`release notes`: https://github.com/gunthercox/ChatterBot/releases
@@ -0,0 +1,36 @@
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`_ language code.
30
+
31
+ ## History
32
+
33
+ See `release notes`_ for changes.
34
+
35
+ .. _`ISO 639-2`: https://www.loc.gov/standards/iso639-2/php/code_list.php
36
+ .. _`release notes`: https://github.com/gunthercox/ChatterBot/releases
@@ -0,0 +1,5 @@
1
+ """
2
+ mathparse is a library for solving mathematical equations contained in strings
3
+ """
4
+
5
+ __version__ = '0.1.5'
@@ -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 operator function.
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 such as "hundred".
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: (?:(?:\d+)\s+(?:hundred|thousand|million)*\s*)+(?:\d+|hundred|thousand|million)+
85
- regex = re.compile(r'(?:(?:\d+)\s+(?:' + scale_pattern + r')*\s*)+(?:\d+|' + scale_pattern + r')+')
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(scale, '* ' + str(scales[scale]) + ')' + add, 1)
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 (precedence[opstack[-1]] >= precedence[token]):
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('Unknown token {}'.format(token))
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('The postfix expression resulted in an empty stack')
246
+ raise PostfixTokenEvaluationException(
247
+ 'The postfix expression resulted in an empty stack'
248
+ )
234
249
 
235
250
  return stack.pop()
236
251
 
@@ -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,67 @@
1
+ Metadata-Version: 2.4
2
+ Name: mathparse
3
+ Version: 0.1.5
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.13,>=3.9
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Requires-Dist: mathparse<0.2,>=0.1
23
+ Requires-Dist: python-dateutil<2.10,>=2.9
24
+ Requires-Dist: sqlalchemy<2.1,>=2.0
25
+ Requires-Dist: spacy<3.9,>=3.8
26
+ Requires-Dist: tqdm
27
+ Provides-Extra: test
28
+ Requires-Dist: flake8; extra == "test"
29
+ Requires-Dist: sphinx<9.0,>=8.2; extra == "test"
30
+ Dynamic: license-file
31
+
32
+ # mathparse
33
+
34
+ The `mathparse` library is a Python module designed to evaluate mathematical equations contained in strings.
35
+
36
+ Here are a few examples:
37
+
38
+ ```python
39
+ from mathparse import mathparse
40
+
41
+ mathparse.parse('50 * (85 / 100)')
42
+ >>> 42.5
43
+
44
+ mathparse.parse('one hundred times fifty four', mathparse.codes.ENG)
45
+ >>> 5400
46
+
47
+ mathparse.parse('(seven * nine) + 8 - (45 plus two)', language='ENG')
48
+ >>> 24
49
+ ```
50
+
51
+ ## Installation
52
+
53
+ ```bash
54
+ pip install mathparse
55
+ ```
56
+
57
+ ## Language support
58
+
59
+ The language parameter must be set in order to evaluate an equation that uses word operators.
60
+ The language code should be a valid `ISO 639-2`_ language code.
61
+
62
+ ## History
63
+
64
+ See `release notes`_ for changes.
65
+
66
+ .. _`ISO 639-2`: https://www.loc.gov/standards/iso639-2/php/code_list.php
67
+ .. _`release notes`: https://github.com/gunthercox/ChatterBot/releases
@@ -1,10 +1,15 @@
1
- README.rst
2
- setup.py
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
3
4
  mathparse/__init__.py
4
5
  mathparse/mathparse.py
5
6
  mathparse/mathwords.py
6
7
  mathparse.egg-info/PKG-INFO
7
8
  mathparse.egg-info/SOURCES.txt
8
9
  mathparse.egg-info/dependency_links.txt
10
+ mathparse.egg-info/requires.txt
9
11
  mathparse.egg-info/top_level.txt
10
- mathparse.egg-info/zip-safe
12
+ tests/test_binary_operations.py
13
+ tests/test_replace_word_tokens.py
14
+ tests/test_unary_operations.py
15
+ tests/test_utils.py
@@ -0,0 +1,9 @@
1
+ mathparse<0.2,>=0.1
2
+ python-dateutil<2.10,>=2.9
3
+ sqlalchemy<2.1,>=2.0
4
+ spacy<3.9,>=3.8
5
+ tqdm
6
+
7
+ [test]
8
+ flake8
9
+ sphinx<9.0,>=8.2
@@ -0,0 +1,52 @@
1
+ [build-system]
2
+ requires = ["setuptools"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [tool.setuptools]
6
+ packages=[
7
+ "mathparse",
8
+ ]
9
+
10
+ [tool.setuptools.dynamic]
11
+ version = {attr = "mathparse.__version__"}
12
+
13
+ [project]
14
+ name = "mathparse"
15
+ requires-python = ">=3.9,<=3.13"
16
+ urls = { Documentation = "https://docs.chatterbot.us", Repository = "https://github.com/gunthercox/ChatterBot", Changelog = "https://github.com/gunthercox/ChatterBot/releases" }
17
+ authors = [
18
+ {name = "Gunther Cox"},
19
+ ]
20
+ license = "MIT"
21
+ description = "mathparse is a library for solving mathematical equations contained in strings"
22
+ readme = "README.md"
23
+ dynamic = ["version"]
24
+ keywords = [
25
+ "mathparse",
26
+ "mathematics",
27
+ "math",
28
+ "nlp",
29
+ ]
30
+ classifiers = [
31
+ "Development Status :: 4 - Beta",
32
+ "Intended Audience :: Developers",
33
+ "Operating System :: OS Independent",
34
+ "Topic :: Software Development :: Libraries :: Python Modules",
35
+ "Topic :: Scientific/Engineering :: Mathematics",
36
+ "Topic :: Text Processing :: General",
37
+ "Topic :: Text Processing :: Linguistic",
38
+ "Programming Language :: Python",
39
+ ]
40
+ dependencies = [
41
+ "mathparse>=0.1,<0.2",
42
+ "python-dateutil>=2.9,<2.10",
43
+ "sqlalchemy>=2.0,<2.1",
44
+ "spacy>=3.8,<3.9",
45
+ "tqdm",
46
+ ]
47
+
48
+ [project.optional-dependencies]
49
+ test = [
50
+ "flake8",
51
+ "sphinx>=8.2,<9.0"
52
+ ]
@@ -0,0 +1,98 @@
1
+ from unittest import TestCase
2
+ from mathparse import mathparse
3
+
4
+
5
+ class PositiveIntegerTestCase(TestCase):
6
+
7
+ def test_addition(self):
8
+ result = mathparse.parse('4 + 4')
9
+
10
+ self.assertEqual(result, 8)
11
+
12
+ def test_addition_words(self):
13
+ result = mathparse.parse('four plus four', language='ENG')
14
+
15
+ self.assertEqual(result, 8)
16
+
17
+ def test_addition_words_large(self):
18
+ result = mathparse.parse(
19
+ 'four thousand two hundred one plus five hundred', language='ENG'
20
+ )
21
+
22
+ self.assertEqual(result, 4701)
23
+
24
+ def test_subtraction(self):
25
+ result = mathparse.parse('30 - 20')
26
+
27
+ self.assertEqual(result, 10)
28
+
29
+ def test_subtraction_words(self):
30
+ result = mathparse.parse('thirty minus twenty', language='ENG')
31
+
32
+ self.assertEqual(result, 10)
33
+
34
+ def test_multiplication(self):
35
+ result = mathparse.parse('9 * 9')
36
+
37
+ self.assertEqual(result, 81)
38
+
39
+ def test_multiplication_words(self):
40
+ result = mathparse.parse('nine times nine', language='ENG')
41
+
42
+ self.assertEqual(result, 81)
43
+
44
+ def test_division(self):
45
+ result = mathparse.parse('15 / 5')
46
+
47
+ self.assertEqual(result, 3)
48
+
49
+ def test_division_words(self):
50
+ result = mathparse.parse('fifteen divided by five', language='ENG')
51
+
52
+ self.assertEqual(result, 3)
53
+
54
+ def test_double_digit_multiplier_for_scale_addition(self):
55
+ result = mathparse.parse('fifty thousand plus one', language='ENG')
56
+
57
+ self.assertEqual(result, 50001)
58
+
59
+ def test_division_by_zero(self):
60
+ result = mathparse.parse('42 / 0', language='ENG')
61
+
62
+ self.assertEqual(result, 'undefined')
63
+
64
+ def test_division_by_zero_words(self):
65
+ result = mathparse.parse('six divided by zero', language='ENG')
66
+
67
+ self.assertEqual(result, 'undefined')
68
+
69
+ def test_division_words_large(self):
70
+ result = mathparse.parse(
71
+ 'one thousand two hundred four divided by one hundred',
72
+ language='ENG'
73
+ )
74
+
75
+ self.assertEqual(str(result), '12.04')
76
+
77
+
78
+ class PositiveFloatTestCase(TestCase):
79
+
80
+ def test_addition(self):
81
+ result = mathparse.parse('0.6 + 0.5')
82
+
83
+ self.assertEqual(result, 1.1)
84
+
85
+ def test_subtraction(self):
86
+ result = mathparse.parse('30.1 - 29.1')
87
+
88
+ self.assertEqual(result, 1)
89
+
90
+ def test_multiplication(self):
91
+ result = mathparse.parse('0.9 * 0.9')
92
+
93
+ self.assertEqual(result, 0.81)
94
+
95
+ def test_division(self):
96
+ result = mathparse.parse('0.6 / 0.2')
97
+
98
+ self.assertEqual(result, 3)
@@ -0,0 +1,33 @@
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')
@@ -0,0 +1,38 @@
1
+ # -*- coding: utf-8 -*-
2
+ from unittest import TestCase
3
+ from mathparse import mathparse
4
+
5
+
6
+ class UnaryOperatorTestCase(TestCase):
7
+
8
+ def test_exponent(self):
9
+ result = mathparse.parse('4 ^ 4')
10
+
11
+ self.assertEqual(result, 256)
12
+
13
+ def test_without_unary_operator_fre(self):
14
+ result = mathparse.parse('50 * (85 / 100)', language='FRE')
15
+ self.assertEqual(result, 42.5)
16
+
17
+ def test_without_unary_operator_rus(self):
18
+ result = mathparse.parse('четыре плюс четыре',
19
+ language='RUS')
20
+ self.assertEqual(result, 8)
21
+
22
+
23
+ class UnaryWordOperatorTestCase(TestCase):
24
+
25
+ def test_sqrt(self):
26
+ result = mathparse.parse('sqrt 4')
27
+
28
+ self.assertEqual(result, 2)
29
+
30
+ def test_sqrt_parenthesis(self):
31
+ result = mathparse.parse('sqrt(4)')
32
+
33
+ self.assertEqual(result, 2)
34
+
35
+ def test_square_root(self):
36
+ result = mathparse.parse('square root of 4', language='ENG')
37
+
38
+ self.assertEqual(result, 2)
@@ -0,0 +1,91 @@
1
+ from unittest import TestCase
2
+ from mathparse import mathparse
3
+
4
+
5
+ class BooleanChecksTestCase(TestCase):
6
+
7
+ def test_is_integer(self):
8
+ self.assertTrue(mathparse.is_int('42'))
9
+
10
+ def test_is_not_integer(self):
11
+ self.assertFalse(mathparse.is_int('42.2'))
12
+
13
+ def test_is_float(self):
14
+ self.assertTrue(mathparse.is_float('0.5'))
15
+
16
+ def test_is_not_float(self):
17
+ self.assertFalse(mathparse.is_float('5'))
18
+
19
+ def test_is_constant(self):
20
+ self.assertTrue(mathparse.is_constant('pi'))
21
+
22
+ def test_is_not_constant(self):
23
+ self.assertFalse(mathparse.is_constant('+'))
24
+
25
+ def test_is_unary(self):
26
+ self.assertTrue(mathparse.is_unary('sqrt'))
27
+
28
+ def test_is_not_unary(self):
29
+ self.assertFalse(mathparse.is_unary('+'))
30
+
31
+ def test_is_binary(self):
32
+ self.assertTrue(mathparse.is_binary('-'))
33
+
34
+ def test_is_not_binary(self):
35
+ self.assertFalse(mathparse.is_binary('sqrt'))
36
+
37
+ def test_is_word(self):
38
+ self.assertTrue(mathparse.is_word('three', language='ENG'))
39
+
40
+ def test_is_not_word(self):
41
+ self.assertFalse(mathparse.is_word('3', language='ENG'))
42
+
43
+
44
+ class TokenizationTestCase(TestCase):
45
+
46
+ def test_lowercase_tokens(self):
47
+ result = mathparse.tokenize('Three PLUS five', language='ENG')
48
+
49
+ self.assertEqual(result, ['three', 'plus', 'five'])
50
+
51
+ def test_load_english_words(self):
52
+ from mathparse import mathwords
53
+
54
+ words = mathwords.words_for_language('ENG')
55
+ self.assertIn('three', words)
56
+
57
+ def test_load_nonexistent_data(self):
58
+ from mathparse import mathwords
59
+
60
+ with self.assertRaises(mathwords.InvalidLanguageCodeException):
61
+ mathwords.words_for_language('&&&')
62
+
63
+
64
+ class ExtractExpressionTestCase(TestCase):
65
+
66
+ def test_empty_string(self):
67
+ result = mathparse.extract_expression('', language='ENG')
68
+
69
+ self.assertEqual(result, '')
70
+
71
+ def test_extract_expression(self):
72
+ result = mathparse.extract_expression('3 + 3', language='ENG')
73
+
74
+ self.assertEqual(result, '3 + 3')
75
+
76
+ def test_ignore_punctuation(self):
77
+ result = mathparse.extract_expression('3?', language='ENG')
78
+
79
+ self.assertEqual(result, '3')
80
+
81
+ def test_extract_expression_simple_additon(self):
82
+ result = mathparse.extract_expression('What is 3 + 3?', language='ENG')
83
+
84
+ self.assertEqual(result, '3 + 3')
85
+
86
+ def test_extract_expression_simple_additon_words(self):
87
+ result = mathparse.extract_expression(
88
+ 'What is three plus three?', language='ENG'
89
+ )
90
+
91
+ self.assertEqual(result, 'three plus three')
mathparse-0.1.2/PKG-INFO DELETED
@@ -1,22 +0,0 @@
1
- Metadata-Version: 1.1
2
- Name: mathparse
3
- Version: 0.1.2
4
- Summary:
5
- mathparse is a library for solving mathematical equations contained in strings
6
-
7
- Home-page: https://github.com/gunthercox/mathparse
8
- Author: Gunther Cox
9
- Author-email: gunthercx@gmail.com
10
- License: MIT
11
- Description: UNKNOWN
12
- Keywords: mathparse
13
- Platform: any
14
- Classifier: Development Status :: 4 - Beta
15
- Classifier: Intended Audience :: Developers
16
- Classifier: License :: OSI Approved :: MIT License
17
- Classifier: Operating System :: OS Independent
18
- Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
- Classifier: Topic :: Scientific/Engineering :: Mathematics
20
- Classifier: Topic :: Text Processing :: General
21
- Classifier: Topic :: Text Processing :: Linguistic
22
- Classifier: Programming Language :: Python
@@ -1,44 +0,0 @@
1
- =========
2
- mathparse
3
- =========
4
-
5
- .. image:: https://travis-ci.org/gunthercox/mathparse.svg?branch=master
6
- :target: https://travis-ci.org/gunthercox/mathparse
7
-
8
- The mathparse library is a Python module designed to evaluate mathematical equations contained in strings.
9
-
10
- Here are a few examples:
11
-
12
- .. code-block:: python
13
-
14
- from mathparse import mathparse
15
-
16
- mathparse.parse('50 * (85 / 100)')
17
- >>> 42.5
18
-
19
- mathparse.parse('one hundred times fifty four', mathparse.codes.ENG)
20
- >>> 5400
21
-
22
- mathparse.parse('(seven * nine) + 8 - (45 plus two)', language='ENG')
23
- >>> 24
24
-
25
- Installation
26
- ============
27
-
28
- .. code-block:: bash
29
-
30
- pip install mathparse
31
-
32
- Language support
33
- ================
34
-
35
- The language parameter must be set in order to evaluate an equation that uses word operators.
36
- The language code should be a valid `ISO 639-2`_ language code.
37
-
38
- History
39
- =======
40
-
41
- See `release notes`_ for changes.
42
-
43
- .. _`ISO 639-2`: https://www.loc.gov/standards/iso639-2/php/code_list.php
44
- .. _`release notes`: https://github.com/gunthercox/ChatterBot/releases
@@ -1,8 +0,0 @@
1
- """
2
- mathparse is a library for solving mathematical equations contained in strings
3
- """
4
-
5
- __version__ = '0.1.2'
6
- __author__ = 'Gunther Cox'
7
- __email__ = 'gunthercx@gmail.com'
8
- __url__ = 'https://github.com/gunthercox/mathparse'
@@ -1,22 +0,0 @@
1
- Metadata-Version: 1.1
2
- Name: mathparse
3
- Version: 0.1.2
4
- Summary:
5
- mathparse is a library for solving mathematical equations contained in strings
6
-
7
- Home-page: https://github.com/gunthercox/mathparse
8
- Author: Gunther Cox
9
- Author-email: gunthercx@gmail.com
10
- License: MIT
11
- Description: UNKNOWN
12
- Keywords: mathparse
13
- Platform: any
14
- Classifier: Development Status :: 4 - Beta
15
- Classifier: Intended Audience :: Developers
16
- Classifier: License :: OSI Approved :: MIT License
17
- Classifier: Operating System :: OS Independent
18
- Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
- Classifier: Topic :: Scientific/Engineering :: Mathematics
20
- Classifier: Topic :: Text Processing :: General
21
- Classifier: Topic :: Text Processing :: Linguistic
22
- Classifier: Programming Language :: Python
@@ -1 +0,0 @@
1
-
mathparse-0.1.2/setup.py DELETED
@@ -1,41 +0,0 @@
1
- #!/usr/bin/env python
2
-
3
- from setuptools import setup
4
-
5
-
6
- # Dynamically retrieve the version information from the chatterbot module
7
- MATHPARSE = __import__('mathparse')
8
- VERSION = MATHPARSE.__version__
9
- AUTHOR = MATHPARSE.__author__
10
- AUTHOR_EMAIL = MATHPARSE.__email__
11
- URL = MATHPARSE.__url__
12
- DOC = MATHPARSE.__doc__
13
-
14
- setup(
15
- name='mathparse',
16
- version=VERSION,
17
- url=URL,
18
- author=AUTHOR,
19
- author_email=AUTHOR_EMAIL,
20
- description=DOC,
21
- packages=[
22
- 'mathparse',
23
- ],
24
- package_dir={'mathparse': 'mathparse'},
25
- license='MIT',
26
- zip_safe=True,
27
- platforms=['any'],
28
- keywords=['mathparse'],
29
- classifiers=[
30
- 'Development Status :: 4 - Beta',
31
- 'Intended Audience :: Developers',
32
- 'License :: OSI Approved :: MIT License',
33
- 'Operating System :: OS Independent',
34
- 'Topic :: Software Development :: Libraries :: Python Modules',
35
- 'Topic :: Scientific/Engineering :: Mathematics',
36
- 'Topic :: Text Processing :: General',
37
- 'Topic :: Text Processing :: Linguistic',
38
- 'Programming Language :: Python',
39
- ],
40
- test_suite='tests'
41
- )
File without changes