MathSpell is a Python package for converting numbers into contextually appropriate words, such as "twenty-first century" for years or "two thousand and twenty-three" for general numbers.
-
Install the package:
pip install mathspell
-
Download the required spaCy language model:
python -m spacy download en_core_web_sm
-
Upgrade Pint to it's latest version if it is incompatible with the current version of numpy.
pip install --upgrade pint
After installation, you can use MathSpell to process text containing numbers. For example:
from mathspell import analyze_text
text = "This is the 21st century. I was born in 1995."
converted_text = analyze_text(text)
print(converted_text)
# Output: "This is the twenty-first century. I was born in nineteen ninety-five."
from mathspell import analyze_text
input_text = "Something happened in 2021."
output = analyze_text(input_text)
print(output)
# Output: "Something happened in twenty twenty-one."
from mathspell import analyze_text
input_text = "This is my 3rd attempt to fix the bug."
output = analyze_text(input_text)
print(output)
# Output: "This is my third attempt to fix the bug."
from mathspell import analyze_text
input_text = "My birthday is on 4th April, 1993."
output = analyze_text(input_text)
print(output)
# Output: "My birthday is on fourth April, nineteen ninety-three."
from mathspell import analyze_text
input_text = "This contains 15 boxes."
output = analyze_text(input_text)
print(output)
# Output: "This contains fifteen boxes."
from mathspell import analyze_text
input_text = "This is the 1st floor, and the elevator can hold 5 people."
output = analyze_text(input_text)
print(output)
# Output: "This is the first floor, and the elevator can hold five people."
from mathspell import analyze_text
input_text = "The temperature is expected to be 25 degrees tomorrow."
output = analyze_text(input_text)
print(output)
# Output: "The temperature is expected to be twenty-five degrees tomorrow."
from mathspell import analyze_text
input_text = "The 2nd prize was awarded in 2022 for the 10th time."
output = analyze_text(input_text)
print(output)
# Output: "The second prize was awarded in twenty twenty-two for the tenth time."