Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Language translator using python threads

I have written a program for a language translator which I want to use to translate data from files to other languages using the Python Goslate library. While running the code on my terminal the code converts some of the text to French, which is the default language I have set.

After converting a few lines of text to French the program gives an HTTP request error saying that the HTTP request timed out.

      File "/usr/lib/python2.7/threading.py", line 808, in __bootstrap_inner
        self.run()
      File "m.py", line 27, in run
        new=gs.translate(host,'fr')
      File "/home/rishabh/goslate.py", line 338, in translate
        return self._translate_single_text(text, target_language, source_language)
      File "/home/rishabh/goslate.py", line 283, in _translate_single_text
        return ''.join(self._execute(make_task(i) for i in split_text(text)))
      File "/home/rishabh/goslate.py", line 166, in _execute
        yield each()
      File "/home/rishabh/goslate.py", line 281, in <lambda>
        return lambda: self._basic_translate(text, target_language, source_lauguage)[0]
      File "/home/rishabh/goslate.py", line 206, in _basic_translate
        response_content = self._open_url(url)
      File "/home/rishabh/goslate.py", line 154, in _open_url
        raise e
    timeout: timed out"""

The Goslate library easily deals with small texts and converts them to the destination language, but I am trying to implement it to deal with large text files.

Here is my code. I need help formatting the threads properly to convert all the text to another language.

    # translating words using google translation api
    #install goslate a python module for translating using google translate api i n windows easy_install goslate
    import goslate
    import threading
    import sys
    import Queue
    import time
    queue=Queue.Queue()

    gs = goslate.Goslate()
    f=open("c:\\Users\\kiit\\SkyDrive\\Pictures\\new.txt",'r').read()
    hosts=f.split("\n")#makes a list of sentences in the file so as to translate line by line 


    class Threadtranslate(threading.Thread):
        def __init__(self,queue):
            threading.Thread.__init__(self)
            self.queue=queue

        def run(self):
            while True:
          l      host=self.queue.get()
                new=gs.translate(host,'fr')#to translate the lines in hosts to frenchlanguage
                print new

                self.queue.task_done()

    start=time.time()
    def main():
        for i in range(len(hosts)):
            t=Threadtranslate(queue)
            t.setDaemon(True)
            t.start()
            for host in hosts:
                queue.put(host)

        queue.join()

    main()
    print "Elapsed Time: %s" % (time.time() - start)
like image 473
rishabhr0y Avatar asked Nov 11 '22 10:11

rishabhr0y


1 Answers

try this, it comes in handy when goslate no lonher goes through, for now, https://pypi.python.org/pypi/textblob

like image 93
programmer44 Avatar answered Nov 15 '22 07:11

programmer44