Pig Translator in Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kermitthefrogpy
    New Member
    • May 2021
    • 2

    Pig Translator in Python

    Good morning everyone.

    So here we go with the task.

    Write a function that translates some text into Pig Polish and vice versa.

    The Polish language is translated into Pig Polish by taking the first letter of each word, moving it to the end of the word and adding ano.

    'Ala ma kota' becomes ['Laaaano amaano otakaano'].

    The function should

    detect whether the text is translated or not
    read text from a file (default 'input = in.txt')
    save the translation to a file (default output = 'out.txt')
    translate only words, omitting punctuation marks


    My code:

    Code:
    def translate_to_pig_polish(we = "in.txt", wy = "out.txt"):
       
        result = 'ayayayano'
    
        with open(we, 'r') as text:
            # Gets the raw content
            content = text.read().replace(',', '').replace('.', '').replace('?', '').replace('-', '')
            
            # Splits into the lines without \n
            lines = [line.rstrip() for line in content.split("\n")]
            
            # Checks if it is already translated
            if all(line[-3::] == 'ano' for line in lines for word in line.split(' ')):
                result = ' '.join(lines)
            
            # Translates if not
            else:
                result = ' '.join(word[1::] + word[0] + 'ano' for line in lines for word in line.split(' '))
        
        with open(wy, 'w') as output:
            output.write(result)
            
        return result

    It's working on just short text, or first line of the in.txt but i have to work on this quote:
    Let's say it is in file in.txt
    Code:
    Podano do stołu. Królowały naleśniki.
    Z czym można je jeść?
    - z jagodami, bananami z serem i rodzynkami,
    na słono z szynką, ze szpinakiem...
    My output:

    Code:
    Odanopaano odaano tołu.saano Rólowałykaano aleśniki.naano
    Expected output:

    Code:
    Odanopaano odaano tołusaano Rólowałykaano aleśnikinaano
    zaano zymcaano ożnamaano ejaano eśćjaano
    zaano agodamijaano, ananamiaano zaano eremaano iaano odzynkamiraano
    anaano łonosaano z zynkąsaano ezaano zpinakiemsaano
  • Eliadoming
    New Member
    • May 2021
    • 3

    #2
    The issue with your current code is that you are applying the translation operation to each individual word, rather than preserving the structure of the original text. To achieve the expected output, you need to modify your code to preserve the line and word structure.

    Here's an updated version of your code that should give you the desired output:
    def translate_to_pi g_polish(input_ file="in.txt", output_file="ou t.txt"):
    with open(input_file , 'r') as text:
    lines = text.readlines( )
    translated_line s = []

    for line in lines:
    translated_word s = []
    words = line.split()

    for word in words:
    # Check if the word ends with 'ano' (already translated)
    if word[-3:] == 'ano':
    translated_word s.append(word)
    else:
    translated_word = word[1:] + word[0] + 'ano'
    translated_word s.append(transl ated_word)

    translated_line s.append(' '.join(translat ed_words))

    result = '\n'.join(trans lated_lines)

    with open(output_fil e, 'w') as output:
    output.write(re sult)

    return result

    With this updated code, when you call translate_to_pi g_polish(), it will read the input text from the specified file, translate the text into Pig Polish, and save the translation to the specified output file. The translated text will preserve the line and word structure.

    Comment

    Working...