Created
November 17, 2012 11:03
-
-
Save mharju/4094920 to your computer and use it in GitHub Desktop.
method to transform number strings to finnish
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 | |
numbers = (u'nolla',u'yksi',u'kaksi',u'kolme',u'neljä',u'viisi',u'kuusi',u'seitsemän',u'kahdeksan',u'yhdeksän',u'kymmenen') | |
suffixes = ((u'toista',u'kymmentä'), (u'sata',u'sataa')) | |
exponents = ((u'',u''), (u'tuhat',u'tuhatta'), (u'miljoona','miljoonaa')) | |
def ones(n, s): | |
if s == 0 or n == 1: | |
return u'' | |
return numbers[s] | |
def tens(n, s): | |
if n == 0: | |
return u'' | |
if n == 1 and s == 0: | |
return numbers[10] | |
elif n == 1: | |
return numbers[s] + suffixes[0][0] | |
else: | |
return numbers[n] + suffixes[0][1] | |
def hundreds(c): | |
if c == 0: | |
return u'' | |
return numbers[c] + suffixes[1][1] if c > 1 else suffixes[1][0] | |
def thousands(index, c, n, s): | |
if c == 0 and n == 0 and s == 0: | |
return u'' | |
elif index != 0 and c == 0 and n == 0 and s == 1: | |
return exponents[index][0] | |
else: | |
return hundreds(c) + tens(n, s) + ones(n, s) + exponents[index][1] | |
def partition(l, n): | |
for i in range(0, len(l), n): | |
yield l[i:i+n] | |
def pad(number, n): | |
return "0" * (n - len(number) % n) + number | |
def say(number): | |
if len(number) == 1: | |
global numbers | |
return numbers[int(number)] | |
converted = [int(n) for n in pad(number, 3)] | |
count = len(converted) / 3 - 1 | |
return ''.join([thousands(count - index, *parts) for index, parts in enumerate(partition(converted, 3))]) | |
print [say(s) for s in ["0", "2", "1", "10", "315", "94101", "1024", "1529933", "20203"]] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment