%3CLINGO-SUB%20id%3D%22lingo-sub-379180%22%20slang%3D%22en-US%22%3EMicrosoft%20Cognitive%20Services%20Translation%20API%20with%20Python%20%26amp%3B%20C%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-379180%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Aug%2021%2C%202017%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId-1175966006%22%20id%3D%22toc-hId-1176020818%22%3E%3C%2FH3%3E%3CBR%20%2F%3E%20If%20you%20want%20to%20programmatically%20translate%20text%20from%20one%20language%20to%20another%2C%20then%20the%20Translation%20service%20(translation%20api)%20is%20the%20right%20one%20for%20you.%20The%20Microsoft%20Translation%20Service%20just%20requires%20an%20authentication%20via%20a%20Microsoft%20Cognitive%20Services%20API%20Key%20and%20then%20you%20can%20easily%20translate%20text%20from%20one%20language%20to%20another.%20By%20leveraging%20the%20scale%20and%20power%20of%20Microsoft%E2%80%99s%20AI%20supercomputer%2C%20specifically%20the%20Microsoft%20Cognitive%20Toolkit%2C%20Microsoft%20Translator%20now%20offers%20neural%20network%20(LSTM)%20based%20translation%20that%20enables%20a%20new%20decade%20of%20translation%20quality%20improvement.%20These%20neural%20network%20models%20are%20available%20for%20all%20speech%20languages%20through%20the%20Microsoft%20Translator%20Speech%20API%20on%20the%20try%20and%20compare%20site%20%3CA%20href%3D%22http%3A%2F%2Ftranslator.microsoft.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20http%3A%2F%2Ftranslator.microsoft.com%20%3C%2FA%3E%20and%20through%20the%20text%20API%20by%20using%20the%20%E2%80%98generalnn%E2%80%99%20category%20ID.%20%3CBR%20%2F%3EHow%20do%20it%20work%3F%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F94845i91F8AA5967E6E903%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20In%20this%20blog%20post%2C%20I%E2%80%99ll%20quickly%20describe%20how%20to%20create%20the%20translation%20service%20and%20how%20to%20use%20it%20with%20Python%2C%20Jupyter%20Notebook%2C%20C%23%2C%20Powershell%20and%20Node.js.%20%3CBR%20%2F%3E%20Create%20the%20translation%20service%20in%20Azure%20%3CBR%20%2F%3E%20The%20translation%20service%20is%20part%20of%20the%20cognitive%20services%20and%20can%20therefore%20be%20found%20as%20cognitive%20service%20in%20the%20Azure%20portal%3A%20For%20this%20demo%20the%20F0%20Free%20Services%20is%20more%20than%20capable%20of%20supporting%20the%20example%20with%20over%202Million%20character%20being%20supported.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F94846iF960A552B27B2AD8%22%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EThe%20creation%20of%20the%20service%20just%20takes%20a%20few%20seconds.%20After%20the%20creation%2C%20we%20can%20check%20the%20service%20and%20see%2C%20that%20it%20is%20globally%20deployed%20and%20it%20also%20shows%20us%20the%20endpoint%20url.%20The%20keys%20can%20be%20accessed%20via%20the%20keys%20link%20in%20menu.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F94847i2A8C4AAE6FA39563%22%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ENavigate%20to%20it%20and%20copy%20one%20of%20the%20keys%20that%20are%20shown%20there.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F94848i0FF03C9C9B60E623%22%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ENow%20we%20are%20ready%20to%20implement%20a%20simple%20python%2C%20script%26nbsp%3B%20that%20reads%20text%26nbsp%3B%20and%20translates%20it%20to%20another%20language%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Translate%20text%20using%20Python%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%23install%20additional%20libraries%20to%20add%20coloured%20text%20to%20output%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20!pip%20install%20termcolor%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20!pip%20install%20bs4%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20from%20termcolor%20import%20colored%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20from%20bs4%20import%20BeautifulSoup%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20import%20requests%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23Using%20Python%20for%20Text%20Translation%20with%20Microsoft%20Cognitive%20Services%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Specify%20the%20subscription%20Key%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20subscriptionKey%20%3D%20%22ENTER%20YOUR%20COGNITIVE%20API%20KEY%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23Specify%20URLs%20for%20Cognitive%20Services%20-%20Translator%20Text%20API%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20translateUrl%20%3D%20'%3CA%20href%3D%22https%3A%2F%2Fapi.microsofttranslator.com%2Fv2%2Fhttp.svc%2FTranslate%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fapi.microsofttranslator.com%2Fv2%2Fhttp.svc%2FTranslate%3C%2FA%3E'%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20cognitiveServiceUrl%20%3D%20'%3CA%20href%3D%22https%3A%2F%2Fapi.cognitive.microsoft.com%2Fsts%2Fv1.0%2FissueToken%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fapi.cognitive.microsoft.com%2Fsts%2Fv1.0%2FissueToken%3C%2FA%3E'%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Request%20Access%20Token%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20requestHeader%20%3D%20%7B'Ocp-Apim-Subscription-Key'%3A%20subscriptionKey%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20responseResult%20%3D%20requests.post(cognitiveServiceUrl%2C%20headers%3DrequestHeader)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20token%20%3D%20responseResult.text%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20print%20(%22Access%20Token%22)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20print%20(token)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Original%20Text%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20text%20%3D%20%22Cr%C3%A9ez%20des%20applications%20intelligentes%20et%20strat%C3%A9giques%20avec%20une%20plateforme%20de%20base%20de%20donn%C3%A9es%20%C3%A9volutive%20et%20hybride%20qui%20int%C3%A8gre%20tout%20ce%20qu'il%20vous%20faut%20%3A%20performances%20in-memory%20et%20s%C3%A9curit%C3%A9%20avanc%C3%A9e%20pour%20les%20analyses%20au%20sein%20de%20la%20base%20de%20donn%C3%A9es.%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20print(text)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Specify%20source%20and%20target%20language%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20srcLanguage%20%3D%20%22fr%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20targetLanguage%20%3D%20%22en%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Define%20Parameters%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20params%20%3D%20%7B'appid'%3A%20'Bearer%20'%2Btoken%2C%20'text'%3A%20text%2C%20'from'%3A%20srcLanguage%2C%20'to'%3A%20targetLanguage%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20requestHeader%20%3D%20%7B'Accept'%3A%20'application%2Fxml'%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Invoke%20Cognitive%20Services%20to%20perform%20translation%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20responseResult%20%3D%20requests.get(translateUrl%2C%20params%3Dparams%2C%20headers%3DrequestHeader%20)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%23%20Show%20original%20and%20target%20text%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20print(colored('Original%20Text%5Cn'%2C%20'green'))%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20print(colored(text%2C'green'))%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20print%20(%22%5Cn%22)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20print(colored('Translated%20Text%5Cn'%2C%20'blue'))%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20soup%20%3D%20BeautifulSoup(responseResult.text%2C%22lxml%22)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20print(colored(soup.get_text()%2C%20'blue'))%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20Using%20Azure%20Notebooks%20(Jupyter)%20%3CBR%20%2F%3E%20see%20%3CA%20href%3D%22https%3A%2F%2Fnotebooks.azure.com%2Fn%2FZzbLkrUnDWs%2Fnotebooks%2FCognitive%2BServices%2BTranslation.ipynb%22%20title%3D%22https%3A%2F%2Fnotebooks.azure.com%2Fn%2FZzbLkrUnDWs%2Fnotebooks%2FCognitive%2BServices%2BTranslation.ipynb%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fnotebooks.azure.com%2Fn%2FZzbLkrUnDWs%2Fnotebooks%2FCognitive%2BServices%2BTranslation.ipynb%20%3C%2FA%3E%20%3CBR%20%2F%3E%20Translate%20text%20using%20C%23%2C%20dotnet%20core%20and%20Translation%20API%20%3CBR%20%2F%3E%20Using%20a%20simple%20%3CSTRONG%3E%20dotnet%20core%20%3C%2FSTRONG%3E%20application%20and%20the%20command%20line%20tools.%26nbsp%3B%20Visual%20Studio%20code%20and%20the%20command%20line%20(but%20the%20code%20would%20by%20sure%20also%20work%20in%20a%20.net%204.x%20Console%20Application%20in%20VS2017)%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20mkdir%20TranslationApp%20%23%20create%20a%20directory%20for%20the%20solution%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20cd%20TranslationApp%20%23%20Goto%20the%20TranslationApp%20Dir%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20dotnet%20new%20console%20%23%20create%20a%20new%20dotnet%20core%20console%20application%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20dotnet%20restore%20%23%20restore%20dotnet%20dependencies%20and%20tools%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20dotnet%20build%20%23%20build%20the%20application%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20code%20Program.cs%20%23%20edit%20the%20file%20in%20Visual%20Studio%20Code%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F94849iADC92794D22C76C5%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Copy%20and%20paste%20the%20following%20C%23%20console%20sample%20to%20the%20Program.cs%20and%20replace%20the%20APIKEY%20with%20the%20one%20that%20you%20got%20from%20the%20Azure%20portal%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20Program.cs%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20using%20System%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20using%20System.Net.Http%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20using%20System.Net.Http.Headers%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20using%20System.Threading.Tasks%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20using%20System.Xml.Linq%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20namespace%20TranslationService%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20class%20Program%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20static%20readonly%20string%20APIKEY%20%3D%20%22APIKEY%22%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20static%20readonly%20string%20TRANSLATETO%20%3D%20%22en%22%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20static%20void%20Main(string%5B%5D%20args)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Console.WriteLine(%22Please%20enter%20a%20text%20to%20translate%3A%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20var%20input%20%3D%20Console.ReadLine()%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Task.Run(async%20()%20%3D%26gt%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20var%20accessToken%20%3D%20await%20GetAuthenticationToken(APIKEY)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20var%20output%20%3D%20await%20Translate(input%2C%20TRANSLATETO%2C%20accessToken)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Console.WriteLine(output)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7D).Wait()%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Console.WriteLine(%22Press%20key%20to%20exit!%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Console.ReadKey()%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20static%20async%20Task%3CSTRING%3E%20Translate(string%20textToTranslate%2C%20string%20language%2C%20string%20accessToken)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20string%20url%20%3D%20%22%3CA%20href%3D%22http%3A%2F%2Fapi.microsofttranslator.com%2Fv2%2FHttp.svc%2FTranslate%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fapi.microsofttranslator.com%2Fv2%2FHttp.svc%2FTranslate%3C%2FA%3E%22%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20string%20query%20%3D%20%24%22%3Ftext%3D%7BSystem.Net.WebUtility.UrlEncode(textToTranslate)%7D%26amp%3Bto%3D%7Blanguage%7D%26amp%3BcontentType%3Dtext%2Fplain%22%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20using%20(var%20client%20%3D%20new%20HttpClient())%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20client.DefaultRequestHeaders.Authorization%20%3D%20new%20AuthenticationHeaderValue(%22Bearer%22%2C%20accessToken)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20var%20response%20%3D%20await%20client.GetAsync(url%20%2B%20query)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20var%20result%20%3D%20await%20response.Content.ReadAsStringAsync()%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20if%20(!response.IsSuccessStatusCode)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20return%20%22ERROR%3A%20%22%20%2B%20result%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20var%20translatedText%20%3D%20XElement.Parse(result).Value%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20return%20translatedText%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20static%20async%20Task%3CSTRING%3E%20GetAuthenticationToken(string%20key)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20string%20endpoint%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Fapi.cognitive.microsoft.com%2Fsts%2Fv1.0%2FissueToken%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fapi.cognitive.microsoft.com%2Fsts%2Fv1.0%2FissueToken%3C%2FA%3E%22%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20using%20(var%20client%20%3D%20new%20HttpClient())%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20client.DefaultRequestHeaders.Add(%22Ocp-Apim-Subscription-Key%22%2C%20key)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20var%20response%20%3D%20await%20client.PostAsync(endpoint%2C%20null)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20var%20token%20%3D%20await%20response.Content.ReadAsStringAsync()%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20return%20token%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FSTRING%3E%3C%2FSTRING%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F94850i2C52AD65F5C8A1F8%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Save%20it%20and%20then%20we%20can%20run%20the%20application%20via%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CCODE%3E%0A%20%20%20dotnet%20run%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F94851i2A0F1B802E11EC4F%22%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Additional%20information%0A%20%20%3CBR%20%2F%3E%0A%20%20Microsoft%20Cognitive%20Services%0A%20%20%3CA%20href%3D%22http%3A%2F%2Faka.ms%2Fcognitive%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%0A%20%20%20http%3A%2F%2Faka.ms%2Fcognitive%0A%20%20%3C%2FA%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Visual%20Studio%20Code%20%E2%80%93%20command%20line%20options%3A%0A%20%20%3CA%20href%3D%22https%3A%2F%2Fcode.visualstudio.com%2Fdocs%2Feditor%2Fcommand-line%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%0A%20%20%20https%3A%2F%2Fcode.visualstudio.com%2Fdocs%2Feditor%2Fcommand-line%0A%20%20%3C%2FA%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-379180%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Aug%2021%2C%202017%20If%20you%20want%20to%20programmatically%20translate%20text%20from%20one%20language%20to%20another%2C%20then%20the%20Translation%20service%20(translation%20api)%20is%20the%20right%20one%20for%20you.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-379180%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Eacademic%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EC%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Ecognitive%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ECognitive%20Services%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EEducation%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eeducator%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Efaculty%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Ejupyter%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ENotebooks%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epython%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Estudent%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Estudents%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ETranslation%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
First published on MSDN on Aug 21, 2017


If you want to programmatically translate text from one language to another, then the Translation service (translation api) is the right one for you. The Microsoft Translation Service just requires an authentication via a Microsoft Cognitive Services API Key and then you can easily translate text from one language to another. By leveraging the scale and power of Microsoft’s AI supercomputer, specifically the Microsoft Cognitive Toolkit, Microsoft Translator now offers neural network (LSTM) based translation that enables a new decade of translation quality improvement. These neural network models are available for all speech languages through the Microsoft Translator Speech API on the try and compare site http://translator.microsoft.com and through the text API by using the ‘generalnn’ category ID.
How do it work?


In this blog post, I’ll quickly describe how to create the translation service and how to use it with Python, Jupyter Notebook, C#, Powershell and Node.js.
Create the translation service in Azure
The translation service is part of the cognitive services and can therefore be found as cognitive service in the Azure portal: For this demo the F0 Free Services is more than capable of supporting the example with over 2Million character being supported.



The creation of the service just takes a few seconds. After the creation, we can check the service and see, that it is globally deployed and it also shows us the endpoint url. The keys can be accessed via the keys link in menu.



Navigate to it and copy one of the keys that are shown there.



Now we are ready to implement a simple python, script  that reads text  and translates it to another language


Translate text using Python


#install additional libraries to add coloured text to output

!pip install termcolor

!pip install bs4

from termcolor import colored

from bs4 import BeautifulSoup

import requests

#Using Python for Text Translation with Microsoft Cognitive Services

# Specify the subscription Key

subscriptionKey = "ENTER YOUR COGNITIVE API KEY"

#Specify URLs for Cognitive Services - Translator Text API

translateUrl = 'https://api.microsofttranslator.com/v2/http.svc/Translate'

cognitiveServiceUrl = 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken'

# Request Access Token

requestHeader = {'Ocp-Apim-Subscription-Key': subscriptionKey}

responseResult = requests.post(cognitiveServiceUrl, headers=requestHeader)

token = responseResult.text

print ("Access Token")

print (token)

# Original Text

text = "Créez des applications intelligentes et stratégiques avec une plateforme de base de données évolutive et hybride qui intègre tout ce qu'il vous faut : performances in-memory et sécurité avancée pour les analyses au sein de la base de données."

print(text)

# Specify source and target language

srcLanguage = "fr"

targetLanguage = "en"

# Define Parameters

params = {'appid': 'Bearer '+token, 'text': text, 'from': srcLanguage, 'to': targetLanguage}

requestHeader = {'Accept': 'application/xml'}

# Invoke Cognitive Services to perform translation

responseResult = requests.get(translateUrl, params=params, headers=requestHeader )

# Show original and target text

print(colored('Original Text\n', 'green'))

print(colored(text,'green'))



print ("\n")

print(colored('Translated Text\n', 'blue'))



soup = BeautifulSoup(responseResult.text,"lxml")

print(colored(soup.get_text(), 'blue'))




Using Azure Notebooks (Jupyter)
see https://notebooks.azure.com/n/ZzbLkrUnDWs/notebooks/Cognitive+Services+Translation.ipynb
Translate text using C#, dotnet core and Translation API
Using a simple dotnet core application and the command line tools.  Visual Studio code and the command line (but the code would by sure also work in a .net 4.x Console Application in VS2017):




mkdir TranslationApp # create a directory for the solution



cd TranslationApp # Goto the TranslationApp Dir



dotnet new console # create a new dotnet core console application



dotnet restore # restore dotnet dependencies and tools



dotnet build # build the application



code Program.cs # edit the file in Visual Studio Code








Copy and paste the following C# console sample to the Program.cs and replace the APIKEY with the one that you got from the Azure portal:

Program.cs


using System;

using System.Net.Http;

using System.Net.Http.Headers;

using System.Threading.Tasks;

using System.Xml.Linq;



namespace TranslationService

{

class Program

{

static readonly string APIKEY = "APIKEY";

static readonly string TRANSLATETO = "en";

static void Main(string[] args)

{

Console.WriteLine("Please enter a text to translate:");

var input = Console.ReadLine();



Task.Run(async () =>

{

var accessToken = await GetAuthenticationToken(APIKEY);

var output = await Translate(input, TRANSLATETO, accessToken);

Console.WriteLine(output);

}).Wait();





Console.WriteLine("Press key to exit!");

Console.ReadKey();

}



static async Task<string> Translate(string textToTranslate, string language, string accessToken)

{

string url = "http://api.microsofttranslator.com/v2/Http.svc/Translate";

string query = $"?text={System.Net.WebUtility.UrlEncode(textToTranslate)}&to={language}&contentType=text/plain";



using (var client = new HttpClient())

{

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

var response = await client.GetAsync(url + query);

var result = await response.Content.ReadAsStringAsync();



if (!response.IsSuccessStatusCode)

return "ERROR: " + result;



var translatedText = XElement.Parse(result).Value;

return translatedText;

}

}



static async Task<string> GetAuthenticationToken(string key)

{

string endpoint = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";



using (var client = new HttpClient())

{

client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key);

var response = await client.PostAsync(endpoint, null);

var token = await response.Content.ReadAsStringAsync();

return token;

}

}

}

}






Save it and then we can run the application via:

dotnet run


Additional information
Microsoft Cognitive Services http://aka.ms/cognitive
Visual Studio Code – command line options: https://code.visualstudio.com/docs/editor/command-line