Language Detection using Text Analytics API 2.0 Cognitive Service

Rajnilari2015
Posted by in Azure category on for Beginner level | Points: 250 | Views : 13689 red flag
Rating: 5 out of 5  
 6 vote(s)

In this article we will look into how to detect language by using Text Analytics API 2.0.

Introduction

Text Analytics API is a cloud-based web service supported by Microsoft Cognitive Services that provides advanced natural language processing over raw text and includes three main functions: language detection, key phrase extraction, sentiment analysis

This API does not need any training dataset.In it's present state, the API is able to detect 120 languages.

In this article we will look into how to detect language by using Text Analytics API 2.0.

Let's do the experiment

Step 1

Let us first get the Text Analytics API key(s)

Step 2

Open Visual Studio 2017 and fire a console application

Step 3

From the Nuget package manager console, fire the below

PM > Install-Package Microsoft.Azure.CognitiveServices.Language -Version 1.0.0-preview

Step 4

Let us write the below code

using Microsoft.Azure.CognitiveServices.Language.TextAnalytics;
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace textanalytics
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize a new instance of the TextAnalyticsAPI class
            ITextAnalyticsAPI client = new TextAnalyticsAPI();

            //set the endpoint region.
            client.AzureRegion = AzureRegions.Westcentralus;

            //set the subscription key
            client.SubscriptionKey = "YOUR SUBSCRIPTION KEY";

            client
                //The API returns the detected language and a numeric score between 0 and 1.
                .DetectLanguage(
                     new BatchInput(
                         new List<Input>()
                         {
                          new Input("1", "I am a boy"),
                          new Input("2", "je suis un garçon"),
                          new Input("3", "??? ???"),
                          new Input("4","??? ???? ????"),
                          new Input("5", "??????"),
                          new Input("6", "Jeg er en dreng"),
                          new Input("7", "?? ?????"),
                          new Input("8","sono un ragazzo"),
                          new Input("9", "Ich bin ein Junge"),
                          new Input("10", "soy un chico"),
                          new Input("11", "jag är en pojke"),
                          new Input("12", "??????"),
                          new Input("13", "??? ?? ????? ???"),
                          new Input("14", "e?µa? a????")
                         }))

                         //gets the documents collection
                         .Documents
                         .ToList()

                         //display the document result
                         .ForEach(i =>
                                     Console.WriteLine("" +
                                     "ID: {0} ," +
                                     " Language: {1}, " +
                                     " Iso6391Name : {2}, " +
                                     " Confidence Level : {3}",
                                     i.Id, 
                                     i.DetectedLanguages[0].Name, 
                                     i.DetectedLanguages[0].Iso6391Name,
                                     i.DetectedLanguages[0].Score
                                ));
            Console.ReadKey();
        }
    }
}

First we create a new instance of the TextAnalyticsAPI class

ITextAnalyticsAPI client = new TextAnalyticsAPI();

We then provide the language text in the DetectLanguage API that returns the detected language and the level of confidence which is a value that lies between 0-1.

Step 5

Now let's run the application.

For our experimental sake, we have supplied text in 14 different languages and the Text Analytics API is able to detect all of them with a confidence level of 1.That's amazing!!!.

Reference

  1. Text Analytics API
  2. Text Analytics API Version 2.0

Conclusion

In this article we learnt Language Detection using Text Analytics API 2.0 Cognitive Service with examples.Hope this helps. Thanks for reading.

Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Posted by: Laurielewis042 on: 8/31/2018 | Points: 25
very nice I am impressed
Posted by: Margaretberck on: 7/29/2019 | Points: 25
Great! Thank you for this step-by-step guide.

Login to post response

Comment using Facebook(Author doesn't get notification)