Translate with Microsoft Translator in Python - AzureTranslate

While I try to create a new service I create a small script in Python to translate with Microsoft Translator. 

To use this you have to connect to this website https://datamarket.azure.com/dataset/bing/microsofttranslator and choose your preferred plan. The first 2.000.000 characters are free. After you register an app you'll receive a Client ID and a Client Secret to replace in code.

With code I create a small command in AIML, the bot name is Azure (https://github.com/MyRobotLab/pyrobotlab/tree/master/home/Papaouitai/aiml), try for example:"Translate hello in italian"


#file : home/Papaouitai/azureTranslator.py edit raw
import urllib, urllib2
import json

from datetime import datetime

acapelaSpeech = Runtime.createAndStart("speech", "AcapelaSpeech")
client_id = 'your client id'
client_secret = 'your client secret'
azure = Runtime.createAndStart('azure', "ProgramAB")
htmlfilter = Runtime.createAndStart("htmlfilter", "HtmlFilter")
azure.addTextListener(htmlfilter)
htmlfilter.addTextListener(acapelaSpeech)

azure.startSession('default', 'azure')

def datestring (display_format="%a, %d %b %Y %H:%M:%S", datetime_object=None):
    if datetime_object is None:
        datetime_object = datetime.utcnow()
    return datetime.strftime(datetime_object, display_format)

def get_access_token ():

    data = urllib.urlencode({
            'client_id' : client_id,
            'client_secret' : client_secret,
            'grant_type' : 'client_credentials',
            'scope' : 'http://api.microsofttranslator.com'
            })

    try:

        request = urllib2.Request('https://datamarket.accesscontrol.windows.net/v2/OAuth2-13')
        request.add_data(data) 

        response = urllib2.urlopen(request)
        response_data = json.loads(response.read())

        if response_data.has_key('access_token'):
            return response_data['access_token']

    except urllib2.URLError, e:
        if hasattr(e, 'reason'):
            print datestring(), 'Could not connect to the server:', e.reason
        elif hasattr(e, 'code'):
            print datestring(), 'Server error: ', e.code
    except TypeError:
        print datestring(), 'Bad data from server'

supported_languages = { # as defined here: http://msdn.microsoft.com/en-us/library/hh456380.aspx
    'ar' : ' Arabic',
 #   'bs-Latn' : 'Bosnian (Latin)',
 #  'bg' : 'Bulgarian',
 #   'ca' : 'Catalan',
 #   'zh-CHS' : 'Chinese (Simplified)',
 #   'zh-CHT' : 'Chinese (Traditional)',
 #   'hr' : 'Croatian',
 #   'cs' : 'Czech',
    'da' : 'Danish',
    'nl' : 'Dutch',
    'en' : 'English',
 #  'et' : 'Estonian',
 #  'fi' : 'Finnish',
    'fr' : 'French',
    'de' : 'German',
    'el' : 'Greek',
 #  'ht' : 'Haitian Creole',
 #  'he' : 'Hebrew',
 #  'hi' : 'Hindi',
 #  'mww' : 'Hmong Daw',
 #  'hu' : 'Hungarian',
 #  'id' : 'Indonesian',
    'it' : 'Italian',
 #  'ja' : 'Japanese',
 #  'sw' : 'Kiswahili',
 #  'tlh' : 'Klingon',
 #  'tlh-Qaak' : 'Klingon (pIqaD)',
 #  'ko' : 'Korean',
 #  'lv' : 'Latvian',
 #  'lt' : 'Lithuanian',
 #  'ms' : 'Malay',
 #  'mt' : 'Maltese',
    'no' : 'Norwegian',
 #  'fa' : 'Persian',
 #  'pl' : 'Polish',
 #  'pt' : 'Portuguese',
 #  'otq' : 'Querétaro Otomi',
 #  'ro' : 'Romanian',
 #  'ru' : 'Russian',
 #  'sr-Cyrl' : 'Serbian (Cyrillic)',
 #  'sr-Latn' : 'Serbian (Latin)',
 #  'sk' : 'Slovak',
 #  'sl' : 'Slovenian',
    'es' : 'Spanish',
    'sv' : 'Swedish',
 #  'th' : 'Thai',
 #  'tr' : 'Turkish',
 #  'uk' : 'Ukrainian',
 #  'ur' : 'Urdu',
 #  'vi' : 'Vietnamese',
 #  'cy' : 'Welsh',
 #  'yua' : 'Yucatec Maya',
}

male_languages = { 
    'ar' : ' Nizar',
    'da' : 'Rasmus',
    'nl' : 'Jeroen',
    'en' : 'Ryan',
    'fr' : 'Antoine',
    'de' : 'Klaus',
    'el' : 'Dimitris',
    'it' : 'Vittorio',
    'no' : 'Olav',
    'es' : 'Antonio',
    'sv' : 'Emil',
}
en_languages = {
    'arab' : ' ar',
    'danish' : 'da',
    'dutch' : 'nl',
    'english' : 'en',
    'french' : 'fr',
    'german' : 'de',
    'greek' : 'el',
    'italian' : 'it',
    'norway' : 'no',
    'spanish' : 'es',
    'sweden' : 'sv',
}
def print_supported_languages ():
    codes = []
    for k,v in supported_languages.items():
        codes.append('\t'.join([k, '=', v]))
    return '\n'.join(codes)

def to_bytestring (s):
    if s:
        if isinstance(s, str):
            return s
        else:
            return s.encode('utf-8')

def translate (access_token, text, to_lang, from_lang=None):
    if not access_token:
        azure.getResponse('Say Sorry, the access token is invalid'
    else:
        if to_lang not in supported_languages.keys():
            azure.getResponse("Say I haven't learned this language")
            print print_supported_languages()
        else:
            data = { 'text' : to_bytestring(text), 'to' : to_lang }

            if from_lang:
                 if from_lang not in supported_languages.keys():
                    azure.getResponse("Say I haven't learned this language")
                    print print_supported_languages()
                    return
                else:
                    data['from'] = from_lang

            try:

                request = urllib2.Request('http://api.microsofttranslator.com/v2/Http.svc/Translate?'+urllib.urlencode(data))
                request.add_header('Authorization', 'Bearer '+access_token)

                response = urllib2.urlopen(request)
                return response.read().replace('', '').replace('', '')
            
            except urllib2.URLError, e:
                if hasattr(e, 'reason'):
                    print datestring(), 'Could not connect to the server:', e.reason
                elif hasattr(e, 'code'):
                    print datestring(), 'Server error: ', e.code
def translateText(text,language):
   to = en_languages[language]
   t_text = translate(get_access_token (),text,to)     
   acapelaSpeech.setVoice(male_languages[to])  
   azure.getResponse('Say '+t_text)


#file : home/Papaouitai/aiml/azure.aiml edit raw


    
    TRANSLATE * IN *
    
	
	  
SAY *




Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bartcam's picture

Not work

In line 145 it has no end ")" before are error.
If I add ) there still not work.

moz4r's picture

Thank for your work.

Thank for your work. Microsoft had migrate azure translator and this service no worky anymore ( or very soon ). Is it possible to update it ?

"DataMarket and Data Services are being retired and will stop accepting new orders after 12/31/2016. Existing subscriptions will be retired and cancelled starting 3/31/2017. Please reach out to your service provider for options if you want to continue service."

http://datamarket.azure.com/dataset/bing/microsofttranslator

moz4r's picture

fixed, azure translator worky

fixed, azure translator worky !