473,513 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pig Translator in Python

2 New Member
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"):
  2.  
  3.     result = 'ayayayano'
  4.  
  5.     with open(we, 'r') as text:
  6.         # Gets the raw content
  7.         content = text.read().replace(',', '').replace('.', '').replace('?', '').replace('-', '')
  8.  
  9.         # Splits into the lines without \n
  10.         lines = [line.rstrip() for line in content.split("\n")]
  11.  
  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)
  15.  
  16.         # Translates if not
  17.         else:
  18.             result = ' '.join(word[1::] + word[0] + 'ano' for line in lines for word in line.split(' '))
  19.  
  20.     with open(wy, 'w') as output:
  21.         output.write(result)
  22.  
  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 
  2.  
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
1 15784
Eliadoming
3 New Member
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_pig_polish(input_file="in.txt", output_file="out.txt"):
with open(input_file, 'r') as text:
lines = text.readlines()
translated_lines = []

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

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

translated_lines.append(' '.join(translated_words))

result = '\n'.join(translated_lines)

with open(output_file, 'w') as output:
output.write(result)

return result

With this updated code, when you call translate_to_pig_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.
Jun 26 '23 #2

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

Similar topics

2
8401
by: Dan Jacobson | last post by:
An old dog can't learn new tricks, so where's the a2py awk to python translator? Perl has a2p. E.g. today I wonder how to do '{print $1}', well with a2p I know how to do it in perl, but with...
7
17619
by: Bruce Dickey | last post by:
Hi, I have some PHP scripts I'm moving from my web server to the local PC. They just do file manipulation. Anybody know of a tool for translating PHP to Python? A partial translation would be...
0
1499
by: Bruce Dickey | last post by:
Hi, I'm moving some functionality from the server to the local PC and need to translate some PHP scripts to Python. They just do file manipulation. Anybody know of a PHP to Python translator?...
42
4029
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time....
14
2830
by: David MacQuigg | last post by:
I am starting a new thread so we can avoid some of the non-productive argument following my earlier post "What is good about Prothon". At Mr. Hahn's request, I will avoid using the name "Prothon"...
7
3722
by: Eric van Riet Paap | last post by:
Wouldn't it be nice if we could program our browser in Python instead of Javascript? - Eric
852
27942
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
31
2745
by: Mark Dufour | last post by:
Hi all, I have recently released version 0.0.20 and 0.0.21 of Shed Skin, an optimizing Python-to-C++ compiler. Shed Skin allows for translation of pure (unmodified), implicitly statically typed...
34
3637
by: Anthony Irwin | last post by:
Hi All, I am currently trying to decide between using python or java and have a few quick questions about python that you may be able to help with. #1 Does python have something like javas...
1
1503
by: Patrick Mullen | last post by:
(sorry michael, didn't mean to personal post On Wed, Mar 19, 2008 at 9:24 AM, Michael Wieher wrote: No, py2exe basically bundles the main script and the interpreter together so it's easy to...
0
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.