怎样才能做出完美的释义?

时间:2017-06-05 04:23:07

标签: python python-3.x

参考答案:https://stackoverflow.com/a/3592020/4948889,我试图修改下面的例子。上面链接中的答案中的Google API对我不起作用。所以我修改了下面给出的示例:

import sys
import json
import goslate

def translate(text,lang1,lang2):

    gs = goslate.Goslate()
    content= gs.translate(text, lang2)
    try:
        trans_dict=json.loads(content)
    except AttributeError:
        try:
            trans_dict=json.load(content)    
        except AttributeError:
            trans_dict=json.read(content)
    return trans_dict['responseData']['translatedText']

languages='de da nl zh-tw ko es pt el'.split()
# text=(' '.join(sys.argv[1:])).decode('utf-8')
text = "Hi, We have a requirement in which we need to change the words or phrases in the sentence while keeping its meaning intact."

for lang in languages:
    result=translate(text,'en',lang)
    result=translate(result,lang,'en')
    print(result)
    print

运行程序后,我收到了以下错误:

Traceback (most recent call last):
  File "C:\Users\PC\Desktop\testrephrase.py", line 25, in <module>
    result=translate(text,'en',lang)
  File "C:\Users\PC\Desktop\testrephrase.py", line 10, in translate
    content= gs.translate(text, lang2)
  File "C:\Python36\lib\site-packages\goslate.py", line 409, in translate
    return _unwrapper_single_element(self._translate_single_text(text, target_language, source_language))
  File "C:\Python36\lib\site-packages\goslate.py", line 334, in _translate_single_text
    results = list(self._execute(make_task(i) for i in split_text(text)))
  File "C:\Python36\lib\site-packages\goslate.py", line 203, in _execute
    yield each()
  File "C:\Python36\lib\site-packages\goslate.py", line 332, in <lambda>
    return lambda: self._basic_translate(text, target_language, source_lauguage)[0]
  File "C:\Python36\lib\site-packages\goslate.py", line 251, in _basic_translate
    response_content = self._open_url(url)
  File "C:\Python36\lib\site-packages\goslate.py", line 191, in _open_url
    raise e
  File "C:\Python36\lib\site-packages\goslate.py", line 181, in _open_url
    response = self._opener.open(request, timeout=self._TIMEOUT)
  File "C:\Python36\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Python36\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python36\lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
  File "C:\Python36\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Python36\lib\urllib\request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 503: Service Unavailable

我不知道该做什么,我的代码有效运行并创建释义而不改变句子的含义。

我使用Python 3.6和Windows 10作为操作系统 请帮助我。

1 个答案:

答案 0 :(得分:2)

以下link显示以下内容:

  

谷歌最近用票证更新了翻译服务   防止像goslate这样的简单爬虫程序的机制   访问。虽然更复杂的爬虫可能仍然有效   从技术上讲,它会越过使用之间的细微差别   服务和破坏服务。 goslate不会更新到   打破谷歌的票务机制。免费午餐结束了。感谢您使用。

所以我目前没有工作的库,因此这个例子没用。