5

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)
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
rishabhr0y
  • 838
  • 1
  • 9
  • 14
  • 2
    And you are aware that it's probably Google who is throttling you? The free translation service was discontinued. There is a Google Translate API which is a [pay-only service](https://developers.google.com/translate/v2/pricing) now- – tripleee Mar 27 '14 at 13:44
  • but it works well for a individual strings as i input a single strings in goslate to translate . – rishabhr0y Mar 27 '14 at 14:01
  • heres a working code which [on github](https://github.com/rishabhsixfeet/TRanslate/blob/master/googletranslate.py) which takes the string and converts it to destination languagei guess the problem is in the way the threads is handling the text strings and the gap of call between the other threads – rishabhr0y Mar 27 '14 at 14:04
  • I wasn't able to get this code to work. – Gabriel Fair Jul 23 '15 at 23:30

1 Answers1

1

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

programmer44
  • 499
  • 1
  • 5
  • 11