例えばこのテキストを
I have 2 apples.
これに変換したいとする
I have two apples.
そういう場合こちらのライブラリが便利だった
Github - num2words
こんな感じ
from num2words import num2words
text = "I have 2 apples."
print(text)
# I have 2 apples.
new_text = ""
split_words = [num2words(word) if word.isdecimal() else word for word in text.split()]
print(" ".join(split_words))
# I have two apples.
上記コードは下記GoogleColabratoryで再現できるようにしておいた。
GoogleColabratory - num2words.ipynb