Transliterate Text with the Translator Text API

Spread the love

Transliteration is a type of conversion of a text from one script to another, that involves swapping letters (thus trans- + liter-) in predictable ways (such as α → a, д → d, χ → ch, ն → n or æ → e). For instance, for the Modern Greek term “Ελληνική Δημοκρατία”, which is usually translated as “Hellenic Republic”, the usual transliteration to Latin script is “Ellēnikḗ Dēmokratía”, and the name for Russia in Cyrillic script, “Россия”, is usually transliterated as “Rossiya”.

In this tutorial we are going to see how to use the Translator Text API to transliterate Text from Japanese to Latin.

Prerequisites

  1. To run the sample code you must have an edition of  Visual Studio installed.
  2. You will need the Json.NET NuGet package.
  3. You will need the .NET SDK installed in your machine
  4. You will need an Azure Cognitive Services account with a Translator Text resource. If you don’t have an account, you can use the free trial to get a subscription key.

Create your Project

To create an application to translate your text follow the steps below:

  1. Create a .NET Core Console Application in Visual Studio 2017
  2. Add the JSON.net nuget package
    Install-Package Newtonsoft.Json
  3. Add the following code under Program
    static void TransliterateText()
          {
              string FromScript = "jpan";
              string ToScript = "latn";
    
              string host = "https://api.cognitive.microsofttranslator.com";
              string route = "/transliterate?api-version=3.0&language=ja&fromScript="+FromScript+"&toScript="+ToScript;
              string subscriptionKey = "enter your subscription key";
    
              System.Object[] body = new System.Object[] { new { Text = @"こんにちは" } };
              var requestBody = JsonConvert.SerializeObject(body);
    
              using (var client = new HttpClient())
              using (var request = new HttpRequestMessage())
              {
                  // Set the method to POST
                  request.Method = HttpMethod.Post;
                  // Construct the full URI
                  request.RequestUri = new Uri(host + route);
                  // Add the serialized JSON object to your request
                  request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
                  // Add the authorization header
                  request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
                  // Send request, get response
                  var response = client.SendAsync(request).Result;
                  var jsonResponse = response.Content.ReadAsStringAsync().Result;
                  // Print the response
                  Console.WriteLine(jsonResponse);
                  Console.WriteLine("Press any key to continue.");
              }
          }
          static void Main(string[] args)
          {
              TransliterateText();
              Console.ReadLine();
          }
  4. Replace the parameters FromScript and ToScript.
  5. Replace your subscription key here: string subscriptionKey = “enter your subscription key”;
  6. Add here the text you want to be translated System.Object[] body = new System.Object[] { new { Text = @”こんにちは” } };
    The following limitations apply:

    • The array can have at most 10 elements.
    • The text value of an array element cannot exceed 1,000 characters including spaces.
    • The entire text included in the request cannot exceed 5,000 characters including spaces.
  7.  Run the program

Get Results

The result is in the following format. That’s it we have transliterated the japanese text to latin!

[ {"text":"konnnichiha","script":"Latn"}]

You can find the complete source code in my Github in this repository in the TextTransliteration Project.

Leave a Reply

Your email address will not be published. Required fields are marked *