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: - 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 - 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: - Odanopaano odaano tołu.saano Rólowałykaano aleśniki.naano
-
Expected output: - 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
1 15618
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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?...
|
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....
|
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"...
|
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
|
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...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |