Habesha / app.py
hopegeb's picture
Update app.py
c3be3fb verified
def amharic_number_reader(number):
nums = []
val = list(number) # Convert the number string to a list of digits
readNums1 = ["አንድ", "ሁለት", "ሶስት", "አራት", "አምስት", "ስድስት", "ሰባት", "ስምንት", "ዘጠኝ", "ዜሮ"]
readNums2 = ["አስራ", "ሃያ", "ሰላሳ", "አርባ", "ሃምሳ", "ስልሳ", "ሰባ", "ሰማንያ", "ዘጠና"]
readNums3 = ["", "መቶ", "ሺ", "አስር ሺ", "መቶ ሺ", "ሚልዮን", "አስር ሚልዮን", "ቢልዮን", "አስር ቢልዮን", "ትሪልዮን"]
group_count = len(val)
for i, digit in enumerate(val):
digit = int(digit)
position = group_count - i - 1 # Position from the right
if position == 12: # Trillions
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[9]) # 'ትሪልዮን'
elif position == 11: # Hundreds of trillions
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[1]) # 'መቶ'
elif position == 10: # Tens of trillions
if digit != 0:
nums.append(readNums2[digit - 1])
elif position == 9: # Billions
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[7]) # 'ቢልዮን'
elif position == 8: # Hundreds of billions
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[1]) # 'መቶ'
elif position == 7: # Tens of billions
if digit != 0:
nums.append(readNums2[digit - 1])
elif position == 6: # Millions
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[5]) # 'ሚልዮን'
elif position == 5: # Hundreds of thousands
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[1]) # 'መቶ'
elif position == 4: # Tens of thousands
if digit != 0:
nums.append(readNums2[digit - 1])
elif position == 3: # Thousands
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[2]) # 'ሺ'
elif position == 2: # Hundreds
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[1]) # 'መቶ'
elif position == 1: # Tens
if digit != 0:
nums.append(readNums2[digit - 1])
elif position == 0: # Ones
if digit != 0:
nums.append(readNums1[digit - 1])
return " ".join(nums) # Return as a sentence
def tigrigna_number_reader(number):
nums = []
val = list(number) # Convert the number string to a list of digits
readNums1 = ["ሓደ", "ክልተ", "ሰለስተ", "ኣርባዕተ", "ሓሙሽተ", "ሽዱሽተ", "ሸዋዓተ", "ሸሞንተ", "ትሽዓተ", "ባዶ"]
readNums2 = ["ዓሰርተ", "ዕስራ", "ሰላሳ", "ዓርባዓ", "ሓምሳ", "ስልሳ", "ሰብዓ", "ሰማንያ", "ቴስዓ"]
readNums3 = ["", "ምእቲ", "ሽሕ", "ዓስርተ ሽሕ", "መቶ ሺሕ", "ሚልዮን", "ዓስርተ ሚልዮን", "ቢልዮን", "ዓስርተ ቢልዮን", "ትሪልዮን"]
group_count = len(val)
for i, digit in enumerate(val):
digit = int(digit)
position = group_count - i - 1 # Position from the right
if position == 12: # Trillions
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[9]) # 'ትሪልዮን'
elif position == 11: # Hundreds of trillions
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[1]) # 'መቶ'
elif position == 10: # Tens of trillions
if digit != 0:
nums.append(readNums2[digit - 1])
elif position == 9: # Billions
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[7]) # 'ቢልዮን'
elif position == 8: # Hundreds of billions
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[1]) # 'መቶ'
elif position == 7: # Tens of billions
if digit != 0:
nums.append(readNums2[digit - 1])
elif position == 6: # Millions
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[5]) # 'ሚልዮን'
elif position == 5: # Hundreds of thousands
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[1]) # 'መቶ'
elif position == 4: # Tens of thousands
if digit != 0:
nums.append(readNums2[digit - 1])
elif position == 3: # Thousands
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[2]) # 'ሺ'
elif position == 2: # Hundreds
if digit != 0:
nums.append(readNums1[digit - 1])
nums.append(readNums3[1]) # 'መቶ'
elif position == 1: # Tens
if digit != 0:
nums.append(readNums2[digit - 1])
elif position == 0: # Ones
if digit != 0:
nums.append(readNums1[digit - 1])
return " ".join(nums) # Return as a sentence
# Example Outputs
print(amharic_number_reader("1221"))
#ሓደ ሽሕን ክልተ ምእቲን ዕስራን ሓደን
print(amharic_number_reader("221222345681"))
#ክልተ ምእቲ ዕስራ ሓደ ቢልዮንን ክልተ ምእቲ ዕስራ ክልተ ሚልዮንን ሰለስተ ምእቲ ዓርባዓ ሓሙሽተ ሽሕን ሽዱሽተ ምእቲን ሰማንያን ሓደን
print(amharic_number_reader("8221222345681"))
#ሸሞንተ ትሪልዮን ክልተ ምእቲ ዕስራ ሓደ ቢልዮን ክልተ ምእቲ ዕስራ ክልተ ሚልዮን ሰለስተ ምእቲ ዓርባዓ ሓሙሽተ ሽሕ ሽዱሽተ ምእቲ ሰማንያ ሓደ