python实现将英文单词表示的数字转换成阿拉伯数字的方法


Posted in Python onJuly 02, 2015

本文实例讲述了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 相关文章推荐
python类型强制转换long to int的代码
Feb 10 Python
python使用PIL缩放网络图片并保存的方法
Apr 24 Python
Python写的一个定时重跑获取数据库数据
Dec 28 Python
python 判断是否为正小数和正整数的实例
Jul 23 Python
全面分析Python的优点和缺点
Feb 07 Python
详解python中的线程
Feb 10 Python
详解Python3中的迭代器和生成器及其区别
Oct 09 Python
Django实现auth模块下的登录注册与注销功能
Oct 10 Python
Python startswith()和endswith() 方法原理解析
Apr 28 Python
python 实现弹球游戏的示例代码
Nov 17 Python
python动态规划算法实例详解
Nov 22 Python
python 使用csv模块读写csv格式文件的示例
Dec 02 Python
python脚本内运行linux命令的方法
Jul 02 #Python
举例区分Python中的浅复制与深复制
Jul 02 #Python
Python多进程机制实例详解
Jul 02 #Python
Python回调函数用法实例详解
Jul 02 #Python
在Python中marshal对象序列化的相关知识
Jul 01 #Python
python保存字符串到文件的方法
Jul 01 #Python
python选择排序算法实例总结
Jul 01 #Python
You might like
《一拳超人》埼玉一拳下去,他们存在了800年毫无意义!
2020/03/02 日漫
PHP入门教程之使用Mysqli操作数据库的方法(连接,查询,事务回滚等)
2016/09/11 PHP
thinkphp 手机号和用户名同时登录
2017/01/20 PHP
PHP实现将上传图片自动缩放到指定分辨率,并保持清晰度封装类示例
2019/06/17 PHP
dojo 之基础篇
2007/03/24 Javascript
基于jquery的仿百度搜索框效果代码
2011/04/11 Javascript
JavaScript列表框listbox全选和反选的实现方法
2015/03/18 Javascript
深入解析JavaScript中的立即执行函数
2016/05/21 Javascript
js 实现省市区三级联动菜单效果
2017/02/20 Javascript
微信小程序获取微信运动步数的实例代码
2017/07/20 Javascript
nodejs 图解express+supervisor+ejs的用法(推荐)
2017/09/08 NodeJs
基于jquery的on和click的区别详解
2018/01/15 jQuery
zTree 树插件实现全国五级地区点击后加载的示例
2018/02/05 Javascript
vue2.0 子组件改变props值,并向父组件传值的方法
2018/03/01 Javascript
Django+Vue实现WebSocket连接的示例代码
2019/05/28 Javascript
基于Vue 撸一个指令实现拖拽功能
2019/10/09 Javascript
Vue+element-ui添加自定义右键菜单的方法示例
2020/12/08 Vue.js
Python bsddb模块操作Berkeley DB数据库介绍
2015/04/08 Python
Python构造自定义方法来美化字典结构输出的示例
2016/06/16 Python
Python的装饰器用法学习笔记
2016/06/24 Python
Python的语言类型(详解)
2017/06/24 Python
python抓取网页中链接的静态图片
2018/01/29 Python
如何使用python把ppt转换成pdf
2019/06/29 Python
解决python 找不到module的问题
2020/02/12 Python
python 读txt文件,按‘,’分割每行数据操作
2020/07/05 Python
python实现猜拳游戏项目
2020/11/30 Python
css 如何让背景图片拉伸填充避免重复显示
2013/07/11 HTML / CSS
约瑟夫·特纳男装:Joseph Turner
2017/10/10 全球购物
阿根廷首家户外用品制造商和经销商:Montagne
2018/02/12 全球购物
关于递归的一道.NET面试题
2013/05/12 面试题
办理生育手续介绍信
2014/01/14 职场文书
硕士研究生求职自荐信范文
2014/03/11 职场文书
副乡长群众路线教育实践活动个人对照检查材料
2014/09/19 职场文书
民间借贷纠纷答辩状
2015/08/03 职场文书
Apache Linkis 中间件架构及快速安装步骤
2022/03/16 Servers
【海涛教你打DOTA】虚空假面第一视角骨弓3房29杀
2022/04/01 DOTA