使用python线程的语言转换器
我已经为一个语言转换器编写了一个程序,我想用它使用Python库将数据从文件翻译成其他语言。 在我的终端上运行代码时,代码会将部分文本转换为法语,这是我设置的默认语言 将几行文本转换为法语后,程序会发出HTTP请求错误,表示HTTP请求超时使用python线程的语言转换器,python,multithreading,localization,google-translate,language-translation,Python,Multithreading,Localization,Google Translate,Language Translation,我已经为一个语言转换器编写了一个程序,我想用它使用Python库将数据从文件翻译成其他语言。 在我的终端上运行代码时,代码会将部分文本转换为法语,这是我设置的默认语言 将几行文本转换为法语后,程序会发出HTTP请求错误,表示HTTP请求超时 File "/usr/lib/python2.7/threading.py", line 808, in __bootstrap_inner self.run() File "m.py", line 27, in ru
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 NoLonher通过时,它就派上了用场,现在,你知道可能是谷歌在扼杀你吗?意译服务中断了。有一个Google Translate API,它是一个now,但它对单个字符串运行良好,因为我在goslate中输入了单个字符串进行翻译。下面是一个工作代码,它将字符串转换为目标语言。我想问题在于线程处理文本字符串的方式以及其他字符串之间的调用间隙threadsI无法使此代码正常工作。请在答案中输入相关信息。
# 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)