Thursday, June 23, 2016

convert number to word in khmer

save script below convert-number-to-word.py

#!/usr/bin/python
import re
import sys
def pos1(num):
    zero = u'\u179f\u17bc\u1793\u17d2\u1799'
    one = u'\u1798\u17bd\u1799'
    two = u'\u1796\u17b8'
    three = u'\u1794\u17b8'
    four = u'\u1794\u17bd\u1793'
    five = u'\u1794\u17d2\u179a\u17b6\u17c6'
    six = u'\u1794\u17d2\u179a\u17b6\u17c6\u1798\u17bd\u1799'
    seven = u'\u1794\u17d2\u179a\u17b6\u17c6\u1796\u17b8'
    eight = u'\u1794\u17d2\u179a\u17b6\u17c6\u1794\u17b8'
    nine = u'\u1794\u17d2\u179a\u17b6\u17c6\u1794\u17bd\u1793'
    if num == 0:
        return zero
    elif num == 1:
        return one
    elif num == 2:
        return two
    elif num == 3:
        return three
    elif num == 4:
        return four
    elif num == 5:
        return five
    elif num == 6:
        return six
    elif num == 7:
        return seven
    elif num == 8:
        return eight
    elif num == 9:
        return nine
def pos2(num):
    zero = u'\u179f\u17bc\u1793\u17d2\u1799'
    ten = u'\u178a\u1794\u17cb'
    twenty = u'\u1798\u17d2\u1797\u17c3'
    thirty = u'\u179f\u17b6\u1798\u179f\u17b7\u1794'
    fourty = u'\u179f\u17c2\u179f\u17b7\u1794'
    fifty = u'\u17a0\u17b6\u179f\u17b7\u1794'
    sixty = u'\u17a0\u17bb\u1780\u179f\u17b7\u1794'
    seventy =u'\u1785\u17b7\u178f\u179f\u17b7\u1794'
    eighty =u'\u1794\u17c2\u178f\u179f\u17b7\u1794'
    ninety =u'\u1780\u17c5\u179f\u17b7\u1794'
    if num == 0:
        return zero + ten
    elif num == 1:
        return ten
    elif num == 2:
        return twenty
    elif num == 3:
        return thirty
    elif num == 4:
        return fourty
    elif num == 5:
        return fifty
    elif num == 6:
        return sixty
    elif num == 7:
        return seventy
    elif num == 8:
        return eighty
    elif num == 9:
        return ninety
def pos3(num):
    hundred =u'\u179a\u1799'
    ncall = pos1(num)
    return ncall + hundred
def pos4(num):
    thousand =u'\u1796\u17b6\u1793\u17cb'
    ncall = pos1(num)
    return ncall + thousand
def pos5(num):
    tenthousand =u'\u1798\u17c9\u17b8\u1793'
    ncall = pos1(num)
    return ncall + tenthousand
def pos6(num):
    hundredthousand =u'\u179f\u17c2\u1793'
    ncall = pos1(num)
    return ncall + hundredthousand
def pos7(num):
    million = u'\u179b\u17b6\u1793'
    ncall = pos1(num)
    return ncall + million
   

def numtoword(num):
    snum = str(num)
    n = 1
    nc = 0
    if len(snum) <= 7:
        for i in snum[::-1]:
            uo = "u = pos"+str(n)+"(" + str(i) + ")"
            exec(uo)
            yield u
            n += 1
            if n == 8:
                n= 1
    else:
        for i in snum[::-1]:
            nc += 1
            million = u'\u179b\u17b6\u1793'
            uo = "u = pos"+str(n)+"(" + str(i) + ")"
            exec(uo)
            yield u
            if n == 6 and nc != len(snum):
                yield u'\u179b\u17b6\u1793'
            n += 1
            if n == 7:
                n= 1
           
def facilitate(num):
    jot = u'\u1780\u17d2\u1794\u17c0\u179f'
    if type(num) == int:
        nword = numtoword(num)
        proceedzero = re.search("0+$", str(num))
        relist = []
        for n in nword:
            relist.append(n)
        relist = relist[::-1]
        if proceedzero:
            return " ".join(relist[:-len(proceedzero.group(0))])
        else:
            return " ".join(relist)
              
           
    elif type(num) == float:
       
        snum = str(num).split(".")
        pnum = numtoword(snum[0])
       
       
        pproceedzero = re.search("0+$", str(snum[0]))
        relist1 = []
        for n in pnum:
            relist1.append(n)
        relist1 = relist1[::-1]
        if pproceedzero:
            presult = " ".join(relist1[:-len(pproceedzero.group(0))])
        else:
            presult = " ".join(relist1)
       
        nproceedzero = re.search("0+$", str(snum[1]))
        relist2 = []
        nnum = numtoword(snum[1])
        for n2 in nnum:
            relist2.append(n2)
        relist2 = relist2[::-1]
        if nproceedzero:
            nresult = " ".join(relist2[:-len(nproceedzero.group(0))])
        else:
            nresult = " ".join(relist2)
           
        return presult + " " + jot + " " + nresult
       
       
    else:
        print "please input valid number"
       

def main():
    if len(sys.argv) == 2:
        num = sys.argv[1]
        numword = facilitate(eval(num))
        print numword
    else:
        print "please provide only one numer"

if __name__ == "__main__":
    main()






--------------------------------
usage:

1- 
$ python convert-number-to-word.py11110567044252525
មួយម៉ីន មួយពាន់ មួយរយ ដប់ សូន្យ លាន ប្រាំសែន ប្រាំមួយម៉ីន ប្រាំពីពាន់ សូន្យរយ សែសិប បួន លាន ពីសែន ប្រាំម៉ីន ពីពាន់ ប្រាំរយ ម្ភៃ ប្រាំ



2-
$ python test.py 15205.56
មួយម៉ីន ប្រាំពាន់ ពីរយ សូន្យដប់ ប្រាំ ក្បៀស ហាសិប ប្រាំមួយ


3-
$ python test.py 15000.500
មួយម៉ីន ប្រាំពាន់ ក្បៀស ប្រាំ

No comments:

Post a Comment