{ "info": { "author": "Will Filho", "author_email": "dookgulliver@willfilho.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Topic :: Software Development :: Internationalization", "Topic :: Utilities" ], "description": "Bing Translator API for Python\r\n------------------------------\r\n\r\nThe *Microsoft* offers an online translation API to make requests\r\nthrough various forms (Http, Ajax, SOAP or OData.). The features\r\nprovided by this API are very useful when we want to perform\r\ntranslations, multiple translations or even listen to the pronunciation\r\nof a certain word or phrase in desejada.Esta language library is an\r\nimplementation in Python that allows to use most of the features present\r\nin this API. Basically what we do is, make a request to the Ajax API\r\nmethod by **requests** module and get the desired response. Below is a\r\nlist of features offered by this library:\r\n\r\n- Translation of one or multiple phrases and texts.\r\n- Obtaining all possible translations for a word or multiple phrases.\r\n- So speak, where you can hear the pronunciation of phrases or text in\r\n the desired language.\r\n- This language detection on single or multiple sentences or texts.\r\n- Obtaining all possible languages for translation and so speak.\r\n\r\nInstallation\r\n~~~~~~~~~~~~\r\n\r\nYou can use pip to make download and installation of the library. It is\r\nnoteworthy that, it uses three modules: requests, json and urllib. Using\r\nthe pip:\r\n\r\n.. code:: sh\r\n\r\n $ pip install bing_translator\r\n\r\nGetting credentials\r\n~~~~~~~~~~~~~~~~~~~\r\n\r\nFirst of all, for us to use Microsoft's API we have to get a credential.\r\nWhich to some extent is free, unlike the Google Translator API that is\r\nonly within 30 days. It is noteworthy that the restriction to the free\r\nacquisition of credentials is 2 million characters per month, that is,\r\nyou should not spend it. If you have an application that is used by a\r\nlot of customers, recommend reviewing it. To see all the steps that must\r\nbe taken, go to the following link and follow the tutorial present in\r\nit: http://blogs.msdn.com/b/translation/p/gettingstarted1.aspx\r\n\r\nThe information that we will use are: the *client\\_id* and\r\n*client\\_secret*. These credentials are used in the request for\r\nadqurirmos the token that allows us to perform translations. ### Getting\r\nStarted The use of the library is very simple, just by knowing what are\r\nthe methods and their parameters. For example, we do a simple\r\ntranslation as follows:\r\n\r\n.. code:: python\r\n\r\n from bing_translator import Bing #importing Bing class for translations.\r\n\r\n client_id = \"\"\r\n client_secret = \"\"\r\n\r\n translator = Bing(client_id, client_secret)\r\n phrase_translated = translator.translate(\"Hello World\", \"pt\") #translating phrase\r\n print (phrase_translated)\r\n\r\nTo see the code of all languages, access the following link:\r\nhttp://msdn.microsoft.com/en-us/library/hh456380.aspx. In the previous\r\nexample, we use the *'en'* code to have displaced who wish to translate\r\nour words into Portuguese. ##### The *translate* method\r\n\r\nThe translate method may receive a number of parameters, as we can see\r\nin the table below.\r\n\r\n+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| Parameter Name | Description |\r\n+==================+==================================================================================================================================================================================+\r\n| text | Is the text to be translated. This parameter is required. |\r\n+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| to\\_lang | This parameter corresponds to the language to which the text should be translated. This parameter is required. |\r\n+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| from\\_lang | This parameter corresponds to this language in the current sentence. This parameter is not required because the Microsoft API recognizes this language in the text by default. |\r\n+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| contentType | This parameter defines the type of data returned. By default it is set to \"text/plain\", but you can use another option, which is \"text/html\". This parameter is not required. |\r\n+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| category | Matches the category of the sentence. By default this parameter is set to \"General\". This parameter is not required. |\r\n+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n\r\nOne of the other methods in this library, much like the translate method\r\nis **translate\\_texts**. The difference between these two methods is\r\nthat the latter accepts a list of strings as parameter. Besides the\r\naforementioned differences, the translate\\_texts method allows the use\r\nof three additional parameters. Consider the table below:\r\n\r\n+------------------+----------------------------------------------------------------------------------------------------------+\r\n| Parameter Name | Description |\r\n+==================+==========================================================================================================+\r\n| uri | A string containing the content location of this translation. |\r\n+------------------+----------------------------------------------------------------------------------------------------------+\r\n| user | A string used to track the originator of the submission. |\r\n+------------------+----------------------------------------------------------------------------------------------------------+\r\n| state | User state to help correlate request and response. The same contents will be returned in the response. |\r\n+------------------+----------------------------------------------------------------------------------------------------------+\r\n\r\nExample of use:\r\n\r\n.. code:: python\r\n\r\n phrases_translated = translator.translate_texts([\"Hello World\",\"Python is all\"], \"pt\") #translating phrase\r\n print (phrase_translated)\r\n\r\nThe return will be something like this:\r\n\r\n::\r\n\r\n [{'OriginalTextSentenceLengths': [9], 'TranslatedText': 'Ol\u00e1 o mundo', 'From': 'en', 'TranslatedTextSentenceLengths': [12]}, {'OriginalTextSentenceLengths': [10], 'TranslatedText': 'Python \u00e9 tudo', 'From': 'en', 'TranslatedTextSentenceLengths': [15]}]\r\n\r\nThe *get\\_translations* method\r\n''''''''''''''''''''''''''''''\r\n\r\nThe **get\\_translations** method returns all possible translations for a\r\nword in the language. This method is very similar to the previous\r\n(**translate\\_texts**), the difference is that you must pass as\r\nparameter only a text/word and he has a parameter called maxTranslations\r\nthat is required. This parameter defines the number of possible\r\ntranslations that the API should return to you. Example of use:\r\n\r\n.. code:: python\r\n\r\n trans = translator.get_translations(\"Speak\", \"pt\", 2)\r\n print (trans)\r\n\r\nThe return will be something like this:\r\n\r\n::\r\n\r\n {'Translations': [{'MatchDegree': 100, 'Rating': 5, 'MatchedOriginalText': '', 'TranslatedText': 'Fala', 'Count': 0}, {'MatchDegree': 99, 'Rating': 1, 'MatchedOriginalText': 'speak', 'TranslatedText': 'Falar', 'Count': 1}], 'From': 'en'}\r\n\r\nThe other very similar method to this is the\r\n**get\\_multiple\\_translations**. The difference between this and the\r\nformer is that you will pass this parameter as a list of strings rather\r\nthan just a string. Example of use:\r\n\r\n.. code:: python\r\n\r\n trans = translator.get_translations([\"Speak\",\"Fly\"], \"pt\", 2)\r\n print (trans)\r\n\r\nThe *detect* method\r\n~~~~~~~~~~~~~~~~~~~\r\n\r\nThe detect method, as its name implies, identifies the language used in\r\na given sentence. He will receive only one parameter, which is the\r\ndesired phrase. Example of use:\r\n\r\n.. code:: python\r\n\r\n trans = translator.detect(\"My name is Jonas\")\r\n print (trans)\r\n\r\nIn return we will have the code of the corresponding language to the\r\ntext. In the example above, we get an answer the following: 'en';\r\nIndicating that the text is in English. We can also identify the present\r\nlanguages in various texts by simply we use the **detect\\_texts**\r\nmethod, which receive as parameter a list of strings. Example of use:\r\n\r\n.. code:: python\r\n\r\n trans = translator.detect_texts([\"My name is Jonas\",\"Voar, subir, cair.\"])\r\n print (trans)\r\n\r\nThe *speak\\_phrase* method\r\n''''''''''''''''''''''''''\r\n\r\nThe speak\\_phrase method is responsible for providing us with a link to\r\ndownload the audio containing the desired phrase. This method will\r\nreceive different parameters from previous. Let's look at the table\r\nbelow:\r\n\r\n+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| Parameter Name | Description |\r\n+==================+================================================================================================================================================================================================================================================================================================================+\r\n| text | Text to be spoken. This parameter is required. |\r\n+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| language | Language in which the text should be spoken. |\r\n+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| format\\_audio | This parameter defines the audio format. By default the audio defined by Microsoft API file format is the \"audio/wav\", but you can use another parameter, which is, \"audio/mp3\". This parameter is not required. |\r\n+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| option | This parameter defines the audio quality. By default, the quality used by the API is \"MinQuality\", but we can use the value \"MaxQuality\" to indicate that we want a better quality audio with. Worth to emphasize that this will influence the size of the file to download. This parameter is not required. |\r\n+------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n\r\nExample of use:\r\n\r\n.. code:: python\r\n\r\n trans = translator.speak_phrase(\"Back to the future\", \"en\", \"audio/mp3\", \"MaxQuality\")\r\n print (trans)\r\n\r\nIn return we will have a string containing a URL to perform download the\r\naudio. To do this, we can use **AudioSpeaked** class with the\r\n**download** method, to use the audio locally. Example of use:\r\n\r\n.. code:: python\r\n\r\n from bing_translator import Bing, AudioSpeaked\r\n\r\n client_id = \"\"\r\n client_secret = \"\"\r\n\r\n translator = Bing(client_id, client_secret)\r\n url = translator.speak_phrase(\"Back to the future\", \"en\", \"audio/mp3\", \"MaxQuality\")\r\n AudioSpeaked.download(url, \"audios/\",\"aud01.mp3\")\r\n\r\nThe **download** of AudioSpeaked class classmethod receive three\r\nparameters. The first is the url to download the audio, the second the\r\ndirectory where it is stored and finally the audio name. Note that audio\r\nextension must be the same length stated in the call **speak\\_phrase**\r\nmethod. For example, in the above code I set that I would have an\r\nextension \"audio/mp3\" and download method I set the file name with an\r\nextension mp3.\r\n\r\nLicense\r\n~~~~~~~\r\n\r\nBing Translator API for Python\r\n\r\nCopyright (c) 2014, Will Filho, All rights reserved.\r\n\r\nThis library is free software; you can redistribute it and/or modify it\r\nunder the terms of the GNU Lesser General Public License as published by\r\nthe Free Software Foundation; either version 3.0 of the License, or (at\r\nyour option) any later version.\r\n\r\nThis library is distributed in the hope that it will be useful, but\r\nWITHOUT ANY WARRANTY; without even the implied warranty of\r\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser\r\nGeneral Public License for more details.\r\n\r\nYou should have received a copy of the GNU Lesser General Public License\r\nalong with this library.", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/dookgulliver/bing_translator", "keywords": "microsoft translator,bing", "license": "LGPL", "maintainer": null, "maintainer_email": null, "name": "bing_translator", "package_url": "https://pypi.org/project/bing_translator/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/bing_translator/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/dookgulliver/bing_translator" }, "release_url": "https://pypi.org/project/bing_translator/0.1/", "requires_dist": null, "requires_python": null, "summary": "Micrososft Translator API V2 for Python", "version": "0.1" }, "last_serial": 1325105, "releases": { "0.1": [] }, "urls": [] }