GithubHelp home page GithubHelp logo

googletranslateapi's Introduction

Motivation

After released mgd v0.1(a command-line front end of google translation service), I realized that there's few mature python package could fulfill my requirements, such as fine-grained single word translation and TTS service. This project is proposed for providing an end solution of google translation api.

This project is mainly inspired by goslate and Google-Translate-TTS.

Interface

  • TranslateService.trans_details(self, src_lang, tgt_lang, src_text)
  • TranslateService.trans_sentence(self, src_lang, tgt_lang, src_text)
  • TranslateService.detect(self, src_text)
  • TTSService.get_mpeg_binary(self, tgt_lang, src_text)

Interfaces presented above is quite self-explaining. For more information, such as please read documentation strings of each interface.

Example of Usage

TranslateService

Python 3.3.3 (default, Dec 10 2013, 18:43:00)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import google_translate_api as api
>>> translator = api.TranslateService()
>>> translator.trans_details('en', 'zh-CN', 'test')
{'sentences': [{'orig': 'test', 'trans': '测试', 'src_translit': '', 'translit': 'Cèshì'}], 'dict': [{'pos': 'noun', 'entry': [{'reverse_translation': ['test', 'examination'], 'score': 0.61608213, 'word': '测试'}, {'reverse_translation': ['test', 'experiment', 'tentative'], 'score': 0.18211353, 'word': '试验'}, {'reverse_translation': ['test', 'examination', 'experiment', 'exam', 'fitting'], 'score': 0.019194625, 'word': '试'}, {'reverse_translation': ['experiment', 'test'], 'score': 0.013611027, 'word': '实验'}, {'reverse_translation': ['examination', 'exam', 'test'], 'score': 0.012588142, 'word': '考试'}, {'reverse_translation': ['test', 'trial', 'ordeal'], 'score': 0.012392981, 'word': '考验'}, {'reverse_translation': ['test', 'quiz'], 'score': 0.0078774579, 'word': '测验'}], 'base_form': 'test', 'terms': ['测试', '试验', '试', '实验', '考试', '考验', '测验'], 'pos_enum': 1}, {'pos': 'verb', 'entry': [{'reverse_translation': ['test', 'examine', 'inspect'], 'score': 0.043255754, 'word': '检验'}, {'reverse_translation': ['test', 'try'], 'score': 0.019194625, 'word': '试'}, {'reverse_translation': ['test', 'study', 'examine', 'investigate', 'verify', 'check'], 'score': 0.011461634, 'word': '考'}, {'reverse_translation': ['test', 'put to test'], 'score': 0.0078774579, 'word': '测验'}, {'reverse_translation': ['test', 'check', 'verify', 'examine', 'prove', 'confirm'], 'score': 0.0011893183, 'word': '验'}, {'reverse_translation': ['test', 'investigate', 'check', 'study'], 'score': 0.00041749, 'word': '考查'}, {'reverse_translation': ['taste', 'flavor', 'try the flavor', 'test', 'flavour'], 'score': 7.5711552e-07, 'word': '尝'}], 'base_form': 'test', 'terms': ['检验', '试', '考', '测验', '验', '考查', '尝'], 'pos_enum': 2}], 'src': {'en': 1.0}, 'server_time': 4, 'spell': {'spell_res': 'Test', 'related': True, 'correction_type': [10]}}
>>> translator.trans_sentence('en', 'zh-CN', 'test')
'测试'
>>> translator.detect('test')
{'en': 1.0}

TTSService

>>> tts = api.TTSService()
>>> import subprocess
>>> import tempfile
>>> with tempfile.NamedTemporaryFile() as f:
...     data = tts.get_mpeg_binary('en', 'This is a sentence.')
...     f.write(data)
...     subprocess.call(['afplay', f.name])

googletranslateapi's People

Stargazers

 avatar

Watchers

Jon Bradley avatar James Cloos avatar Hunt Zhan avatar  avatar

Forkers

jackieee

googletranslateapi's Issues

sample script errors

The sample script which you provide on PyPi errors.

Input:

import google_translate_api as api
translator = api.TranslateService()
translator.trans_details('en', 'zh-CN', 'test')
translator.trans_sentence('en', 'zh-CN', 'test')    
translator.detect('test')

Output:

~/Desktop/test % python3 translate.py
Traceback (most recent call last):
  File "translate.py", line 5, in <module>
    translator.trans_details('en', 'zh-CN', 'test')
  File "/usr/local/lib/python3.5/site-packages/google_translate_api.py", line 329, in trans_details
    return self._translate(src_lang, tgt_lang, src_text)
  File "/usr/local/lib/python3.5/site-packages/google_translate_api.py", line 315, in _translate
    return self._request(src_lang, tgt_lang, src_texts)
  File "/usr/local/lib/python3.5/site-packages/google_translate_api.py", line 210, in _request
    self._check_threads(threads)
  File "/usr/local/lib/python3.5/site-packages/google_translate_api.py", line 97, in _check_threads
    raise future.exception()
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.5/site-packages/google_translate_api.py", line 133, in _basic_request
    return response.json()
  File "/usr/local/lib/python3.5/site-packages/requests/models.py", line 812, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

This was using Python 3.5. Is it possible that google no longer supports the translation service in this way? If so, can you provide a fix or remove the library from PyPi?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.