In this article will explore role of Cognitive Text analytics service in Microsoft Azure Logic Apps for detecting text sentiments.
- Microsoft Azure Subscription – Sign up for Azure Free account
- Basic knowledge of Azure Logic Apps
- Basic knowledge of Cognitive Services.
- Zeal to learn.
Azure logic App helps to build business workflow integrating different systems be it on cloud or on premises, with no code & easy to go designer tools. Azure Logic App is Microsoft Azure service offering for Enterprise Integration. It comes with managed API connectors to communicate with system. This connector act as ‘Triggers’ & ‘Action’.
- Trigger – This are conditions or entry point of Workflow to get start with. For example, a new Tweet is posted or mail is received, depends on connectors.
- Action-This connector act as action on data received or posted by Workflow Trigger connectors. For example, if mail is received (trigger), create service ticket or send notification to owner or concerned team member (action).
Cognitive Text Analytic API also plays a role as connector in Azure Logic App to leverage cognitive powers to business workflows. As of now, it provides three actions, all in preview,
- Text Analytics API – Detect Language
- Text Analytics API – Detect Key phrase
- Text Analytics API – Detect Sentiments
In this article as mentioned above we will be looking in to, Text Analytics API – Detect Sentiments
As name suggest API analyses Text as Data and returns the sentiments of Text. This response comes in score from 0 to 1, 0 being most harsh or disappointed to 1 with the happiest. On this score we can recognize the user sentiments while communicating.
Let’s understand it better with the help of Use case.
Consider we have an event organised. In this event we are welcoming feedback, event experiences from attendees through Twitter with a predefined hashtag. Now, this tweet can be with different sentiments like positive or negative tweets. Its tough to review each & every tweet before displaying it on any application or Kiosk.
Here will create a Logic App workflow to review this tweet. Once user Tweets with defined Hashtag, will pass this Tweet to Text Analytics API to analyse & detect sentiment of Tweeted message. If its seems to be negative or harsh as per score return, will pass this Tweet to admin or reviewer team via mail notification. If fine, then could be processed further.
Let’s have this workflow in action!
Building Workflow with Text Analytics API
Open Microsoft Azure portal
Click New=>Enterprise Integration=>LogicApp
Enter required details,
- Name – Name of LogicApp workflow, its mandatory. Provide any valid name. Should be unique only across your subscription. For this article its name as, detectseniment-logicapp-demo.
- Subscription – Microsoft Azure Subscription, its mandatory
- Resource Group -LogicApp, a Azure resource must be clubbed with Resource group. Can create new or select from existing resource in subscription, its mandatory. For this article, created new with name as cognitive-logicapp-demo.
- Location – Location where the service will get deployed. For this article, region selected is North Central US
Entering all required details, Pin to Dashboard (best practice) and,
After services is deployed, it will open up Logic App Designer Window. It comes with predefined templates for building Workflows, along with blank Template.
Select Blank Logic App Template,
As per Use Case, Select Twitter connector as Trigger.
Login with Twitter account credentials, & authorize Azure Apps for communicating with your twitter account details.
Note: Read all permission & details before authorizing.
Enter any defined Hashtag, for Twitter to search & trigger the workflow.
For this article, we have used #TextDetectSentiments as Hashtag.
Go with default frequency for Trigger check, i.e. after every three minutes.
Click on Save.
Now, when a new tweet is posted,
Now, click on +New step=>Add an Action
As an action connector, here we will be using Cognitive Text Analytics API. The tweet posted will be passed to Text analytics API to detect the sentiments of the Tweet, and accordingly we will process the Tweet.
Search for Text analytics.
Under Action, Select Text Analytics – Detect Sentiments.
For the brevity of this article, I will not go with each step it takes to create Cognitive Text Analytics accounts suing Azure Portal.
In Azure Portal => Click New=>AI + Cognitive Services=>Text Analytics API
Enter required details like name, subscription, region, resource group along with pricing plan.
Once account is created, go to Keys section & copy Name along with anyone among Key1 or Key2
Keep this detail handy as we need to enter this in Text Analytics connector for leveraging cognitive service in Azure Logic App workflow.
Enter above details in Text Analytics connector,
- Name as Connection Name
- Key as Account Key
- Site URL – Can be kept as default, or we can copy it from,
Text Analytics created account => Overview section => Under Essentials -Copy Endpoint URL
Click on Create.
This will create connection between Azure Logic App Text Analytics API connector & Text analytics account. For this article, we have ‘demo-text-cognitive’ as Text Analytics account, which is now connected with this connector. Refer below Image.
We need to pass Text that’s need to be pass through Text analytics service for detecting its sentiments.
Click on Text Box against field Text, you can see a window appears at Right hand corner with few parameters. These parameters are nothing but output from earlier connectors in Workflow, which can be used for further processing.
Here it will list all possible output parameters from earlier Twitter connector. As we need to detect the sentiment of tweeted Text,
Select ‘Tweet Text’
Click on Save.
Next, we need to add a condition over here.
As mentioned earlier, Text analytics API returns a score on detecting sentiments. Score with near to 0 are Harsh or negative sentiments. And score near to 1 are the happiest or satisfies to go with.
Text Analytics connector will return a Score, which we can use in our Workflow condition, so as to decide the further action or implementation of the Flow.
Click on +New step=>Add a Condition
Here you will get three fields to enter as conditions.
- First Option – This will be the value to be compared or use in condition. For this article, this will be Score being returned from earlier connector i.e. Text Analytics Connector. Once you click on first Text box, there will be window at right hand corner with all possible outputs from all connectors used earlier in flow. Select Score, as we need to decide the further action based on this Score.
- Second Option – This will be used as condition between First Option and Third Option. Like, If First Option value => Is greater than or less than (second option) => Third option value. This is basically a drop down with different conditional statements. For this article will select ‘is less than’.
- Third Option – This will be the Value; first option value will get compared with. Again, this can be from earlier parameters or any custom value. As per our Use Case, we need to review negative tweets. For this we will provide value as 0.4.
With all above options our final conditional statement will go as,
If SCORE =>Is less than =>0.4,
Then it should send a mail to any configurable email id say of reviewer to review the Tweets before processing it further.
As per above workflow, this mail needs to be send when the condition comes to True. And if condition is false concludes Tweeted Text is non-negative.
Again, for brevity of this article, will focus on any one condition i.e. of True.
As shown in below image,
Under If true box => Click Add an Action
Here will send mail to reviewer or admin about the negative Tweet to take further action.
For sending mail, there are multiple connectors on Azure Logic Apps.
For this article we will using Outlook.Com connector as action i.e. for sending mail.
Select Outlook.com=>Action as Send an email
Again, connection process remains the same, login with your outlook.com account credentials, and authorize Azure Apps service to communicate & talk with Outlook.com APIs.
Enter required details for Outlook.com connector for sending mail.
- To – Email ID, mails need to be sent. For this article, we gave it as ‘firstname.lastname@example.org’. You can even give multiple email ids over here. As per Use Case this email will be of Reviewer.
- Subject – Subject of mail. Any custom text can be used here. Also, any output variable from above connector can be used. For this article, we gave Subject as “Review tweet”.
- Body – Body of mail to be sent. Same goes here as Subject. For this article, we gave it as, “Please review Tweet, Tweet as parameter from earlier Twitter connector, Tweeted by, Tweeted by”parameter from earlier Twitter connector.
Above three are mandatory fields. There are more options you can decorate your mail, but Outlook.com connector is out of scope for this article.
Enter all details, and Click Save.
Enter all details, Click Save&Run to start Logic App Workflow.
Testing Logic App workflow
Following all above mentioned steps, completes out workflow building. Its time to Test our flow.
Tweet some harsh statement using Hashtag we entered in Twitter connector used as Trigger.
Note: This Tweet is only for learning purpose.
This should now trigger our Workflow.
Go to Logic App Overview section
Here clicking on,
- Run Trigger – Manually or OnDemand check for workflow Trigger condition.
- Refresh – Refresh data for Run History & Trigger History.
- Edit – Opens the designer to edit workflow.
- Delete – Deletes Logic App account.
- Disable – Will disable Workflow.
Run History shows the successful execution of our Workflow along with total time taken.
Trigger History shows the details of triggered activity. If it doesn’t find trigger condition to be true, its states as ‘Skipped’ and if its found to be true, it states as ‘Succeeded’.
As per our tweet, it was successfully triggered.
Refer below Image.
Click on record under Run History,
Here we can see our Workflow is success. It also mentions time taken by each connector along with input and output details.
Click on Text Analytics API – Detect Sentiment connector for checking it details,
Notice the Score being given out as response, less than 0.4
Also, time taken for detecting sentiments was very much quick.
Hence our condition was true and mail was sent as configured.
Check Mail account, where mail was supposed to be sent,
Mail received with Tweet details,
Now, let’s check with some Happy going text and directly see the output.
Text Analytics API – Detect Sentiment connector output,
Score greater than 0.4, and hence condition remains false, with no mail sent for reviewing.
Azure Logic Apps helps building complex workflows with ease & no coding involved. And Cognitive offering help to make this workflow smarter by leveraging its features. Text Analytics API, at the time of writing this article is in preview.
We learn how to implement Text Analytics API – Detect sentiment as connector in Azure Logic Apps work flow, in next article will learn about Text Analytics API – Detect Language implementation again with an interesting Use case.
ASP.net developers India would recommend to try above mentioned steps with different use case & do let us know your feedback or queries.