我编写了一个语言翻译程序,我想用它来使用Python Goslate库将数据从文件翻译成其他语言。 在终端上运行代码时,代码会将部分文本转换为法语,这是我设置的默认语言。
将几行文本转换为法语后,程序会发出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)