Last active
November 23, 2020 14:47
-
-
Save blgnksy/2c744af9068c04bb2a0a40055f63acfb to your computer and use it in GitHub Desktop.
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
''' | |
Adopted from https://stackoverflow.com/questions/19504350/how-to-convert-numbers-to-words-without-using-num2word-library for Turkish | |
''' | |
import re | |
ones = { | |
0: '', 1: 'bir', 2: 'iki', 3: 'üç', 4: 'dört', 5: 'beş', 6: 'altı', | |
7: 'yedi', 8: 'sekiz', 9: 'dokuz', 10: 'on', 11: 'onbir', 12: 'oniki', | |
13: 'onüç', 14: 'ondört', 15: 'onbeş', 16: 'onaltı', | |
17: 'onyedi', 18: 'onsekiz', 19: 'ondokuz'} | |
tens = { | |
2: 'yirmi', 3: 'otuz', 4: 'kırk', 5: 'elli', 6: 'atmış', | |
7: 'yetmiş', 8: 'seksen', 9: 'doksan'} | |
illions = { | |
1: 'bin', 2: 'milyon', 3: 'milyar', 4: 'trilyon', 5: 'katrilyon'} | |
def say_number(i): | |
""" | |
Convert an integer in to it's word representation. | |
say_number(i: integer) -> string | |
""" | |
if i < 0: | |
return _join('eksi', _say_number_pos(-i)) | |
if i == 0: | |
return 'sıfır' | |
text = _say_number_pos(i) | |
text = re.sub('bir yüz', 'yüz', text) | |
text = re.sub('^bir bin', 'bin', text) | |
return text | |
def _say_number_pos(i): | |
if i < 20: | |
return ones[i] | |
if i < 100: | |
return _join(tens[i // 10], ones[i % 10]) | |
if i < 1000: | |
return _divide(i, 100, 'yüz') | |
for illions_number, illions_name in illions.items(): | |
if i < 1000**(illions_number + 1): | |
break | |
return _divide(i, 1000**illions_number, illions_name) | |
def _divide(dividend, divisor, magnitude): | |
return _join( | |
_say_number_pos(dividend // divisor), | |
magnitude, | |
_say_number_pos(dividend % divisor), | |
) | |
def _join(*args): | |
return ' '.join(filter(bool, args)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment