将文本数字转换为字符串中的数字的小型库
text2digits的Python项目详细描述
安装
pip3 install text2digits
用法
仅限Python3!
from text2digits import text2digits
t2d = text2digits.Text2Digits()
t2d.convert("twenty ten and twenty one")
> 2010 and 21
它能处理各种短语。口语/非正式和正式语言:
"A random string" -> 'a random string'
"I am thirty six years old with a child that is four. I would like to get him four cars!" -> 'i am 36 years old with a child that is 4. i would like to get him 4 cars!'
"I was born in twenty ten" -> 'i was born in 2010'
"I was born in nineteen sixty four" -> 'i was born in 1964'
"I am the fourth cousin" -> 'i am the 4 cousin'
"I am twenty nine" -> 'i am 29'
"it was twenty ten and was negative thirty seven degrees" -> 'it was 2010 and was negative 37 degrees'
"thirty twenty one" -> '3021'
"one thousand six hundred sixty six" -> '1666'
"one thousand and six hundred and sixty six" -> '1666'
"sixteen sixty six" -> '1666'
"eleven hundred twelve" -> '1112'
"Sixteen and seven" -> '16 and 7'
"twenty ten and twenty one" -> '2010 and 21'
"I was born in nineteen ninety two and am twenty six years old!" -> 'i was born in 1992 and am 26 years old!'
"three forty five" -> '345'
如果使用alexa/lex将音频转换为文本,并且必须将文本转换为数字,我觉得这很有用。
改进/问题
- 仍然需要添加对十进制数的支持
- 需要添加对负数的支持
致谢
我在这里大量使用了so answers的代码:https://stackoverflow.com/questions/493174/is-there-a-way-to-convert-number-words-to-integers 并加以改进