Skip to content

Instantly share code, notes, and snippets.

@sdfsdhgjkbmnmxc
Forked from Oziabr/parseNum.py
Last active September 8, 2018 16:50
Show Gist options
  • Save sdfsdhgjkbmnmxc/8817eef55cfb3bcffa4493b017e23c63 to your computer and use it in GitHub Desktop.
Save sdfsdhgjkbmnmxc/8817eef55cfb3bcffa4493b017e23c63 to your computer and use it in GitHub Desktop.
d = {}
for i, v in enumerate('zero one two three four five six seven eight nine ten eleven twelve thirteen'.split()):
d[v] = i
for v, i in d.items():
if 3 < i < 10:
d[v + 'teen'] = i + 10
d.update({
'fifteen': 15,
'eighteen': 18,
})
for i, v in enumerate('twenty thirty forty fifty sixty seventy eighty ninety'.split()):
d[v] = (i + 2) * 10
def parse_int(s):
if 'million' in s:
return 1E6
s = s.replace('thousand', '|').replace('and ', '').replace('-', ' ').replace('hundred', '-')
keys = [[sum(d[k] for k in x.strip().split()) for x in reversed(a.split('-'))] for a in reversed(s.split('|'))]
return sum(sum(100 ** j * 1000 ** i * a for j, a in enumerate(b)) for i, b in enumerate(keys))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment