Home > Software design >  How to translate String to another connected String in Python, Need solution
How to translate String to another connected String in Python, Need solution

Time:12-15

I want to create a program that will for example if file contains word "sun" translate to "nuclear fusion", to translate word "Mars" to word "Earth" and similar.

import time
import os
import sys
import pyperclip
import numpy as np


translator = {
    'E' :  'L' ,
    'H' :  'O' ,
    'L' : 'S' ,
    'O' :  'A' ,
    'HELLO' : 'Salut',
}

def a():
    f_path = input("File: ")
    f = open(f_path, 'r')
    lines = f.read()
    converted_data = ""

    for i in range(0, len(lines)):
        if lines[i] in translator.keys():
           converted_data  = translator[lines[i]]
        else:
           converted_data  = lines[i]
    print(converted_data)

a()

This is a part of the code, because other part doesn't have anything to do with this.When I run it, and add a file that contains word "HELLO", I get the output "OLSSA", not the word "Salut", I know that I used dict.keys() in this code and that it only uses keys, but I need it to translate whole sentences because my program isn't using one key, it's using sentences, how can I make it work with whole sentences?

CodePudding user response:

Your program translates your message character-by-character because that's what you do when you iterate over the string like that and do lines[i].

Instead, if you wanted to translate each word in your message, and fall back to translating character-by-character if the word doesn't exist, you will need to split the message into words instead of iterating over each character.

def translate_file(f_path, translator):
    translated_lines = []
    with open(f_path) as f:
        for line in f:
            t_line = []
            words = line.strip().split() # Split line into words
            # Iterate over each word
            for word in words:
                #        Try getting word from translator
                t_word = translator.get(word, 
                             translate_word(word, translator)) # Use this default if word not found in translator

                t_line.append(t_word)) 

            translated_lines.append(" ".join(t_line)) # # Join words in line by spaces, append to list of lines

    return "\n".join(translated_lines) # Join lines by \n, return result

def translate_word(word, translator):
    # Translate word character-by-character
    return "".join(translator.get(c, c) for c in word)

Now, if we tried calling this code with a test file:

HELLO WORLD 
HELO LOHE HEOL HELLO HOLE
translator = translator = {
    'E' :  'L' ,
    'H' :  'O' ,
    'L' : 'S' ,
    'O' :  'A' ,
    'HELLO' : 'Salut',
}

result = translate_file("test.txt", translator)
print(result)
# Salut WARSD
# OLSA SAOL OLAS Salut OASL

CodePudding user response:

I think he wanted to just replace the words in the File and other words to be the same and for it to be saved in File again.

CodePudding user response:

I think he wanted to translate words in the File and the result to be translated file. What if the words were connected, for example "HELLO(S" like translating the program script, so the translation of this program should be for example "Salut(S". Need answer.

  • Related