将文本数字转换为字符串中的数字的小型库

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 并加以改进

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何向xsi:nil元素添加另一个属性?   Java抽象泛型方法,使用具体类型实现通配符   java使用pcap4j截断pcap文件   当我放置字母a、b和c时,java中的异常预期会下降   java设置活动对话框不可取消   接口类型变量上的Java克隆   使用Java或BouncyCastle对CSR(证书签名请求)进行安全解码/读取   java调用SavingsAccount对象上的函数并打印结果   java如何在Android应用程序上显示地图上的兴趣点(POI)并与之交互?   如果在JavaFX中的ResultSet中未找到任何内容,则显示java警报   java我将springboot和@component与@scheduled一起使用,它每12小时锁定一次   ApachePOI如何使用java删除包含字符串的word表的行   java如果对象(x,y)靠近其他对象(x,y)   从未对JMSException调用java JMS CachingConnectionFactory OneException方法   javascript使用java将HTML页面转换为MS word