Skip to content

Instantly share code, notes, and snippets.

@loxs
Created July 13, 2010 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loxs/473592 to your computer and use it in GitHub Desktop.
Save loxs/473592 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# Copyright (c) 2010 Metin Akat
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
words = {0: 'нула', 1: 'един', 2: 'два', 3: 'три', 4: 'четири', 5: 'пет',
6: 'шест',7: 'седем', 8: 'осем', 9: 'девет', 10: 'десет',
11: 'единадесет', 100: 'сто', 200: 'двеста', 300: 'триста',
1000: 'хиляда', 1000000: 'един милион', 1000000000: 'един милиард'}
words.update(dict([(x+10, words[x] + 'надесет') for x in range(2,10)] +
[(x*10, words[x] + 'десет') for x in range(2,10)] +
[(x*100, words[x] + 'стотин') for x in range(4,10)]))
bigwords = ['', ' хиляди', ' милиона', ' милиарда', ' трилиона']
correctwords = {'един хиляди': 'една хиляди', 'два хиляди': 'две хиляди',
'един стотинки': 'една стотинки', 'два стотинки': 'две стотинки'}
def group2words(num, i):
string = ''
single = int(num) * 1000 ** i
if single in words:
return words[single]
elif int(num) in words:
return words[int(num)] + bigwords[i]
else:
hundreds, y = divmod(int(num), 100)
if hundreds: string += words[hundreds*100]
if y in words: string += ' и %s' % words[y]
else:
tens, y = divmod(y, 10)
if tens: string += ' %s и %s' % (words[tens*10], words[y])
return string + bigwords[i]
def num2words(num):
rev = str(num)[::-1]
ls = [rev[x:x+3][::-1] for x in range(0, len(rev), 3)]
mls = [group2words(ls[x], x) for x in range(len(ls))][::-1]
if len(mls) > 1: ret = ' '.join(mls[:-1]) + ' и ' + mls[-1]
else: ret = ''.join(mls)
return ret.strip()
def correct(moneyString):
for w in correctwords.keys():
moneyString = moneyString.replace(w, correctwords[w])
return moneyString
def num2textBGN(numString):
if '.' in numString:
whole, fract = map(lambda x:int(x), numString.split('.'))
ret = '%s лева и %s стотинки' % tuple(map(num2words, [whole,fract]))
else:
ret = '%s лева' % num2words(numString)
return correct(ret)
if __name__ == '__main__':
import sys
print num2textBGN(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment