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 相关文章推荐
python2.7删除文件夹和删除文件代码实例
Dec 18 Python
Python socket.error: [Errno 98] Address already in use的原因和解决方法
Aug 25 Python
老生常谈python函数参数的区别(必看篇)
May 29 Python
Python实现导出数据生成excel报表的方法示例
Jul 12 Python
python微信跳一跳系列之棋子定位像素遍历
Feb 26 Python
快速排序的四种python实现(推荐)
Apr 03 Python
详解Python的循环结构知识点
May 20 Python
如何使用Python自动控制windows桌面
Jul 11 Python
Python日志logging模块功能与用法详解
Apr 09 Python
python安装及变量名介绍详解
Dec 12 Python
python time.strptime格式化实例详解
Feb 03 Python
Python采集壁纸并实现炫轮播
Apr 30 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
MySQL数据源表结构图示
2008/06/05 PHP
php _autoload自动加载类与机制分析
2012/02/10 PHP
深入PHP购物车模块功能分析(函数讲解,附源码)
2013/06/25 PHP
php获取网页标题和内容函数(不包含html标签)
2014/02/03 PHP
php+mysql大量用户登录解决方案分析
2014/12/29 PHP
详解Yii2 定制表单输入字段的标签和样式
2017/01/04 PHP
Array的push与unshift方法性能比较分析
2011/03/05 Javascript
Jquery index()方法 获取相应元素索引值
2012/10/12 Javascript
JS实现的自定义右键菜单实例二则
2015/09/01 Javascript
jQuery实用技巧必备(下)
2015/11/03 Javascript
浅谈Web页面向后台提交数据的方式和选择
2016/09/23 Javascript
JS多物体实现缓冲运动效果示例
2016/12/20 Javascript
js eval函数使用,js对象和字符串互转实例
2017/03/06 Javascript
整理关于Bootstrap排版的慕课笔记
2017/03/29 Javascript
element-ui table组件如何使用render属性的实现
2019/11/04 Javascript
Vue中使用wangeditor富文本编辑的问题
2021/02/07 Vue.js
[01:18:33]Secret vs VGJ.S Supermajor小组赛C组 BO3 第一场 6.3
2018/06/04 DOTA
Python中endswith()函数的基本使用
2015/04/07 Python
Python中使用copy模块实现列表(list)拷贝
2015/04/14 Python
自己使用总结Python程序代码片段
2015/06/02 Python
matplotlib subplots 调整子图间矩的实例
2018/05/25 Python
pytorch 预训练层的使用方法
2019/08/20 Python
html5是什么_动力节点Java学院整理
2017/07/07 HTML / CSS
Spartoo瑞典:鞋子、包包和衣服
2018/09/15 全球购物
北京某公司的.net笔试题
2014/03/20 面试题
内容编辑个人求职信
2013/12/10 职场文书
教师自我鉴定
2013/12/13 职场文书
医学专业职业生涯规划范文
2014/02/05 职场文书
《小猫刮胡子》教学反思
2014/02/21 职场文书
人事部专员岗位职责
2014/03/04 职场文书
影视广告专业求职信
2014/09/02 职场文书
防汛工作情况汇报
2014/10/28 职场文书
超搞笑婚前保证书
2015/05/08 职场文书
人事部:年度述职报告范文
2019/07/12 职场文书
【超详细】八大排序算法的各项比较以及各自特点
2021/03/31 Python
解析python中的jsonpath 提取器
2022/01/18 Python