number-parser
number-parser is a Python library designed to convert numbers written in natural language into their equivalent numeric forms. It supports cardinal numbers in English, Hindi, Spanish, Ukrainian, and Russian, and also handles ordinal numbers and simple fractions in English. The current version is 0.3.2, and it maintains an active, though somewhat irregular, release cadence with several updates in early 2023.
Warnings
- breaking Prior to version 0.3.2, users might have encountered an import bug (#91), which could lead to incorrect or failed imports of the parsing functions. This was fixed in version 0.3.2.
- gotcha Inconsistent whitespace handling around sentence separators following numbers was an issue in versions prior to 0.3.1, potentially affecting parsing accuracy in specific contexts.
- gotcha The library's language support is granular: cardinal numbers are supported in multiple languages, but ordinal numbers and simple fractions are currently supported only for English. Attempting to parse non-English ordinals or fractions might yield unexpected results or errors.
- gotcha Parsing natural language numbers can be inherently ambiguous, especially across different locales and contexts (e.g., 'one hundred thousand' vs. 'one, hundred thousand'). While the library aims for robust parsing, complex or unusual phrasing might not always be interpreted as expected.
Install
-
pip install number-parser
Imports
- parse
from number_parser import parse
- parse_number
from number_parser import parse_number
- parse_ordinal
from number_parser import parse_ordinal
- parse_fraction
from number_parser import parse_fraction
Quickstart
from number_parser import parse
# Convert numbers in a sentence
text = "I have two hats and thirty seven coats."
parsed_text = parse(text)
print(f"Original: {text}\nParsed: {parsed_text}")
# Convert a single number in words
number_in_words = "two thousand and twenty"
parsed_number = parse_number(number_in_words)
print(f"Number '{number_in_words}' parsed to: {parsed_number}")