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
- 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
Expand|Select|Wrap|Line Numbers
- 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...
Expand|Select|Wrap|Line Numbers
- Odanopaano odaano tołu.saano Rólowałykaano aleśniki.naano
Expand|Select|Wrap|Line Numbers
- 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