dcsimg

Working with Microsoft Cognitive Text Analytics

WEBINAR:
On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Introduction

Microsoft Cognitive Text Analytics API is a Cloud-based machine learning service that provides advanced natural language processing. The Text Analytics service provides sentiment analysis, key phrase extraction, and language detection. This API supports a total of 120 languages. The Text Analysis API returns the detected language details and a numeric score between 0 and 1; if the score is close to 1, that indicates 100% certainty that the identified language is true.

Text Analytics API

The Text Analytics API is used to analyze unstructured text, sentiment analysis, key phrase extraction, and language detection. To test the API, open the Microsoft Cognitive service Web site and type some sample text. In response, the Text Analysis API returns sentiment, key phrases, and language details. This is shown in Figure 1.

Cognitive Text Analysis Request and Response
Figure 1: Cognitive Text Analysis Request and Response

For developers, you can get a JSON response, as shown in the following code snippet.

{
   "languageDetection": {
      "documents": [
         {
            "id": "4bd6d1bf-5ca3-4f04-9937-f291d2727a2c",
            "detectedLanguages": [
               {
                  "name": "English",
                  "iso6391Name": "en",
                  "score": 1.0
               }
            ]
         }
      ],
      "errors": []
   },
   "keyPhrases": {
      "documents": [
         {
            "id": "4bd6d1bf-5ca3-4f04-9937-f291d2727a2c",
            "keyPhrases": [
               "Human"
            ]
         }
      ],
      "errors": []
   },
   "sentiment": {
      "documents": [
         {
            "id": "4bd6d1bf-5ca3-4f04-9937-f291d2727a2c",
            "score": 0.78137826919555664
         }
      ],
      "errors": []
   },
   "entities": {
      "documents": [
         {
            "id": "4bd6d1bf-5ca3-4f04-9937-f291d2727a2c",
            "entities": [
               {
                  "name": "Mars",
                  "matches": [
                     {
                        "text": "Mars",
                        "offset": 23,
                        "length": 4
                     }
                  ],
                  "wikipediaLanguage": "en",
                  "wikipediaId": "Mars",
                  "wikipediaUrl": "https://en.wikipedia.org/wiki/
                     Mars",
                  "bingId": "7a1af859-79db-9e2e-bf8a-465c092243cc"
               }
            ]
         }
      ],
      "errors": []
   }
}

Azure Cognitive Text Analytics API

Microsoft Azure provides a Cloud-based Text Analysis API for developers. To register a Cognitive Services API account for the Text Analytics API, open https://azure.microsoft.com/en-in/try/cognitive-services/?api=text-analytics and click the 'Get API Key' link (see Figure 2).

Cognitive Text Analysis Get API Key
Figure 2: Cognitive Text Analysis Get API Key

Next, select the appropriate account type applicable for you, as shown in Figure 3. You can use the free tier for 5,000 transactions/month.

Selecting the Cognitive Text Analysis Plan
Figure 3: Selecting the Cognitive Text Analysis Plan

After you've selected an account type, create a new Azure account if you don't have already. Otherwise, you can use an existing one (see Figure 4).

Cognitive Text Analysis Select/Create Azure Account
Figure 4: Cognitive Text Analysis Select/Create Azure Account

After a successful login, you will landed at the Azure Cognitive service portal; it will ask you to provide a service name and other details. After entering all the required details, click Create, as shown in Figure 5. The system will create the Cognitive Text Analytics API endpoint for you and redirect you to the overview tab.

Cognitive Text Analysis Create Service Endpoint
Figure 5: Cognitive Text Analysis Create Service Endpoint

Copy the subscription name, endpoint, and two keys in Notepad. Those details will be required while consuming the service from code. See Figures 6 and 7.

Cognitive Text Analysis Copy Subscription
Figure 6: Cognitive Text Analysis Copy Subscription

Cognitive Text Analysis Copy Keys
Figure 7: Cognitive Text Analysis Copy Keys

After you've registered your Azure service, let's create a new console project in Visual Studio.

To create a Sample application in .NET, Open Visual Studio -> File Menu -> New, then Project. It will open a new project window. Choose the Console Application type. Specify the project name "TextAnalyticsSample" and click OK.

Right-click the solution and select Manage NuGet Packages for Solution. Select the Include Prerelease check box. Select the Browse tab, and search for Microsoft.Azure.CognitiveServices.Language. Finally, select the NuGet package and install it.

Copy and paste the following code snippet to test the Text Analytics API created in the previous step.

using System;
using System.Net.Http;
using System.Threading;
using System.Net.Http.Headers;


namespace TextAnalyticsSample
{
   class Program
   {
      static void Main()
      {
         CallTextAnalyticsAPI();
         Console.WriteLine("Hit ENTER to exit...");
         Console.ReadLine();
      }

      static async void CallTextAnalyticsAPI()
      {
         var client = new HttpClient();
         var queryString =
            HttpUtility.ParseQueryString(string.Empty);
         // Request headers
         client.DefaultRequestHeaders.Add("2d882e79-b33c-4365-8068
            -62f32c95db95", "1be1210e18934958a79b96531a5da300");

         var uri = "https://eastus.api.cognitive.microsoft.com/
            text/analytics/v2.0?" + queryString;

         HttpResponseMessage response;

         // Request body
         byte[] byteData = Encoding.UTF8.GetBytes("{body}");

         using (var content = new ByteArrayContent(byteData))
        {
            content.Headers.ContentType = new MediaTypeHeaderValue
               ("{ "documents": [  {  "id": "1",
               "text": "Hello world"
           }
           }
           ");
            response = await client.PostAsync(uri, content);
         }

      }

   }
}

Conclusion

Cognitive Text Analyzer has a new preview version of the Entity Linking API that takes unstructured text. Also, for each JSON document, it returns a list of disambiguated entities with links to more information of the Wikipedia and Bing.

This Cognitive Services How-to link will give you more details.

That's all for today. Happy coding!



About the Author

Tapas Pal

I am working in Microsoft Technology for last 15 years and presently working with Cognizant Technology Solutions, India as Senior Architect. I have completed TOGAF 9.1,Microsoft Certification on .NET 1.1 , .NET 2.0, SQL Server 2005 and Sharepoint. Please visit my Blog - http://tapas-pal.blogspot.com/

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date