<reponame>christiansteinert/tibetan-dictionary
#!/usr/bin/env python3

import sys, re

def getAlternativeValue(value):
    value = re.sub(r"\b1\b","one",value)
    value = re.sub(r"\b2\b","two",value)
    value = re.sub(r"\b3\b","three",value)
    value = re.sub(r"\b4\b","four",value)
    value = re.sub(r"\b5\b","five",value)
    value = re.sub(r"\b6\b","six",value)
    value = re.sub(r"\b7\b","seven",value)
    value = re.sub(r"\b8\b","eight",value)
    value = re.sub(r"\b9\b","nine",value)
    value = re.sub(r"\b10\b","ten",value)
    value = re.sub(r"\b11\b","eleven",value)
    value = re.sub(r"\b12\b","twelve",value)
    value = re.sub(r"\b13\b","thirteen",value)
    value = re.sub(r"\b14\b","fourteen",value)
    value = re.sub(r"\b15\b","fifteen",value)
    value = re.sub(r"\b16\b","sixteen",value)
    value = re.sub(r"\b17\b","seventeen",value)
    value = re.sub(r"\b18\b","eighteen",value)
    value = re.sub(r"\b19\b","nineteen",value)
    value = re.sub(r"\b20\b","twenty",value)
    value = re.sub(r"\b21\b","twenty one",value)
    value = re.sub(r"\b22\b","twenty two",value)
    value = re.sub(r"\b23\b","twenty three",value)
    value = re.sub(r"\b24\b","twenty four",value)
    value = re.sub(r"\b25\b","twenty five",value)
    value = re.sub(r"\b26\b","twenty six",value)
    value = re.sub(r"\b27\b","twenty seven",value)
    value = re.sub(r"\b28\b","twenty eight",value)
    value = re.sub(r"\b29\b","twenty nine",value)
    value = re.sub(r"\b30\b","thirty",value)
    value = re.sub(r"\b31\b","thirty one",value)
    value = re.sub(r"\b32\b","thirty two",value)
    value = re.sub(r"\b33\b","thirty three",value)
    value = re.sub(r"\b34\b","thirty four",value)
    value = re.sub(r"\b35\b","thirty five",value)
    value = re.sub(r"\b36\b","thirty six",value)
    value = re.sub(r"\b37\b","thirty seven",value)
    value = re.sub(r"\b38\b","thirty eight",value)
    value = re.sub(r"\b39\b","thirty nine",value)
    value = re.sub(r"\b40\b","fourty",value)
    value = re.sub(r"\b41\b","fourty one",value)
    value = re.sub(r"\b42\b","fourty two",value)
    value = re.sub(r"\b43\b","fourty three",value)
    value = re.sub(r"\b44\b","fourty four",value)
    value = re.sub(r"\b45\b","fourty five",value)
    value = re.sub(r"\b46\b","fourty six",value)
    value = re.sub(r"\b47\b","fourty seven",value)
    value = re.sub(r"\b48\b","fourty eight",value)
    value = re.sub(r"\b49\b","fourty nine",value)
    value = re.sub(r"\b50\b","fifty",value)
    value = re.sub(r"\b51\b","fifty one",value)
    value = re.sub(r"\b52\b","fifty two",value)
    value = re.sub(r"\b53\b","fifty three",value)
    value = re.sub(r"\b54\b","fifty four",value)
    value = re.sub(r"\b55\b","fifty five",value)
    value = re.sub(r"\b56\b","fifty six",value)
    value = re.sub(r"\b57\b","fifty seven",value)
    value = re.sub(r"\b58\b","fifty eight",value)
    value = re.sub(r"\b59\b","fifty nine",value)
    value = re.sub(r"\b60\b","sixty",value)
    value = re.sub(r"\b61\b","sixty one",value)
    value = re.sub(r"\b62\b","sixty two",value)
    value = re.sub(r"\b63\b","sixty three",value)
    value = re.sub(r"\b64\b","sixty four",value)
    value = re.sub(r"\b65\b","sixty five",value)
    value = re.sub(r"\b66\b","sixty six",value)
    value = re.sub(r"\b67\b","sixty seven",value)
    value = re.sub(r"\b68\b","sixty eight",value)
    value = re.sub(r"\b69\b","sixty nine",value)
    value = re.sub(r"\b70\b","seventy",value)
    value = re.sub(r"\b71\b","seventy one",value)
    value = re.sub(r"\b72\b","seventy two",value)
    value = re.sub(r"\b73\b","seventy three",value)
    value = re.sub(r"\b74\b","seventy four",value)
    value = re.sub(r"\b75\b","seventy five",value)
    value = re.sub(r"\b76\b","seventy six",value)
    value = re.sub(r"\b77\b","seventy seven",value)
    value = re.sub(r"\b78\b","seventy eight",value)
    value = re.sub(r"\b79\b","seventy nine",value)
    value = re.sub(r"\b80\b","eighty",value)
    value = re.sub(r"\b81\b","eighty one",value)
    value = re.sub(r"\b82\b","eighty two",value)
    value = re.sub(r"\b83\b","eighty three",value)
    value = re.sub(r"\b84\b","eighty four",value)
    value = re.sub(r"\b85\b","eighty five",value)
    value = re.sub(r"\b86\b","eighty six",value)
    value = re.sub(r"\b87\b","eighty seven",value)
    value = re.sub(r"\b88\b","eighty eight",value)
    value = re.sub(r"\b89\b","eighty nine",value)
    value = re.sub(r"\b90\b","ninety",value)
    value = re.sub(r"\b91\b","ninety one",value)
    value = re.sub(r"\b92\b","ninety two",value)
    value = re.sub(r"\b93\b","ninety three",value)
    value = re.sub(r"\b94\b","ninety four",value)
    value = re.sub(r"\b95\b","ninety five",value)
    value = re.sub(r"\b96\b","ninety six",value)
    value = re.sub(r"\b97\b","ninety seven",value)
    value = re.sub(r"\b98\b","ninety eight",value)
    value = re.sub(r"\b99\b","ninety nine",value)    
    value = re.sub(r"\b100\b","hundred",value)
    value = re.sub(r"\b1000\b","thousand",value)

    if value.startswith("to "):
        value=value[3:]

    return value



def cleanupValueMinimal(value):
    value = value.rstrip("\r\n")
    value = value.rstrip("\r")
    value = value.rstrip("\n")
    value = value.replace("-"," ")
    value = re.sub(r"\s+"," ",value)
    value = re.sub(r"^\s+","",value)
    value = re.sub(r"\s+$","",value)
    value = value.replace("\\n",";")

    return value



def cleanupValue(value):
    value = cleanupValueMinimal(value)
    value = re.sub(r"[:]","",value)
    value = re.sub(r"\{.*?\}","",value)
    value = re.sub(r"\[.*?\]","",value)
    value = re.sub(r"\(.*?\)","",value)
    value = re.sub(r"\"","",value)
    value = re.sub(r"″","",value)
    value = re.sub(r"^\s*as verb\s*:\s*","",value)
    value = re.sub(r"^\s*verb\s*:\s*","",value)
    value = re.sub(r"^\s*as noun\s*:\s*","",value)
    value = re.sub(r"^\s*noun\s*:\s*","",value)
    value = re.sub(r"^\s*as adjective\s*:\s*","",value)
    value = re.sub(r"^\s*adjective\s*:\s*","",value)
    value = re.sub(r"^\s*adj\.\s*:\s*","",value)
    value = re.sub(r"\.\.\.","",value)
    value = value.replace("?","")
    value = value.replace("*","")
    value = value.replace("—"," ")
    value = value.replace("'","")
    value = re.sub(r"^\s+","",value)
    value = re.sub(r"\s+$","",value)

    value = value.replace("ä","a")
    value = value.replace("ö","o")
    value = value.replace("ü","u")
    value = value.replace("Ä","A")
    value = value.replace("Ö","O")
    value = value.replace("Ü","U")
    value = value.replace("ā","A")
    value = value.replace("Ā","A")
    value = value.replace("ī","I")
    value = value.replace("Ī","I")
    value = value.replace("ū","U")
    value = value.replace("Ū","U")
    value = value.replace("ē","e")
    value = value.replace("ō","o")

    value = value.replace("ṛ","R")
    value = value.replace("ṛ","R")
    value = value.replace("ṝ","RR")
    value = value.replace("Ṛ","R")
    value = value.replace("Ṝ","RR")
    value = value.replace("ḷ","lR")
    value = value.replace("ḹ","lRR")
    value = value.replace("Ḷ","lR")
    value = value.replace("Ḹ","lRR")
    value = value.replace("ṃ","M")
    value = value.replace("ḥ","H")
    value = value.replace("ṅ","N")
    value = value.replace("Ṅ","N")
    value = value.replace("ṇ","N")
    value = value.replace("Ṇ","N")
    value = value.replace("ñ","N")
    value = value.replace("Ñ","N")
    value = value.replace("ṭ","T")
    value = value.replace("Ṭ","T")
    value = value.replace("ḍ","D")
    value = value.replace("Ḍ","D")
    value = value.replace("ś","S")
    value = value.replace("Ś","S")
    value = value.replace("ṣ","S")
    value = value.replace("Ṣ","S")

    value = value.replace("­","") # delete zero-width non-joiner

    value = re.sub(r"\s+"," ",value)

    return value


def main():
    with open(sys.argv[1], 'r') as inp:        
        print(sys.argv[1])
        with open(sys.argv[2], 'w') as out:
            for line in inp:
                if ( not line.startswith("#") ) and ( "|" in line ):
                    tibetanOriginal, englishOriginal = line.split("|");
                    tibetan = cleanupValueMinimal(tibetanOriginal)
                    englishTerms = cleanupValue(englishOriginal)
                    englishTermList = englishTerms.split(";")
                    for english in englishTermList:
                        english = cleanupValue(english)
                        if english != "" and tibetan != "" and ( "/" not in english ) and ("." not in english) and ("," not in english) and (")" not in english) and ("(" not in english) and ("=" not in english):
                            #print(r"{0}|{{{1}}}: {2}".format(english,tibetan,englishOriginal))
                            out.write(r"{0}|{{{1}}}: {2}".format(english,tibetan,englishOriginal))
                            english2 = getAlternativeValue(english)
                            if english != english2:
                              out.write(r"{0}|{{{1}}}: {2}".format(english2,tibetan,englishOriginal))



main()

#!/usr/bin/env python3

import sys, re

def getAlternativeValue(value):
    value = re.sub(r"\b1\b","one",value)
    value = re.sub(r"\b2\b","two",value)
    value = re.sub(r"\b3\b","three",value)
    value = re.sub(r"\b4\b","four",value)
    value = re.sub(r"\b5\b","five",value)
    value = re.sub(r"\b6\b","six",value)
    value = re.sub(r"\b7\b","seven",value)
    value = re.sub(r"\b8\b","eight",value)
    value = re.sub(r"\b9\b","nine",value)
    value = re.sub(r"\b10\b","ten",value)
    value = re.sub(r"\b11\b","eleven",value)
    value = re.sub(r"\b12\b","twelve",value)
    value = re.sub(r"\b13\b","thirteen",value)
    value = re.sub(r"\b14\b","fourteen",value)
    value = re.sub(r"\b15\b","fifteen",value)
    value = re.sub(r"\b16\b","sixteen",value)
    value = re.sub(r"\b17\b","seventeen",value)
    value = re.sub(r"\b18\b","eighteen",value)
    value = re.sub(r"\b19\b","nineteen",value)
    value = re.sub(r"\b20\b","twenty",value)
    value = re.sub(r"\b21\b","twenty one",value)
    value = re.sub(r"\b22\b","twenty two",value)
    value = re.sub(r"\b23\b","twenty three",value)
    value = re.sub(r"\b24\b","twenty four",value)
    value = re.sub(r"\b25\b","twenty five",value)
    value = re.sub(r"\b26\b","twenty six",value)
    value = re.sub(r"\b27\b","twenty seven",value)
    value = re.sub(r"\b28\b","twenty eight",value)
    value = re.sub(r"\b29\b","twenty nine",value)
    value = re.sub(r"\b30\b","thirty",value)
    value = re.sub(r"\b31\b","thirty one",value)
    value = re.sub(r"\b32\b","thirty two",value)
    value = re.sub(r"\b33\b","thirty three",value)
    value = re.sub(r"\b34\b","thirty four",value)
    value = re.sub(r"\b35\b","thirty five",value)
    value = re.sub(r"\b36\b","thirty six",value)
    value = re.sub(r"\b37\b","thirty seven",value)
    value = re.sub(r"\b38\b","thirty eight",value)
    value = re.sub(r"\b39\b","thirty nine",value)
    value = re.sub(r"\b40\b","fourty",value)
    value = re.sub(r"\b41\b","fourty one",value)
    value = re.sub(r"\b42\b","fourty two",value)
    value = re.sub(r"\b43\b","fourty three",value)
    value = re.sub(r"\b44\b","fourty four",value)
    value = re.sub(r"\b45\b","fourty five",value)
    value = re.sub(r"\b46\b","fourty six",value)
    value = re.sub(r"\b47\b","fourty seven",value)
    value = re.sub(r"\b48\b","fourty eight",value)
    value = re.sub(r"\b49\b","fourty nine",value)
    value = re.sub(r"\b50\b","fifty",value)
    value = re.sub(r"\b51\b","fifty one",value)
    value = re.sub(r"\b52\b","fifty two",value)
    value = re.sub(r"\b53\b","fifty three",value)
    value = re.sub(r"\b54\b","fifty four",value)
    value = re.sub(r"\b55\b","fifty five",value)
    value = re.sub(r"\b56\b","fifty six",value)
    value = re.sub(r"\b57\b","fifty seven",value)
    value = re.sub(r"\b58\b","fifty eight",value)
    value = re.sub(r"\b59\b","fifty nine",value)
    value = re.sub(r"\b60\b","sixty",value)
    value = re.sub(r"\b61\b","sixty one",value)
    value = re.sub(r"\b62\b","sixty two",value)
    value = re.sub(r"\b63\b","sixty three",value)
    value = re.sub(r"\b64\b","sixty four",value)
    value = re.sub(r"\b65\b","sixty five",value)
    value = re.sub(r"\b66\b","sixty six",value)
    value = re.sub(r"\b67\b","sixty seven",value)
    value = re.sub(r"\b68\b","sixty eight",value)
    value = re.sub(r"\b69\b","sixty nine",value)
    value = re.sub(r"\b70\b","seventy",value)
    value = re.sub(r"\b71\b","seventy one",value)
    value = re.sub(r"\b72\b","seventy two",value)
    value = re.sub(r"\b73\b","seventy three",value)
    value = re.sub(r"\b74\b","seventy four",value)
    value = re.sub(r"\b75\b","seventy five",value)
    value = re.sub(r"\b76\b","seventy six",value)
    value = re.sub(r"\b77\b","seventy seven",value)
    value = re.sub(r"\b78\b","seventy eight",value)
    value = re.sub(r"\b79\b","seventy nine",value)
    value = re.sub(r"\b80\b","eighty",value)
    value = re.sub(r"\b81\b","eighty one",value)
    value = re.sub(r"\b82\b","eighty two",value)
    value = re.sub(r"\b83\b","eighty three",value)
    value = re.sub(r"\b84\b","eighty four",value)
    value = re.sub(r"\b85\b","eighty five",value)
    value = re.sub(r"\b86\b","eighty six",value)
    value = re.sub(r"\b87\b","eighty seven",value)
    value = re.sub(r"\b88\b","eighty eight",value)
    value = re.sub(r"\b89\b","eighty nine",value)
    value = re.sub(r"\b90\b","ninety",value)
    value = re.sub(r"\b91\b","ninety one",value)
    value = re.sub(r"\b92\b","ninety two",value)
    value = re.sub(r"\b93\b","ninety three",value)
    value = re.sub(r"\b94\b","ninety four",value)
    value = re.sub(r"\b95\b","ninety five",value)
    value = re.sub(r"\b96\b","ninety six",value)
    value = re.sub(r"\b97\b","ninety seven",value)
    value = re.sub(r"\b98\b","ninety eight",value)
    value = re.sub(r"\b99\b","ninety nine",value)    
    value = re.sub(r"\b100\b","hundred",value)
    value = re.sub(r"\b1000\b","thousand",value)

    if value.startswith("to "):
        value=value[3:]

    return value



def cleanupValueMinimal(value):
    value = value.rstrip("\r\n")
    value = value.rstrip("\r")
    value = value.rstrip("\n")
    value = value.replace("-"," ")
    value = re.sub(r"\s+"," ",value)
    value = re.sub(r"^\s+","",value)
    value = re.sub(r"\s+$","",value)
    value = value.replace("\\n",";")

    return value



def cleanupValue(value):
    value = cleanupValueMinimal(value)
    value = re.sub(r"[:]","",value)
    value = re.sub(r"\{.*?\}","",value)
    value = re.sub(r"\[.*?\]","",value)
    value = re.sub(r"\(.*?\)","",value)
    value = re.sub(r"\"","",value)
    value = re.sub(r"″","",value)
    value = re.sub(r"^\s*as verb\s*:\s*","",value)
    value = re.sub(r"^\s*verb\s*:\s*","",value)
    value = re.sub(r"^\s*as noun\s*:\s*","",value)
    value = re.sub(r"^\s*noun\s*:\s*","",value)
    value = re.sub(r"^\s*as adjective\s*:\s*","",value)
    value = re.sub(r"^\s*adjective\s*:\s*","",value)
    value = re.sub(r"^\s*adj\.\s*:\s*","",value)
    value = re.sub(r"\.\.\.","",value)
    value = value.replace("?","")
    value = value.replace("*","")
    value = value.replace("—"," ")
    value = value.replace("'","")
    value = re.sub(r"^\s+","",value)
    value = re.sub(r"\s+$","",value)

    value = value.replace("ä","a")
    value = value.replace("ö","o")
    value = value.replace("ü","u")
    value = value.replace("Ä","A")
    value = value.replace("Ö","O")
    value = value.replace("Ü","U")
    value = value.replace("ā","A")
    value = value.replace("Ā","A")
    value = value.replace("ī","I")
    value = value.replace("Ī","I")
    value = value.replace("ū","U")
    value = value.replace("Ū","U")
    value = value.replace("ē","e")
    value = value.replace("ō","o")

    value = value.replace("ṛ","R")
    value = value.replace("ṛ","R")
    value = value.replace("ṝ","RR")
    value = value.replace("Ṛ","R")
    value = value.replace("Ṝ","RR")
    value = value.replace("ḷ","lR")
    value = value.replace("ḹ","lRR")
    value = value.replace("Ḷ","lR")
    value = value.replace("Ḹ","lRR")
    value = value.replace("ṃ","M")
    value = value.replace("ḥ","H")
    value = value.replace("ṅ","N")
    value = value.replace("Ṅ","N")
    value = value.replace("ṇ","N")
    value = value.replace("Ṇ","N")
    value = value.replace("ñ","N")
    value = value.replace("Ñ","N")
    value = value.replace("ṭ","T")
    value = value.replace("Ṭ","T")
    value = value.replace("ḍ","D")
    value = value.replace("Ḍ","D")
    value = value.replace("ś","S")
    value = value.replace("Ś","S")
    value = value.replace("ṣ","S")
    value = value.replace("Ṣ","S")

    value = value.replace("­","") # delete zero-width non-joiner

    value = re.sub(r"\s+"," ",value)

    return value


def main():
    with open(sys.argv[1], 'r') as inp:        
        print(sys.argv[1])
        with open(sys.argv[2], 'w') as out:
            for line in inp:
                if ( not line.startswith("#") ) and ( "|" in line ):
                    tibetanOriginal, englishOriginal = line.split("|");
                    tibetan = cleanupValueMinimal(tibetanOriginal)
                    englishTerms = cleanupValue(englishOriginal)
                    englishTermList = englishTerms.split(";")
                    for english in englishTermList:
                        english = cleanupValue(english)
                        if english != "" and tibetan != "" and ( "/" not in english ) and ("." not in english) and ("," not in english) and (")" not in english) and ("(" not in english) and ("=" not in english):
                            #print(r"{0}|{{{1}}}: {2}".format(english,tibetan,englishOriginal))
                            out.write(r"{0}|{{{1}}}: {2}".format(english,tibetan,englishOriginal))
                            english2 = getAlternativeValue(english)
                            if english != english2:
                              out.write(r"{0}|{{{1}}}: {2}".format(english2,tibetan,englishOriginal))



main()
#!/usr/bin/env python3 # delete zero-width non-joiner #print(r"{0}|{{{1}}}: {2}".format(english,tibetan,englishOriginal))