Here I am using askopenfile to upload a (text)file, I also wanted to display the file name next to the upload button and then I want to read its contents and convert it to another language. Say for example if the document is in English I want to convert it into some other language, I had already created a dictionary for it so I need to tell the compiler to compare both the Input file (.txt) and the dictionary values to replace the text file with the dictionary values. So after the user clicks the convert button the text in the (.txt) must be converted into the other language and the same must be saved automatically. Is this possible to do?

Recommended Answers

All 15 Replies

For starters you will have to load/open and read the file Post back when you have this coded (whether it works or not) with a sample of input and the output you would like.

@woooee
I'm trying to build it as a gui application so I am supposed to get the file names as an input and need to display the file's name which the user'd uploaded. Say for example if the user has uploaded a file called 'sample.txt', Next to the upload button the file name ('sample.txt') must be displayed.

Oh boy,
This may be a long winded post.

First off, I would love to know a little more about you personally. Such as: how long have you been programming; how long in python; is this program for school, personal, or other; what are your goals for this program? Of course you don't have to answer these, they just may help in my answering as well as getting to know a fellow Daniweber a little better. :)

First thing I would do is write out the steps. For this program there are very few general steps:
1) Upload file
2) Convert file
3) Save file

Pretty simple.

My next piece of advice is to work on the back-end code first. That is, write the code that will open the file, read it, convert it and save it first. The GUI is really only for ease-of-use for the user, what really makes the program work is the important part. After that you can make it pretty. :)

Another thing I would say about your particular program that I think will be difficult is converting the file to another language. Some things won't be too hard, like (English to Spanish): Hello -> Hola. But other things can get complicated. For instance, in English we simply say "you". But in Spanish if you are talking to someone you want to show respect to (someone you don't know; a professor; an elderly person; etc.) you say "usted", but if you know them (a classmate; friend; etc) you would say "tu", and that kind of thing extends to other words (we, they, etc.). So, another good question is do you know another language or have someone working with you who does? It will be key to the translation aspect of your program.

My last piece of advice is this: the Daniweb community is big on doing your own work. We (as a community) are great at answering questions and helping out on anything you are confused or stumped on, but we like to see your code, your research, and your specific problem/question(s). I know of many people who will not respond if they do not see you really try at it and have (at least some, even non-working) code to show. We're not trying to be jerks or picky, we just know that you don't learn programming by having someone else do all your thinking for you. Programming is nothing more than a tool to express your thoughts and ideas.

Sorry for such a long post, I can get carried away sometimes. :) I hope at least some of this has been helpful and/or informative for you (if you've read down this far). If you post up some code (or need some help getting started with the code) and a question we will help you out as much as we can. Best of luck friend,

  • WolfShield

@WolfShield
Yeah sure thing, I've been working on this even I've coded it but little bit confused on it, first I'd attempted to do a console application for translating an input text from English to German language, for that I'd used dic. function for it and it's working fine, but my reviewers want me to build a GUI for it. If you want me to post my code I'll surely do.

Okay,
If you want help on the GUI, I use wxPython (based on wxWidgets), so that's most of what I know GUI-wise. But there are other people here who can help you with Tkinter (I assume you're using based on original post).

If you have a specific question, just post up the code you have so far and your question.

Sincerely,

  • WolfShield

Yes I have a specific question now . First I am choosing a file using askopenfiles. then I have a label button to display the choosen file in the label, if the user did not choose any file then the label should show 'No files were updated'.

import Tkinter as tk
from tkFileDialog import askopenfilename
import os
class En2De(tk.Frame):
    def __init__(self,master=None):
        tk.Frame.__init__(self,master)
        self.grid()
        self.createWidgets()
    def createWidgets(self):
        self.quitButton = tk.Button(self, text='Quit',command=self.quit)
        self.UploadButton = tk.Button(self, text='UPLOAD FILES HERE',command= self.uploadButton)
        self.Label1=tk.Label(self)
        self.Label2 = tk.Label(self,text='Please Select a language:')
        optionlist = ('--Select--','Afrikaans','Albanian','English','French','German','Hindi','Tamil','Telugu')
        self.var=tk.StringVar()
        self.var.set(optionlist[0])
        self.om=tk.OptionMenu(self,self.var,*optionlist)#om=optionmenu
        #self.ConvertButton=tk.button(self, text'Convert Files',command=self.convertButton)
        #Registering it to the window
        self.quitButton.grid(sticky=tk.NE)
        self.UploadButton.grid()
        self.Label1.grid()
        self.Label2.grid()
        self.om.grid()
    def uploadButton(self):
        filename = askopenfilename(filetypes=(("Template Files","*.tplate"),("Portable Document File","*.pdf"),("Text File","*.txt"),("Word Document","*.docx"),("Word 97-2003 Document","*.doc"),("All Files","*.*")))

Translater = En2De()
Translater.master.title('A Translater for Translating Documents From English to German')
Translater.mainloop()

You set filename at line 26, but you are not using it for anything.

All widgets have config options, config options for the Label Click Here or Click Here Your code modified to show some other techniques.

import Tkinter as tk
from tkFileDialog import askopenfilename
import os

class En2De():
    def __init__(self):
        self.master=tk.Tk()
        self.master.geometry("+300+10")
        self.master.title('A Translater for Translating Documents From English to German')
        self.createWidgets()
        self.master.mainloop()

    def createWidgets(self):
        quitButton = tk.Button(self.master, text='Quit',
                          command=self.master.quit, bg="lightblue",
                          width=10)
        UploadButton = tk.Button(self.master,
                           text='UPLOAD FILES HERE',
                           command= self.uploadButton,
                           width=20)
        self.Label1=tk.Label(self.master)
        self.Label2 = tk.Label(self.master,text='Please Select a language:')
        self.optionlist = ('--Select--','Afrikaans','Albanian','English','French','German','Hindi','Tamil','Telugu')
        self.var=tk.StringVar()
        self.var.set(self.optionlist[0])
        self.om=tk.OptionMenu(self.master, self.var,*self.optionlist)#om=optionmenu
        #self.ConvertButton=tk.button(self, text'Convert Files',command=self.convertButton)
        #Registering it to the window
        quitButton.grid(row=4, sticky=tk.NE)
        UploadButton.grid(row=2)
        self.Label1.grid(row=3)
        self.Label2.grid(row=0)
        self.om.grid(row=1)


    def display_message(self, msg):

        def update_counter():
            lb.configure(text="%s\n %d" % (msg, self.ctr))
            self.ctr -= 1
            if self.ctr > -1:
                lb.after(1000, update_counter)
            else:
                tlv.destroy()

        tlv=tk.Toplevel(self.master, takefocus=True)
        tlv.geometry("+320+50")
        lb=tk.Label(tlv, bg="yellow")
        lb.grid()
        self.ctr = 5
        update_counter()

    def uploadButton(self):
        filename = askopenfilename(filetypes=(("Template Files","*.tplate"),("Portable Document File","*.pdf"),("Text File","*.txt"),("Word Document","*.docx"),("Word 97-2003 Document","*.doc"),("All Files","*.*")))
        if filename:
            option = self.var.get()
            if option != self.optionlist[0]:
                self.Label1.config(text=filename,
                                    fg="red", bg="white")
            else:
                self.display_message("First select a language")

Translater = En2De()

I have changed the line no 12 of my code to the following

self.Label1=tk.Label(self, text='*No files were uploaded!!!',fg='red')

and I have added the following code to the def uploadButton method

def uploadButton(self):
        filename = askopenfilename(filetypes=(("Template Files","*.tplate"),("Portable Document File","*.pdf"),("Text File","*.txt"),("Word Document","*.docx"),("Word 97-2003 Document","*.doc"),("All Files","*.*")))
        #self.Label1.config(text=filename, fg="blue") is used to display filename along with path
        self.Label1.config(text=os.path.basename(filename), fg="blue")

Is this a right way to code ?

@woooee
It's me who posted there also, sorry for wasting your time. I'll remove it now.

@woooee
I had tried this, but I could not get the exact translation out of it.

import re
import os
f= open('.\Sample SRS doc\SRS.txt','r')
d=f.read()
#print d
directory = ".\Sample SRS doc\Converted files"
if not os.path.exists(directory):
    os.mkdir(directory)
#os.mkdir(directory)
con=open('.\Sample SRS doc\Converted files\Germansrs.txt','w')
v=f
c=str(d)
dic_word= {
    #A Words
    #'a':'ein',
    'an':'eine',
    'able':'KOmmen',
    'about':'gegen',
    'above':'Uber',
    'absence':'Abwesenheit',
    'absent':'abwesend',
    'accent':'Betonung',
    'accept':'akzeptieren',
    'according':'nach',
    'acquainted':'kennen',
    'across':'iiber',
    'actor':'Schauspieler',
    'address':'Adresse',
    'add':'hinzufugen',
    'admit':'zugeben',
    'adolescence':'Jugendalter',
    'adopt':'adoptieren',
    'adventure':'Abenteuer',
    'advertisement':'Anzeige',
    'adviser':'Ratgeber',
    'afraid':'sich furchten',
    'after':'nach',
    'afternoon':'Nachmittag',
    'air':'Luft',
    'airplane':'Flugzeug',
    'airport':'Flughafen',
    'air-conditioned':'Klimaanlage',
    'alarm clock':'Wecker'
    #coded till 'Z' words
}
def word_Rep(text,dic_word):
    rc = re.compile('|'.join(map(re.escape,dic_word)))
    def translate(match):
                return dic_word[match.group()]
        return rc.sub(translate, text)
Trans=word_Rep(c,dic_word)
#print Trans
con.write(Trans)
f.close
con.close

Here the sample input is SRS.txt(created in English) and the sample output is Germansrs.txt(Translated to German).

@Varunkrishna
It doesn't translate exactly? What do you mean by that? Does the code give any error(s)? What do the text files you are having a problem with look like?

@WolfShield
Let us take for example "Tea means Tee in German" So for example let us take Teacher,"Teacher means Lehrer in German" So if my text document contains teacher it must replace teacher it must replace with lehrer but it is replacing with Teecher. Can you guess what is happening. So to address this I had followed the comment by vegaseat in this Post. So I had followed his way and I was out of luck or may be I didn't know how to make it work for my application.

Your question was already answered in this post with a demo of converting some words using a dictionary. You insist on using re which produces the undesired results, instead of a dictionary lookup. There is nothing that anyone can do to make bad code work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.