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程序与MySQL的教程
Apr 29 Python
Python中列表元素转为数字的方法分析
Jun 14 Python
Python 编码处理-str与Unicode的区别
Sep 06 Python
分分钟入门python语言
Mar 20 Python
Python中的二维数组实例(list与numpy.array)
Apr 13 Python
Python3多目标赋值及共享引用注意事项
May 27 Python
Python2与Python3的区别点整理
Dec 12 Python
Pytorch实现神经网络的分类方式
Jan 08 Python
keras Lambda自定义层实现数据的切片方式,Lambda传参数
Jun 11 Python
python实现黄金分割法的示例代码
Apr 28 Python
忆童年!用Python实现愤怒的小鸟游戏
Jun 07 Python
使用python将HTML转换为PDF pdfkit包(wkhtmltopdf) 的使用方法
Apr 21 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中error_log()函数的使用方法
2015/01/20 PHP
win10环境PHP 7 安装配置【教程】
2016/05/09 PHP
php自定义中文字符串截取函数substr_for_gb2312及substr_for_utf8示例
2016/05/28 PHP
PHP使用反向Ajax技术实现在线客服系统详解
2019/07/01 PHP
js使用post 方式打开新窗口
2015/02/26 Javascript
JavaScript使用addEventListener添加事件监听用法实例
2015/06/01 Javascript
基于jquery插件编写countdown计时器
2016/06/12 Javascript
JavaScript函数表达式详解及实例
2017/05/05 Javascript
bootstrap轮播图示例代码分享
2017/05/17 Javascript
vue中使用localstorage来存储页面信息
2017/11/04 Javascript
vue动态注册组件实例代码详解
2019/05/30 Javascript
过滤器vue.filters的使用方法实现
2019/09/18 Javascript
Vue多选列表组件深入详解
2021/03/02 Vue.js
python实现绘制树枝简单示例
2014/07/24 Python
Python 创建子进程模块subprocess详解
2015/04/08 Python
在Python中使用CasperJS获取JS渲染生成的HTML内容的教程
2015/04/09 Python
python实现从ftp服务器下载文件的方法
2015/04/30 Python
python中reload(module)的用法示例详解
2017/09/15 Python
Python中实现变量赋值传递时的引用和拷贝方法
2018/04/29 Python
使用python打印十行杨辉三角过程详解
2019/07/10 Python
使用python绘制温度变化雷达图
2019/10/18 Python
python如何实现不用装饰器实现登陆器小程序
2019/12/14 Python
Python中常用的高阶函数实例详解
2020/02/21 Python
python中sympy库求常微分方程的用法
2020/04/28 Python
CSS3实现千变万化的文字阴影text-shadow效果设计
2016/04/26 HTML / CSS
CSS3 box-sizing属性详解
2016/11/15 HTML / CSS
html5 worker 实例(二) 图片变换效果
2013/06/24 HTML / CSS
如何设置Java的运行环境
2013/04/05 面试题
简述进程的启动、终止的方式以及如何进行进程的查看
2013/07/12 面试题
经典的班主任推荐信
2013/10/28 职场文书
社区平安建设方案
2014/05/25 职场文书
2014年世界艾滋病日宣传活动总结
2014/11/18 职场文书
优秀校长事迹材料
2014/12/24 职场文书
解决python存数据库速度太慢的问题
2021/04/23 Python
MySQL中LAG()函数和LEAD()函数的使用
2022/08/14 MySQL
TS 类型收窄教程示例详解
2022/09/23 Javascript