python实现将英文单词表示的数字转换成阿拉伯数字的方法
作者:秋风秋雨 发布时间:2022-07-12 03:01:11
标签:python,数字,转换
本文实例讲述了python实现将英文单词表示的数字转换成阿拉伯数字的方法。分享给大家供大家参考。具体实现方法如下:
import re
_known = {
'zero': 0,
'one': 1,
'two': 2,
'three': 3,
'four': 4,
'five': 5,
'six': 6,
'seven': 7,
'eight': 8,
'nine': 9,
'ten': 10,
'eleven': 11,
'twelve': 12,
'thirteen': 13,
'fourteen': 14,
'fifteen': 15,
'sixteen': 16,
'seventeen': 17,
'eighteen': 18,
'nineteen': 19,
'twenty': 20,
'thirty': 30,
'forty': 40,
'fifty': 50,
'sixty': 60,
'seventy': 70,
'eighty': 80,
'ninety': 90
}
def spoken_word_to_number(n):
"""Assume n is a positive integer".
assert _positive_integer_number('nine hundred') == 900
assert spoken_word_to_number('one hundred') == 100
assert spoken_word_to_number('eleven') == 11
assert spoken_word_to_number('twenty two') == 22
assert spoken_word_to_number('thirty-two') == 32
assert spoken_word_to_number('forty two') == 42
assert spoken_word_to_number('two hundred thirty two') == 232
assert spoken_word_to_number('two thirty two') == 232
assert spoken_word_to_number('nineteen hundred eighty nine') == 1989
assert spoken_word_to_number('nineteen eighty nine') == 1989
assert spoken_word_to_number('one thousand nine hundred and eighty nine') == 1989
assert spoken_word_to_number('nine eighty') == 980
assert spoken_word_to_number('nine two') == 92 # wont be able to convert this one
assert spoken_word_to_number('nine thousand nine hundred') == 9900
assert spoken_word_to_number('one thousand nine hundred one') == 1901
"""
n = n.lower().strip()
if n in _known:
return _known[n]
else:
inputWordArr = re.split('[ -]', n)
assert len(inputWordArr) > 1 #all single words are known
#Check the pathological case where hundred is at the end or thousand is at end
if inputWordArr[-1] == 'hundred':
inputWordArr.append('zero')
inputWordArr.append('zero')
if inputWordArr[-1] == 'thousand':
inputWordArr.append('zero')
inputWordArr.append('zero')
inputWordArr.append('zero')
if inputWordArr[0] == 'hundred':
inputWordArr.insert(0, 'one')
if inputWordArr[0] == 'thousand':
inputWordArr.insert(0, 'one')
inputWordArr = [word for word in inputWordArr if word not in ['and', 'minus', 'negative']]
currentPosition = 'unit'
prevPosition = None
output = 0
for word in reversed(inputWordArr):
if currentPosition == 'unit':
number = _known[word]
output += number
if number > 9:
currentPosition = 'hundred'
else:
currentPosition = 'ten'
elif currentPosition == 'ten':
if word != 'hundred':
number = _known[word]
if number < 10:
output += number*10
else:
output += number
#else: nothing special
currentPosition = 'hundred'
elif currentPosition == 'hundred':
if word not in [ 'hundred', 'thousand']:
number = _known[word]
output += number*100
currentPosition = 'thousand'
elif word == 'thousand':
currentPosition = 'thousand'
else:
currentPosition = 'hundred'
elif currentPosition == 'thousand':
assert word != 'hundred'
if word != 'thousand':
number = _known[word]
output += number*1000
else:
assert "Can't be here" == None
return(output)
希望本文所述对大家的Python程序设计有所帮助。


猜你喜欢
- 本文实例为大家分享了python模拟登录图书馆的具体代码,供大家参考,具体内容如下模拟表单提交的原理:我们都知道Http是无状态的,所以当我
- 如下所示:(x,y)为要转的点,(pointx,pointy)为中心点,如果顺时针角度为anglesrx = (x-pointx)*cos(
- 有时候我反问我自己,怎么不知道在Python 3中用更简单的方式做“这样”的事,当我寻求答案时,随着时间的推移,我当然发现更简洁、有效并且b
- 原文地址:http://ilovetypography.com/2007/10/22/so-you-want-to-create-a-fon
- 一、安装pip install pymongo二、连接数据库import pymongo# 方式一client = pymongo.Mong
- 我们都知道ACCESS是ASP的亲密伙伴。因为两种最简单的东西碰在一起总能迸发出火花。然而,当我们过滤不严格的时候经常出现日文字符,这个时候
- 说明本文根据https://github.com/liuchengxu/blockchain-tutorial的内容,用python实现的,
- 本文实例为大家分享了使用RNN进行文本分类,python代码实现,供大家参考,具体内容如下1、本博客项目由来是oxford 的nlp 深度学
- 1.简介当一个表数据量很大时候,很自然我们就会想到将表拆分成很多小表,在执行查询时候就到各个小表去查,最后汇总数据集返回给调用者加快查询速度
- CSS的背景属性“background”提供了众多属性值,如颜色、图像、定位等,为网页背景图像的定义提供了极大的便利。看看backgroun
- 赶快记录一下,只是懂皮毛,或许多积累就好了import sysfrom PyQt4 import QtGuiclass MainWindow
- 研究了一段时间酷狗音乐的接口,完美破解了其vip音乐下载方式,想着能更好的追求开源,故写下此篇文章,本文仅供学习参考。虽然没什么
- 我们这里所说的head区域,是指页页html代码的<head>和</head>之间的内容。在以前的文章中,主要介绍了
- MySQL在5.1引入了一个rename database操作,但在MySQL5.1.23后又不支持这个命令。可以说是一个实验性的功能,没有
- 代码如下所示:$num = 10.4567; //第一种:利用round()对浮点数进行四舍五入 &n
- python压缩和解压缩模块之zlib由于早期的zlib和Python之间不兼容,故推荐1.1.4以后的版本。导入zlib后可以查看版本号&
- 本文实例为大家分享了python实现转盘效果的具体代码,供大家参考,具体内容如下#抽奖 面向对象版本import tkinterimport
- Python 用了好长一段时间了,起初是基于对爬虫的兴趣而接触到的。随着不断的深入,慢慢的转了其它语言,毕竟工作机会真的太少了。很多技能长时
- 临时表与内存表内存表,指的是使用Memory引擎的表,建表语法是create table … engine=memory。这种 表的数据都保
- 1. 首先看要设置登陆的界面 book/view.py@user_util.my_login #相当于 select_all=my_logi