<filename>src/latinum.py
from difflib import SequenceMatcher

def latin_symbols():
    return {
        'ā': 'a',
        'ē': 'e',
        'ī': 'i',
        'ō': 'o',
        'ū': 'u',
    }

def to_normal(text: str):
    """Converts āēīōū into aeiou"""
    for key in latin_symbols():
        text = text.replace(key, latin_symbols()[key])

    return text

def load():
    dictionary = [] # ironically, the dictionary isn't a dict lmao

    la = open('data/la_raw.txt', encoding='utf-8', errors='replace').readlines()
    gr = open('data/gr_raw.txt', encoding='utf-8', errors='replace').readlines()
    de = open('data/de_raw.txt', encoding='utf-8', errors='replace').readlines()
    en = open('data/en_raw.txt', encoding='utf-8', errors='replace').readlines()

    l = 0
    for line in la:
        dictionary.append([la[l].strip(), gr[l].strip(), de[l].strip(), en[l].strip()])

        l += 1
    return dictionary

def translate_word(text: str, from_lang:str, to_lang: str):
    for meaning in load():
        if from_lang == 'la':
            word = meaning[0]
        if from_lang == 'gr':
            word = meaning[1]
        if from_lang == 'de':
            word = meaning[2]
        if from_lang == 'en':
            word = meaning[3]

        wd = 0
        for word in word.split(', '):
            try:
                if len(text) <= 4:
                    raise Exception
                a = to_normal(text.lower())[:-2]
                b = to_normal(word.lower())[:-2]
            except:
                a = to_normal(text.lower())
                b = to_normal(word.lower())

            ratio = SequenceMatcher(None, a, b).ratio()

            # print(a, b, ratio)
            condition = ratio >= 0.88

            if condition:
                #print(word, meaning)
                if to_lang == 'la':
                    text = meaning[0]
                if to_lang == 'gr':
                    text = meaning[1]
                if to_lang == 'de':
                    text = meaning[2]
                if to_lang == 'en':
                    text = meaning[3]
                return text if text else ''
    return text

def translate_text(text: str, from_lang: str, to_lang: str):
    translated = ''
    for word in text.split():
        translated += translate_word(word, from_lang=from_lang, to_lang=to_lang) + ' '
    return translated

if __name__ == '__main__':
    print(translate_text('Ich hoffe, das funktioniert jetzt, denn ich habe sehr viel Zeit in dieses Programm gesteckt.', 'de', 'la'))