-2

I'm trying to make a python program that will translate to a custom language from an input that is given. I had it working with a gender system, for example the man is de mno while the woman is di felio. I then moved on to making a system where with the man becomes aile mno instead of ail de mno. I'm not too good with Python, so for me in theory the way I did this should work.

The way I thought it would work is that generally a preposition comes before an article and noun, so if the translator hits a preposition, it checks ahead to the noun to see whether it is female or male and changes based on that (which I successfully made it do). It then adds all the characters to prepo. If it hits 'the', then it checks back to see if there is a preposition before. If there is, the pass function runs so it doesn't add any translation for 'the', but if there isn't it checks ahead for gender and runs as usual.

The main code is in the elif word in Preposition: and elif word == 'the': lines.

The end goal would be to turn The man is with the woman into De mno aili felio instead of De mno ail di felio.

I don't know if the code is actually working because when I run it and type anything with 'the' other than 'the' by itself, the code stops running but I can still type things and enter them. When I end the program it says keyboardInterrupt.

A fix for that problem and a solution for how I would go about doing it would really be appreciated.

import string

NounM = {
    'man': 'mno',
    'rock': 'lehr'
}

NounF = {
    'woman': 'felio',
    'chair': 'poen'
}

Verb = {
    'sit': 'colt',
    'is': 'ne',
    'are': 'sey',
    'was': 'sai'
}

Preposition = {
    'on': 'mit',
    'with': 'ail'
}

Pronoun = {
    'he': 'tse',
    'she': 'se',
    'i': 'ile',
    'me': 'men',
    'they': 'er',
    'it': 'ze'
}

Adjective = {
    'happy': 'kliony',
    'sad': 'probo',
    'good': 'klio',
    'bad': 'pro'
}

Article = {
    'that': 'arei',
    'those': 'sie'
}


def translate():
    sentence = input('Enter the sentence to turn into your custom language! ')
    split = sentence.split()
    translated_list = []
    translated_sentence = ''
    
    for index, word in enumerate(split):
        char = ''
        for a in string.punctuation:
            if str(a) in word:
                char = a
        if word in NounM:
            translated_sentence += NounM[word]
        elif word in NounF:
            translated_sentence += NounF[word]
        elif word in Verb:
            translated_sentence += Verb[word]
        elif word in Preposition:
            translated_sentence += Preposition[word]
            try:
                c = split[index + 1]
                while c not in NounM and c not in NounF:
                    a = 2
                    c = split[index + a]
                    a += 1
                if c in NounM:
                    translated_sentence += 'e'
                elif c in NounF:
                    translated_sentence += 'i'
            except IndexError:
                pass
        elif word in Pronoun:
            translated_sentence += Pronoun[word]
        elif word in Adjective:
            translated_sentence += Adjective[word]
        elif word in Article:
            translated_sentence += Article[word]
        elif word == 'the':
            try:
                c = split[index - 1]
                while c not in Preposition:
                    a = 2
                    c = split[index - a]
                    a += 1
                f = len(c)
                if c[f] == 'e' or c[f] == 'i':
                    pass
            except IndexError:
                c = split[index + 1]
                while c not in NounM and c not in NounF:
                    a = 2
                    c = split[index + a]
                    a += 1
                if c in NounM:
                    translated_sentence += 'de'
                elif c in NounF:
                    translated_sentence += 'di'
                else:
                    pass
        elif word == 'a':
            c = split[index + 1]
            while c not in NounM and c not in NounF:
                a = 2
                c = split[index + a]
                a += 1
            if c in NounM:
                translated_sentence += 'es'
            elif c in NounF:
                translated_sentence += 'en'
            else:
                pass
        else:
            translated_sentence += word
        word += str(char)
        word += ' '
        for i in translated_sentence:
            translated_list += i
        translated_list += str(char)
        translated_list += ' '
        translated_sentence = ''

    leng = len(translated_list) - 2
    final = translated_list[leng]
    if final in string.punctuation:
        translated_list.remove(final)

    translated_sentence = ''
    for i in translated_list:
        translated_sentence += i

    if final in string.punctuation:
        translated_sentence += final

    print(translated_sentence)
    other_translate = input('Would you like to translate another sentence? y/n ')
    if other_translate == 'y':
        translate()


translate()
qwerteee
  • 197
  • 9

1 Answers1

1

The reason for this bug is the code is stuck in a while loop forever. The bug is in here:

while c not in Preposition:
    a = 2
    c = split[index - a]
    a += 1

In case the input sentence doesn't have 'on' or 'with'. This loop will iterate forever.

You should add a break case for this loop, adding these codes for example:

while c not in Preposition:
    a = 2
    c = split[index - a]
    a += 1
    if (index - a) < 0:
        break

Hope it works!

Nora
  • 161
  • 1
  • 6