from UzMorphAnalyser import UzMorphAnalyser

# Avval obyekt yaratish kerak
analyser = UzMorphAnalyser()
def lemmatize(word):
    # So‘zni lemmatizatsiya qilish
    a=analyser.lemmatize(word)
    return a


def number_to_text(number):
    def minglikgacha(son):
        birliklar = ["", "bir", "ikki", "uch", "to‘rt", "besh", "olti", "yetti", "sakkiz", "to‘qqiz"]
        onliklar = ["", "o‘n", "yigirma", "o‘ttiz", "qirq", "ellik", "oltmish", "yetmish", "sakson", "to‘qson"]
        yuzlik = son // 100
        onlik = (son % 100) // 10
        birlik = son % 10
        natija = []
        if yuzlik > 0:
            natija.append(birliklar[yuzlik])
            natija.append("yuz")
        if onlik > 0:
            natija.append(onliklar[onlik])
        if birlik > 0:
            natija.append(birliklar[birlik])
        return " ".join(natija)

    def numtotex(son):
        if son == 0:
            return "nol"
        darajalar = ["", "ming", "million", "milliard", "trillion", "kvadrillion", "kvintillion", "seksillion", "septillion", "oktillion", "nonillion", "desillion"]
        # 10^36 gacha sanay oladi. Yana davom ettirish mumkin. Muammo bo'lmaydi.
        natija = []
        if son < 0:
            natija.append("minus")
            son = abs(son)
        guruhlar = []
        while son > 0:
            guruhlar.append(son % 1000)
            son //= 1000
        for i in range(len(guruhlar) - 1, -1, -1):
            qiymat = guruhlar[i]
            if qiymat != 0:
                matn = minglikgacha(qiymat)
                if darajalar[i]:
                    natija.append(matn + " " + darajalar[i])
                else:
                    natija.append(matn)
        return " ".join(natija).strip()

    def kasr_uchun(qism):
        uzunlik = len(qism)
        kasr_darajasi = {
            1: "o‘ndan",
            2: "yuzdan",
            3: "mingdan",
            4: "o‘n mingdan",
            5: "yuz mingdan",
            6: "milliondan",
            7: "o‘n milliondan",
            8: "yuz milliondan",
            9: "trilliondan" # 10^-9 gacha sanay oladi, yana davom ettirish mumkin, muammo bo'lmaydi
        }
        if not qism.isdigit():
            return ""
        qism_int = int(qism)
        if qism_int == 0:
            return ""
        kasr_soz = numtotex(qism_int)
        daraja_soz = kasr_darajasi.get(uzunlik, f"10^{uzunlik}dan")
        return f"{daraja_soz} {kasr_soz}"

    if isinstance(number, float) or isinstance(number, int):
        son_str = str(number)
    elif isinstance(number, str):
        son_str = number.replace(",", ".")
    else:
        return "Noto‘g‘ri format"

    if '.' in son_str:
        butun, kasr = son_str.split('.')
        butun_soz = numtotex(int(butun))
        kasr = kasr.rstrip('0')
        if kasr:
            kasr_soz = kasr_uchun(kasr)
            return f"{butun_soz} butun {kasr_soz}".strip()
        else:
            return butun_soz
    else:
        return numtotex(int(son_str))




def text_to_number(text):
    birliklar = {
        "nol": 0, "bir": 1, "ikki": 2, "uch": 3, "to'rt": 4, "to’rt": 4, "besh": 5,
        "olti": 6, "yetti": 7, "sakkiz": 8, "to'qqiz": 9, "to'qqiz": 9
    }
    onliklar = {
        "o'n": 10, "o’n": 10, "yigirma": 20, "o'ttiz": 30, "o’ttiz": 30, "qirq": 40, "ellik": 50,
        "oltmish": 60, "yetmish": 70, "sakson": 80, "to'qson": 90, "to’qson": 90
    }
    darajalar = {
        "yuz": 100, "ming": 1_000, "million": 1_000_000, "milliard": 1_000_000_000,
        "trillion": 1_000_000_000_000, "kvadrillion": 10**15
    }
    kasr_darajalar = {
        "o'ndan": 10, "o’ndan": 10, "yuzdan": 100, "mingdan": 1_000, "o’n mingdan": 10_000, "o'n mingdan": 10_000,
        "yuz mingdan": 100_000, "milliondan": 1_000_000, "o’n milliondan": 10_000_000, "o'n milliondan": 10_000_000,
        "yuz milliondan": 100_000_000, "trilliondan": 1_000_000_000_000
    }

    sozlar = text.lower().split()
    is_negative = False
    if sozlar[0] == "minus":
        is_negative = True
        sozlar = sozlar[1:]

    if "butun" in sozlar:
        idx = sozlar.index("butun")
        butun_qism = sozlar[:idx]
        kasr_qism = sozlar[idx+1:]
    else:
        butun_qism = sozlar
        kasr_qism = []

    def parse_butun(qism):
        summa = 0
        vaqtincha = 0
        for word in qism:
            if word in birliklar:
                vaqtincha += birliklar[word]
            elif word in onliklar:
                vaqtincha += onliklar[word]
            elif word == "yuz":
                if vaqtincha == 0:
                    vaqtincha = 1
                vaqtincha *= 100
            elif word in darajalar:
                if vaqtincha == 0:
                    vaqtincha = 1
                summa += vaqtincha * darajalar[word]
                vaqtincha = 0
        summa += vaqtincha
        return summa

    def parse_kasr(qism):
        if not qism:
            return 0
        if len(qism) >= 2 and f"{qism[0]} {qism[1]}" in kasr_darajalar:
            daraja_soz = f"{qism[0]} {qism[1]}"
            raqam_sozlar = qism[2:]
        else:
            daraja_soz = qism[0]
            raqam_sozlar = qism[1:]
        daraja = kasr_darajalar.get(daraja_soz, None)
        if not daraja:
            return 0
        raqam = parse_butun(raqam_sozlar)
        return raqam / daraja

    butun = parse_butun(butun_qism)
    kasr = parse_kasr(kasr_qism)
    natija = butun + kasr
    if is_negative:
        natija = -natija
    return round(natija, 10)