0

Hi I'm new to learning python and I have a project that requires me to translate a document containing a chapter passage into a 'meme' language using a dictionary provided. I'm having an issue getting my code to translate. I don't think I'm quite understanding the syntax needed to make the code work.

Step 1: import text file

file = open("english.txt", "r")
eng = file.readlines()
form_eng = ''.join(eng)

Step 2: import glossary

import json

with open('tranzlashun.json', 'r') as t:
    lol = json.load(t) 

So my text document is form_eng and my dictionary is lol

Step 3: Translate the English text into Lolspeak (lol dictionary) Here is the code I'm having a problem with

lol_translation = []

for word, new_word in lol.items():
    tran = form_eng.replace(word, new_word.lower())
    lol_translation.append(tran)

print(lol_translation)

When I run this, I get the original text document back with all of the letter 'a's missing because 'a' is the first key in my dictionary.

I want to understand the what my code is doing and why and how to fix it.

  • You may try to print out "word" and "new_word" to see what did "lol.items()" returned each time. – Aymen Oct 04 '22 at 12:41

1 Answers1

1

First, we lack information on your json file.

From my understanding, the first item of your dictionary is 'a':'' and you end up with an array of different translated versions of your initial text, where each version has only 1 word replaced based on your dictionary.

To apply all your items of the dictionary in your final translation, your last block of code should look like this:

lol_translation = form_eng

for word, new_word in lol.items():
    lol_translation = lol_translation.replace(word, new_word.lower())

print(lol_translation)

so that the translated version lol_translation is updated while iterating your dictionary.

Also, while wondering if there is a faster way instead of chaining .replace, I found this, which suggests sticking with this .replace chain.

stevp
  • 76
  • 4
  • omg thank you so much, I've been to figure out the correct syntax for this. i ran the code and it did translate! however the lower function didn't work for all of the text in the file so i had to change the case in the file import. thank you a bunch! – Yvette Cheesman Oct 04 '22 at 15:45
  • i ran my code through a sanity check and i failed because all of the a's in my text are being removed since { a : " "} is the first letter key in my dictionary. for some reason its not recognizing words, its recognizing the characters instead – Yvette Cheesman Oct 05 '22 at 12:39
  • You may need to use {" a ": " "} (added space before and after 'a') to capture the word 'a' instead of the letter 'a'. – stevp Oct 05 '22 at 12:45
  • i'm running it through my dictionary so stand alone a's are supposed to be removed {"a": " "}, not all a's. i've been trying to find ways to use the replace method and define what a 'word' as character strings between two spaces ( " 'word' ") and i'm not finding any answers. i was also told that my original code, my variable is being reassigned each time and not keeping my changes to the words. – Yvette Cheesman Oct 05 '22 at 15:05
  • By using the code in my answer, the variable is getting updated instead of reassigned. Regarding the stand alone a's, surround the 'a' in the dictionary key with spaces to remove only the word 'a' and not the letters: {" a ": " "}. In that case, the 3 characters " a " (space, character a, space) will be replaced by a single space. – stevp Oct 05 '22 at 15:21
  • i don't believe i'm allowed to change the dictionary because the file belongs to the company who i created the program i'm in. i'm really sorry that im not grasping these concepts but thank you for helping. – Yvette Cheesman Oct 05 '22 at 15:29
  • If the same this applies for all keys in your dictionary, you could use this format (add a space before and after the dictionary key and value: lol_translation = lol_translation.replace(' '+ word + ' ', ' '+new_word.lower()+' ') – stevp Oct 05 '22 at 16:07
  • okay! that helped a bunch. some of the text still doesn't translate and there's a big indentation throughout the text. i'm not sure if its a problem with the 'word' not being declared or how i imported the text. – Yvette Cheesman Oct 05 '22 at 17:23
  • If you could share some values of your dictionary, a sample input text and its expected output, that would help a bunch. – stevp Oct 06 '22 at 12:39