468,473 Members | 1,776 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,473 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1. def translate_to_pig_polish(we = "in.txt", wy = "out.txt"):
  3.     result = 'ayayayano'
  5.     with open(we, 'r') as text:
  6.         # Gets the raw content
  7.         content = text.read().replace(',', '').replace('.', '').replace('?', '').replace('-', '')
  9.         # Splits into the lines without \n
  10.         lines = [line.rstrip() for line in content.split("\n")]
  12.         # Checks if it is already translated
  13.         if all(line[-3::] == 'ano' for line in lines for word in line.split(' ')):
  14.             result = ' '.join(lines)
  16.         # Translates if not
  17.         else:
  18.             result = ' '.join(word[1::] + word[0] + 'ano' for line in lines for word in line.split(' '))
  20.     with open(wy, 'w') as output:
  21.         output.write(result)
  23.     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
Expand|Select|Wrap|Line Numbers
  1. Podano do stołu. Królowały naleśniki.
  2. Z czym można je jeść?
  3. - z jagodami, bananami z serem i rodzynkami,
  4. na słono z szynką, ze szpinakiem...
My output:

Expand|Select|Wrap|Line Numbers
  1. Odanopaano odaano tołu.saano Rólowałykaano aleśniki.naano 
Expected output:

Expand|Select|Wrap|Line Numbers
  1. Odanopaano odaano tołusaano Rólowałykaano aleśnikinaano
  2. zaano zymcaano ożnamaano ejaano eśćjaano
  3. zaano agodamijaano, ananamiaano zaano eremaano iaano odzynkamiraano
  4. anaano łonosaano z zynkąsaano ezaano zpinakiemsaano
May 5 '21 #1
0 1136

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Dan Jacobson | last post: by
7 posts views Thread by Bruce Dickey | last post: by
reply views Thread by Bruce Dickey | last post: by
42 posts views Thread by Fred Ma | last post: by
14 posts views Thread by David MacQuigg | last post: by
7 posts views Thread by Eric van Riet Paap | last post: by
852 posts views Thread by Mark Tarver | last post: by
31 posts views Thread by Mark Dufour | last post: by
34 posts views Thread by Anthony Irwin | last post: by
1 post views Thread by Patrick Mullen | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.