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.
- Create a Cognitive Service account by providing the details as shown below.
- 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.
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.
Create a Logic App by providing the details as shown below.
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
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.
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
JSON schema for the above payload looks like below
Now use this JSON schema in the Request trigger body as shown below
Next step is to look for Cognitive Service API connector in the managed API list.
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.
Now you need to provide the Text value to the Detect Sentiment API from “text” variable of the Request trigger as shown below.
Next step would be to add Cognitive Service connector for Key Phrases same way we did for Detect Sentiment.
Now we would use the Compose and Response action to send HTTP response for the sentiment and key phrase analysis.
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.
You can also use code view to compose the response message as shown below.
And finally use the output of compose action to send the HTTP response.
So here is complete workflow look like
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:
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.