Giter VIP home page Giter VIP 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])

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.