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实现telnet服务器的方法
Jul 10 Python
python 二分查找和快速排序实例详解
Oct 13 Python
python利用rsa库做公钥解密的方法教程
Dec 10 Python
详解tensorflow实现迁移学习实例
Feb 10 Python
基于Python在MacOS上安装robotframework-ride
Dec 28 Python
Python进度条的制作代码实例
Aug 31 Python
python绘制无向图度分布曲线示例
Nov 22 Python
pygame用blit()实现动画效果的示例代码
May 28 Python
django 将自带的数据库sqlite3改成mysql实例
Jul 09 Python
Python Request类源码实现方法及原理解析
Aug 17 Python
简单了解python关键字global nonlocal区别
Sep 21 Python
python中如何打包用户自定义模块
Sep 23 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
文章推荐系统(三)
2006/10/09 PHP
Yii2基于Ajax自动获取表单数据的方法
2016/08/10 PHP
深入理解 PHP7 中全新的 zval 容器和引用计数机制
2018/10/15 PHP
javascript跟随滚动效果插件代码(javascript Follow Plugin)
2013/08/03 Javascript
JavaScript中的运算符种类及其规则介绍
2013/09/26 Javascript
jquery $(this).attr $(this).val方法使用介绍
2013/10/08 Javascript
JavaScript charCodeAt方法入门实例(用于取得指定位置字符的Unicode编码)
2014/10/17 Javascript
angular-ngSanitize模块-$sanitize服务详解
2017/06/13 Javascript
详解自定义ajax支持跨域组件封装
2018/02/08 Javascript
Vue项目中使用flow做类型检测的方法
2020/03/18 Javascript
原生JS实现汇率转换功能代码实例
2020/05/13 Javascript
jQuery cookie的公共方法封装和使用示例
2020/06/01 jQuery
js屏蔽F12审查元素,禁止修改页面代码等实现代码
2020/10/02 Javascript
python基础入门详解(文件输入/输出 内建类型 字典操作使用方法)
2013/12/08 Python
python操作数据库之sqlite3打开数据库、删除、修改示例
2014/03/13 Python
常见的在Python中实现单例模式的三种方法
2015/04/08 Python
Django后台获取前端post上传的文件方法
2018/05/28 Python
Pyqt5 实现跳转界面并关闭当前界面的方法
2019/06/19 Python
python获取Linux发行版名称
2019/08/30 Python
python实现五子棋游戏(pygame版)
2020/01/19 Python
详解Python 中的容器 collections
2020/08/17 Python
python中watchdog文件监控与检测上传功能
2020/10/30 Python
HTML5中indexedDB 数据库的使用实例
2017/05/11 HTML / CSS
欧洲著名的珠宝和手表网上商城:uhrcenter
2017/04/10 全球购物
银行实习生的自我评价
2014/01/13 职场文书
英语课前三分钟演讲稿
2014/08/19 职场文书
四风问题个人自查剖析材料思想汇报
2014/09/21 职场文书
早读课迟到检讨书
2014/09/25 职场文书
征用土地赔偿协议书
2014/09/26 职场文书
运动会广播稿200字
2014/10/18 职场文书
先进个人事迹材料范文
2014/12/30 职场文书
2015年社区党务工作总结
2015/04/21 职场文书
幼儿园教师师德师风承诺书
2015/04/28 职场文书
庆祝教师节主题班会
2015/08/17 职场文书
高并发下Redis如何保持数据一致性(避免读后写)
2022/03/18 Redis
nginx访问报403错误的几种情况详解
2022/07/23 Servers