mathparse 0.2.3__tar.gz → 0.2.4__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mathparse
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  Summary: mathparse is a library for solving mathematical equations contained in strings
5
5
  Author: Gunther Cox
6
6
  License-Expression: MIT
@@ -2,4 +2,4 @@
2
2
  mathparse is a library for solving mathematical equations contained in strings
3
3
  """
4
4
 
5
- __version__ = '0.2.3'
5
+ __version__ = '0.2.4'
@@ -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
- pattern = r'\b' + re.escape(number) + r'\b'
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
 
@@ -403,6 +439,7 @@ def to_postfix(tokens: list) -> list:
403
439
  Convert a list of evaluatable tokens to postfix format.
404
440
  """
405
441
  precedence = {
442
+ '.': 5,
406
443
  '/': 4,
407
444
  '*': 4,
408
445
  '+': 3,
@@ -499,6 +536,17 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
499
536
  total = 'undefined'
500
537
  else:
501
538
  total = Decimal(str(a)) / Decimal(str(b))
539
+ elif token == '.':
540
+ # Treat decimal points as a binary operator that combines the
541
+ # integer and fractional part of two numbers
542
+ # Example: 53 . 25 = 53.25
543
+ if b == 0:
544
+ total = Decimal(a)
545
+ else:
546
+ # Count the digits in b to determine the divisor
547
+ digits = len(str(int(b)))
548
+ divisor = 10 ** digits
549
+ total = a + (b / divisor)
502
550
  else:
503
551
  raise PostfixTokenEvaluationException(
504
552
  'Unknown token "{}"'.format(token)