Perform Text Analysis on a File in Blob Storage via Azure Functions

Azure Cognitive Service provides a set of machine learning algorithms which helps developers to solve problems in the field of Artificial Intelligence (AI). It enhances developer’s capabilities to create, manage, and deploy powerful applications to assist cloud customers perform any number of tasks.

In this article, we would perform Text Analytics on contents of a file in blob storage. The Text Analytics API is a cloud-based service that provides advanced natural language processing over raw text provides APIs for sentiment analysis, key phrase extraction, language detection, and named entity recognition among others

Process Flow

As shown in the figure, we will create a Blog Trigger Function which would get triggered on File upload to a Container Folder in Blob Storage Account. The Function would read the content of the folder and call Azure Cognitive Service – Text Analytics API. The output from the API will be saved into the output folder of the container by the function.

Provision Azure Cognitive Service – Text Analytics

Provision Text Analytics Service to invoke client APIs and analyze the contents of file

  • Login to Azure Portal
  • Search for Text Analytics and Click on Text Analytics under Marketplace
  • Provide Name and Click on Create
  • Go to Resource > Keys and Endpoint. Copy Values of Key and Endpoint. These values would be configured for calling the APIs

Provision Storage Account

Provision a blob storage account to upload files which need to be processed and analyzed.

  • Login to Azure Portal
  • Search for Storage Accounts
  • Click on Add
  • Provide details and click on Review + Create
  • Go to resources and click on containers.
  • Click on + Container
  • Provide name for Container and click on Create

Create Azure Function

Create Azure Function to deploy blob triggers. These trigger will get invoked when a file is uploaded to blob storage.

  • Open Visual Studio
  • Click on Create a New Project

  • Search for Azure Functions Template
  • Select the template and click on Next
  • Provide Project Name and click on Create
  • Select “Blob Trigger”. Configure the storage account created in previous steps. Provide the Connection String Name and the container details in the path. Click on Create.

Add Cognitive Service Nuget Package

  • Right Click on Project > Manage Nuget Packages
  • Search for “Microsoft.Azure.CognitiveServices.Language.TextAnalytics”. Select and Click on Install

Create Azure Cognitive Services Client

  • Right Click on Project > Add > New Item…
  • Select “Class”, provide name and click Add
  • Update the KeyPhraseExtraction.Cs with below Code.

In this code we have created a static Run method to create a Text Analytics Client and  invoke KeyPhases() method.

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
 
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics;
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models;
using Microsoft.Rest;
 
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
 
namespace DemoFuntionApp
{
    public static class KeyPhraseExtraction
    {
        public static StringCollection Run(string endpoint, string key, string fileContent)
        {
            try
            {
                var credentials = new ApiKeyServiceClientCredentials(key);
                var client = new TextAnalyticsClient(credentials)
                {
                    Endpoint = endpoint
                };
 
                var kpResults = client.KeyPhrases(fileContent);
                StringCollection kpoutput = new StringCollection();
                //Printing keyphrases
                foreach (string keyphrase in kpResults.KeyPhrases)
                {
                    kpoutput.Add(keyphrase);
                }
 
                return kpoutput;
 
            }
            catch (Exception ex)
            {
                StringCollection kpoutput = new StringCollection();
                kpoutput.Add(ex.Message);
                return kpoutput;
 
            }
        }
    }
    class ApiKeyServiceClientCredentials : ServiceClientCredentials
    {
        private readonly string subscriptionKey;
 
        /// <summary>
        /// Creates a new instance of the ApiKeyServiceClientCredentails class
        /// </summary>
        /// <param name="subscriptionKey">The subscription key to authenticate and authorize as</param>
        public ApiKeyServiceClientCredentials(string subscriptionKey)
        {
            this.subscriptionKey = subscriptionKey;
        }
 
        public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
 
            request.Headers.Add("Ocp-Apim-Subscription-Key", this.subscriptionKey);
 
            return base.ProcessHttpRequestAsync(request, cancellationToken);
        }
    }
}

Update the Azure Function

  • Right Click on the Function and rename it.
  • Update below code in the Function.

The below code triggers on any file upload to the Input folder in the container. Reads the content of the file and calls the Run method on the KeyPhraseExtraction class. The output of the method is saved in the output folder of the container

using System;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using System.Collections.Specialized;
 
namespace DemoFuntionApp
{
    public static class KeyPhrasesExtractionFn
    {
        public static string Endpoint = "";
        public static string SubscriptionKey = "";
 
        [FunctionName("KeyPhrasesExtractionFn")]
        public static void Run([BlobTrigger("democontainer/input/{name}", Connection = "democonn")] Stream myBlob,
            [Blob("democontainer/output/{name}", FileAccess.Write)] Stream validationOutput, string name, ILogger log,
             ExecutionContext context)
        {
 
            log.LogInformation($"Text Analytics - Key Phrase Extracton blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
            StreamReader reader = new StreamReader(myBlob);
            string fileContent = reader.ReadToEnd();
            StringCollection kpOutput = KeyPhraseExtraction.Run(Endpoint, SubscriptionKey, fileContent);
            using (var sw = new StreamWriter(validationOutput))
            {
                try
                {
                    sw.WriteLine("Key Phrases in the Text:");
                    foreach (string dir in kpOutput)
                    {
                        sw.WriteLine(dir);
                    }
 
                    sw.Flush();
                }
                catch (Exception ex)
                {
                    log.LogInformation(ex.Message);
                }
            }
        }
    }
}

In addition to Key Phrase Extraction , Text Analytics Service provides APIs for Sentiment Analysis, Entity Recognition and Language detection as well. This solution can be scaled up to implement these APIs by adding respective functions and service clients. Try to implement these APIs and explore other facets of Azure Cognitive Services.

Happy Coding!!!



Categories: Azure, Azure Cognitive Service, Azure Functions

Tags: , , , ,

2 replies

  1. hi,
    while i run this program getting error democonn not exists in ‘local.settings.json’. could you please help me in getting this sorted.

    • please add below to your local.settings.json file and replace the values with actual ones

      “democonn”: “server=sqlserver.database.windows.net;database=databasename;uid=abc@xyz.com;password=pwd;”,

Leave a Reply to WizardCancel reply

Discover more from Cloud Wizard Inc.

Subscribe now to keep reading and get access to the full archive.

Continue reading