Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. import math
  2.  
  3. # Tokens from 1000 and up
  4. _PRONOUNCE = [
  5. 'million ',
  6. 'thousand ',
  7. ''
  8. ]
  9.  
  10. # Tokens up to 90
  11. _SMALL = {
  12. '0' : '',
  13. '1' : 'one',
  14. '2' : 'two',
  15. '3' : 'three',
  16. '4' : 'four',
  17. '5' : 'five',
  18. '6' : 'six',
  19. '7' : 'seven',
  20. '8' : 'eight',
  21. '9' : 'nine',
  22. '10' : 'ten',
  23. '11' : 'eleven',
  24. '12' : 'twelve',
  25. '13' : 'thirteen',
  26. '14' : 'fourteen',
  27. '15' : 'fifteen',
  28. '16' : 'sixteen',
  29. '17' : 'seventeen',
  30. '18' : 'eighteen',
  31. '19' : 'nineteen',
  32. '20' : 'twenty',
  33. '30' : 'thirty',
  34. '40' : 'forty',
  35. '50' : 'fifty',
  36. '60' : 'sixty',
  37. '70' : 'seventy',
  38. '80' : 'eighty',
  39. '90' : 'ninety'
  40. }
  41.  
  42. def get_num(num):
  43. '''Get token <= 90, return '' if not matched'''
  44. return _SMALL.get(num, '')
  45.  
  46. def triplets(l):
  47. '''Split list to triplets. Pad last one with '' if needed'''
  48. res = []
  49. for i in range(int(math.ceil(len(l) / 3.0))):
  50. sect = l[i * 3 : (i + 1) * 3]
  51. if len(sect) < 3: # Pad last section
  52. sect += [''] * (3 - len(sect))
  53. res.append(sect)
  54. return res
  55.  
  56. def norm_num(num):
  57. """Normelize number (remove 0's prefix). Return number and string"""
  58. n = int(num)
  59. return n, str(n)
  60.  
  61. def small2eng(num):
  62. '''English representation of a number <= 999'''
  63. n, num = norm_num(num)
  64. hundred = ''
  65. ten = ''
  66. if len(num) == 3: # Got hundreds
  67. hundred = get_num(num[0]) + ' hundred'
  68. num = num[1:]
  69. n, num = norm_num(num)
  70. if (n > 20) and (n != (n / 10 * 10)): # Got ones
  71. tens = get_num(num[0] + '0')
  72. ones = get_num(num[1])
  73. ten = tens + ' ' + ones
  74. else:
  75. ten = get_num(num)
  76. if hundred and ten:
  77. return hundred + ' ' + ten
  78. #return hundred + ' and ' + ten
  79. else: # One of the below is empty
  80. return hundred + ten
  81.  
  82. def num2eng(num):
  83. '''English representation of a number'''
  84. num = str(long(num)) # Convert to string, throw if bad number
  85. if len(num)>7: # Sanity check
  86. raise ValueError('Number too big')
  87. elseif num[0]>=1:
  88. raise ValueError('Number too big')
  89. if num == '0': # Zero is a special case
  90. return 'zero'
  91.  
  92. # Create reversed list
  93. x = list(num)
  94. x.reverse()
  95. pron = [] # Result accumolator
  96. ct = len(_PRONOUNCE) - 1 # Current index
  97. for a, b, c in triplets(x): # Work on triplets
  98. p = small2eng(c + b + a)
  99. if p:
  100. pron.append(p + ' ' + _PRONOUNCE[ct])
  101. ct -= 1
  102. # Create result
  103. pron.reverse()
  104. return ', '.join(pron)
  105.  
  106. if __name__ == '__main__':
  107. print num2eng(1000000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement