5

我已經編寫了一個語言翻譯程序,我想用它來使用Python Goslate庫將數據從文件翻譯成其他語言。 在我的終端上運行代碼時,代碼將一些文本轉換爲法語,這是我設置的默認語言。使用python線程的語言翻譯程序

在將幾行文本轉換爲法語後,該程序給出HTTP請求錯誤,指出HTTP請求超時。

 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""" 

的Goslate庫輕鬆地與小文本交易,並將它們轉換爲目標語言, 但我想實現它來對付大的文本文件。

這是我的代碼。我需要幫助正確格式化線程以將所有文本轉換爲另一種語言。

# 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) 
+2

而且大家都知道,它可能是谷歌誰是節流嗎?免費翻譯服務已停止。有一個谷歌翻譯API,它是一個[付費服務](https://developers.google.com/translate/v2/pricing)now- – tripleee

+0

,但它適用於單個字符串,因爲我輸入單個字符串goslate翻譯。 – rishabhr0y

+0

繼承人一個工作代碼[在github上](https://github.com/rishabhsixfeet/TRanslate/blob/master/googletranslate.py),它接受字符串並將其轉換爲目標語言,然後猜測問題出在線程中正在處理文本字符串和其他線程之間的呼叫差距 – rishabhr0y

回答