Logic App to detect sentiment and extract key phrases from your text

Microsoft’s Cognitive services provides set of powerful intelligence APIs. These APIs can be integrated into your app on the platform of your choice to tap into ever-growing collection of powerful artificial intelligence algorithms for vision, speech, language, knowledge and search.

Integrating Cognitive Services into an application provides the app with the ability to SEE, RECOGNIZE, HEAR and even understand the SENTIMENT of your text.

In this blog post, I am trying to experiment with the Text Analytics API in Logic App. The API is a suite of text analytics services built with Azure Machine Learning to evaluate sentiment and topics of text to understand what user want.

For Sentiment analysis the API returns a numeric score between 0 and 1. Scores close to 1 indicate positive sentiment and scores close to 0 indicate negative sentiment. For Key phrase extraction the API returns a list of strings denoting the key talking points in the input text.

Cognitive Service account for the Text Analytics APIs

To build to Logic App to use Text Analytics APIs, first you need to sign up of the text analytic services.

  • Login to Azure Portal with your valid MSDN Subscription and Search for Cognitive Services APIs.

image.png

  • Create a Cognitive Service account by providing the details as shown below.

image.png

  • Make sure Text Analytics is selected as the ‘API type’ and select free plan – free tier for 5,000 transactions/month
  • Complete the other fields and create your account.
  • After you sign up for Text Analytics, find your API Key. Copy the primary key, as you will need it Logic App.

image.png

 

Logic App to detect sentiment and extract key phases

Logic Apps is a cloud-based service that you can use to create workflows that run in cloud. It provides a way to connect your applications, data and SaaS using rich set of connectors. If you are new to Logic App, please refer the Azure documentation for further details.

Now let’s create a Logic App to detect sentiment and extract key phrases from user’s text using the Text Analytic API.

Go to New >Enterprise Integration and select Logic App as shown below.

image.png

Create a Logic App by providing the details as shown below.

image.png

After our deployment success, we can start editing our Logic App.

To access it, in your left, browse All Resources > [Name of your Logic App].

Clicking in your Logic App will open the Logic Apps Designer. In welcome screen, there are a lot of templates ready to use. Choose a blank template from Logic Apps Designer

image.png

On Logic App designer, a search box is available where you can look for available Microsoft managed connectors and APIs available. Select the Request from the list which would act as a trigger to your Logic App and can receive incoming request.

image.png

Now we need to define a request body JSON Schema and the designer will generate tokens to parse and pass data from the trigger through the workflow.

We can use a tool like jsonschema.net to generate a JSON schema from a sample body payload

image.png

JSON schema for the above payload looks like below

image.png

Now use this JSON schema in the Request trigger body as shown below

image.png

Next step is to look for Cognitive Service API connector in the managed API list.

image.png

Select the Detect Sentiment  and provide a connection name and Cognitive Service Account Key which we have copied in the previous section and click on Create.

image.png

Now you need to provide the Text value to the Detect Sentiment API from “text” variable of the Request trigger as shown below.

image.png

Next step would be to add Cognitive Service connector for Key Phrases same way we did for Detect Sentiment.

image.png

Now we would use the Compose and Response action to send HTTP response for the sentiment and key phrase analysis.

image.png

This is how I have composed the response using a simple new JSON message using the variable “key phrase” and “score” from the Key Phrase and Detect Sentiment APIs.

image.png

You can also use code view to compose the response message as shown below.

image.png

And finally use the output of compose action to send the HTTP response.

image.png

So here is complete workflow look like

image.png

Quick and easy! Now once you save the workflow the topmost Request trigger will have the URL for this particular Logic App.

Now let’s invoke this Logic App from one of my favorite API testing tool, Postman.

I submitted the sample JSON message to the endpoint with following text – I had a wonderful experience! Azure cognitive services are amazing.

Sure enough, I got the key phrase and sentiment score as below:

image.png

image.png

Conclusion

Clearly Microsoft’s Cognitive Services are easy to use in your app on the platform of your choice. The Text Analytics API is just one of many different Artificial Intelligence APIs provide by Microsoft. I am sure this new platform would mature in the coming days and different types of app can leverage this technology.

 

References:

https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-text-analytics-quick-start

https://social.technet.microsoft.com/wiki/contents/articles/36074.logic-apps-with-azure-cognitive-service.aspx

Advertisements

2 thoughts on “Logic App to detect sentiment and extract key phrases from your text

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s