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多线程下载文件的方法
Jul 10 Python
Python随机数random模块使用指南
Sep 09 Python
Python实现的递归神经网络简单示例
Aug 11 Python
python版简单工厂模式
Oct 16 Python
Python无损音乐搜索引擎实现代码
Feb 02 Python
python的dataframe和matrix的互换方法
Apr 11 Python
pycharm 主题theme设置调整仿sublime的方法
May 23 Python
神经网络相关之基础概念的讲解
Dec 29 Python
python 中pyqt5 树节点点击实现多窗口切换问题
Jul 04 Python
Python中zip()函数的解释和可视化(实例详解)
Feb 16 Python
python中os包的用法
Jun 01 Python
Python实现JS解密并爬取某音漫客网站
Oct 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
也谈 PHP 和 MYSQL
2006/10/09 PHP
php文件操作之小型留言本实例
2015/06/20 PHP
摘自织梦CMS中的图片处理类
2015/08/08 PHP
PHP扩展迁移为PHP7扩展兼容性问题记录
2016/02/15 PHP
php简单实现单态设计模式的方法分析
2017/07/28 PHP
Laravel 5.4.36中session没有保存成功问题的解决
2018/02/19 PHP
详解php伪造Referer请求反盗链资源
2019/01/24 PHP
很可爱的输入框
2008/08/03 Javascript
Javascript select下拉框操作常用方法
2009/11/09 Javascript
jQuery Validate表单验证入门学习
2015/12/18 Javascript
Sea.JS知识总结
2016/05/05 Javascript
Bootstrap导航条可点击和鼠标悬停显示下拉菜单
2016/11/25 Javascript
JavaScript实现的冒泡排序法及统计相邻数交换次数示例
2017/04/26 Javascript
JS实现批量上传文件并显示进度功能
2017/06/27 Javascript
常见的浏览器Hack技巧整理
2017/06/29 Javascript
从对象列表中获取一个对象的方法,依据关键字和值
2017/09/20 Javascript
jQuery实现获取动态添加的标签对象示例
2018/06/28 jQuery
Python Socket编程入门教程
2014/07/11 Python
python访问类中docstring注释的实现方法
2015/05/04 Python
Python 数据库操作 SQLAlchemy的示例代码
2019/02/18 Python
python如何以表格形式打印输出的方法示例
2019/06/21 Python
django项目中新增app的2种实现方法
2020/04/01 Python
Python3实现个位数字和十位数字对调, 其乘积不变
2020/05/03 Python
Python压缩模块zipfile实现原理及用法解析
2020/08/14 Python
Ubuntu20.04环境安装tensorflow2的方法步骤
2021/01/29 Python
英国珠宝和手表专家:Pleasance & Harper
2020/10/21 全球购物
what is the difference between ext2 and ext3
2015/08/25 面试题
物资采购方案
2014/06/12 职场文书
优秀本科毕业生自荐信
2014/07/04 职场文书
2014最新版群众路线四风整改措施
2014/09/24 职场文书
2014县政府领导班子三严三实对照检查材料思想汇报
2014/09/26 职场文书
校园新闻广播稿5篇
2014/10/10 职场文书
打架检讨书范文
2015/01/27 职场文书
公司放假通知怎么写
2015/04/15 职场文书
2016廉洁从政心得体会
2016/01/19 职场文书
Python文件的操作示例的详细讲解
2021/04/08 Python