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的设计模式编程入门指南
Apr 02 Python
Python解析最简单的验证码
Jan 07 Python
一步步教你用Python实现2048小游戏
Jan 19 Python
Python实现检测文件MD5值的方法示例
Apr 11 Python
python检测空间储存剩余大小和指定文件夹内存占用的实例
Jun 11 Python
使用GitHub和Python实现持续部署的方法
May 09 Python
Python自动化运维之Ansible定义主机与组规则操作详解
Jun 13 Python
Python 闭包,函数分隔作用域,nonlocal声明非局部变量操作示例
Oct 14 Python
Python-opencv 双线性插值实例
Jan 17 Python
python识别验证码图片实例详解
Feb 17 Python
关于python中的xpath解析定位
Mar 06 Python
详解python3类型注释annotations实用案例
Jan 20 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
php xml-rpc远程调用
2008/12/19 PHP
php购物车实现方法
2015/01/03 PHP
常用的php图片处理类(水印、等比缩放、固定高宽)分享
2015/06/19 PHP
php数组函数array_push()、array_pop()及array_shift()简单用法示例
2020/01/26 PHP
Using the TextRange Object
2006/10/14 Javascript
为Extjs加加速(javascript加速)
2010/08/19 Javascript
基于jquery的使ListNav兼容中文首字拼音排序的实现代码
2011/07/10 Javascript
jQuery:节点(插入,复制,替换,删除)操作
2013/03/04 Javascript
深入理解JavaScript系列(28):设计模式之工厂模式详解
2015/03/03 Javascript
详解addEventListener的三个参数之useCapture
2015/03/16 Javascript
jQuery随手笔记之常用的jQuery操作DOM事件
2015/11/29 Javascript
JavaScript学习笔记之ES6数组方法
2016/03/25 Javascript
jQuery实现图片轮播效果代码
2016/09/27 Javascript
原生js实现可拖动的登录框效果
2017/01/21 Javascript
node.js使用redis储存session的方法
2018/09/26 Javascript
点击按钮弹出模态框的一系列操作代码实例
2019/03/29 Javascript
node.js中module模块的功能理解与用法实例分析
2020/02/14 Javascript
如何阻止移动端浏览器点击图片浏览
2020/08/29 Javascript
vue中使用vue-pdf的方法详解
2020/09/05 Javascript
使用Python操作Elasticsearch数据索引的教程
2015/04/08 Python
python数组过滤实现方法
2015/07/27 Python
django中模板的html自动转意方法
2018/05/27 Python
Python 2/3下处理cjk编码的zip文件的方法
2019/04/26 Python
详解python 爬取12306验证码
2019/05/10 Python
Python的缺点和劣势分析
2019/11/19 Python
Python PyPDF2模块安装使用解析
2020/01/19 Python
使用tensorflow显示pb模型的所有网络结点方式
2020/01/23 Python
lululemon美国官网:瑜伽服+跑步装备
2018/11/16 全球购物
Ray-Ban雷朋西班牙官网:全球领先的太阳眼镜品牌
2018/11/28 全球购物
手工制作的男士奢华英国鞋和服装之家:Goodwin Smith
2019/06/21 全球购物
印度在线购物网站:Paytmmall
2019/07/24 全球购物
乌克兰珠宝大卖场:Zlato.ua
2020/09/27 全球购物
员工年终演讲稿
2014/01/03 职场文书
《跨越百年的美丽》教学反思
2014/02/11 职场文书
学校总务处领导班子民主生活会对照检查材料思想汇报
2014/09/27 职场文书
2016年9月份红领巾广播稿
2015/12/21 职场文书