python实现将英文单词表示的数字转换成阿拉伯数字的方法

 更新时间:2015年07月02日 14:48:35   作者:秋风秋雨  
这篇文章主要介绍了python实现将英文单词表示的数字转换成阿拉伯数字的方法,涉及Python字符串转换操作的相关技巧,需要的朋友可以参考下

本文实例讲述了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中的pprint模块

    Python中的pprint模块

    本文介绍了 Python中的pprint模块,pprint模块包含一个“美观打印机”,用于生成数据结构的一个美观的视图。格式化工具会生成数据结构的一些表示,不仅能够由解释器正确地解析,还便于人阅读。输出会尽可能放在一行上,分解为多行时会缩进,想了解具体内容请参考下文
    2021-11-11
  • Python学习之str重要函数

    Python学习之str重要函数

    这篇文章主要介绍了Python str重要函数,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-10-10
  • python 判断矩阵中每行非零个数的方法

    python 判断矩阵中每行非零个数的方法

    今天小编就为大家分享一篇python 判断矩阵中每行非零个数的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • Python进阶语法之类的继承

    Python进阶语法之类的继承

    这篇文章主要为大家介绍了Python类的继承,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-12-12
  • python进程池和线程池的区别

    python进程池和线程池的区别

    本文主要介绍了python进程池和线程池的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-12-12
  • python实现CTC以及案例讲解

    python实现CTC以及案例讲解

    这篇文章主要介绍了python实现CTC以及案例讲解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Python一行代码实现快速排序的方法

    Python一行代码实现快速排序的方法

    排序算法是在高考或中考中出现频率最多的点,所以大家要掌握,今天小编给大家带来了通过Python一行代码实现快速排序的方法,感兴趣的朋友跟随小编一起看看吧
    2019-04-04
  • Pytorch中实现只导入部分模型参数的方式

    Pytorch中实现只导入部分模型参数的方式

    今天小编就为大家分享一篇Pytorch中实现只导入部分模型参数的方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • python列表详情

    python列表详情

    这篇文章主要介绍了python列表详情,python没有数组,而是引入了列表(list),列表可以存储任何类型的数据,而且同一个列表中的数据类型也可以不同,下面一起来看文章详细内容吧
    2021-12-12
  • Django中日期时间型字段进行年月日时分秒分组统计

    Django中日期时间型字段进行年月日时分秒分组统计

    这篇文章主要介绍了Django中日期时间型字段进行年月日时分秒分组统计,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11

最新评论