Globalize your Bot using multiple languages with LUIS

According to the 2017 Ethnologue, English is no more the most spoken language and Arabic language’s rank is not the same as it was before. Undoubtedly, English still has the significance but interacting with one’s primary language is a different experience that could lead to satisfaction more quickly than with the second language. Therefore, if your target audience is not restricted only to English language then you should now think of globalizing your business.

In this post, I will brief you about how easily we can incorporate the support for multiple languages to boost up your business. The multiple language support has been around for a while but it’s not being used much because of the integrity and translation quality. Earlier, the engine used by Microsoft was based upon Statistical Machine Translation technology. However, they have now moved to Deep Neutral Network (DNN) technology for better and quick responses and with more human like feel. Still, I understand that the translations may not be up to par but the translator engines / APIs are continuously improving which may give you the best results in near future.

For demo purpose, I will be editing the same opensource project as we have been using lately.  You’re always encouraged and welcome to contribute to this so all of this community get benefit from your experiences.

You’re a telco company and you have to interact with your customers in the language of their choice.

I will be using Urdu (because of obvious reasons) and just to surprise you that this language (Hindi / Urdu) is the 4th most spoken language around the globe now.

Bot Conversation in Urdu

Before you kick off, you need to have Translation API key to continue further. Once you have it, then you can easily implement the method as below which just translates the text into the target language and return the text to you.

public static async Task<string> TranslateText(string inputText, string language)
{
string accessToken = await GetAuthenticationToken(Constants.TranslationAPIKey);
string url = "http://api.microsofttranslator.com/v2/Http.svc/Translate";
string query = $"?text={System.Net.WebUtility.UrlEncode(inputText)}&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 "Failed: " + result;

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

Now comes the tricky part of the job. You have a LUIS application which is perfectly trained with an English language (of course, you do not have the support for multiple languages in LUIS on the fly yet) and you want to land your request in the exact same intent as you were expecting in English.

How do we do it then? 

One word: Override!

We will override the MessageReceived method of the LUIS class as this message is called before the request falls into any intent (according to its highest score).

In that method, you just have to the changes as I did.

protected override async Task MessageReceived(IDialogContext context, IAwaitable<IMessageActivity> item)
{
IMessageActivity message = context.Activity.AsMessageActivity();
var response = await Utilities.TranslateText(message.Text, responseLang);
message.Text = response;

//Create a Translating Context to translate any messages that gets sent subsequently.
await base.MessageReceived(context, item);
}

If you still think that the translation is not up to your cultural or organization needs, you can build your own machine translation system using translation Hub. Please note that you can use whatever translation API you want.  The code is updated at the repository and since it’s just there for demo purpose so you may not witness the quality of exception handling and other best practices implemented there.

Until next time.