Text Analytics with Azure Cognitive Service Complete guide for Power User – Part 1

In this blog series, we will try to understand the basics of Azure Microsoft Cognitive Services: Text Analytics API with practical example’s which should help you leverage the Power of Text Analytics API with the platform of your choice.

This blog series is focused towards Power-User’s so we will be using the Platforms\Tools below:

Before getting into the example, let first understand ‘Why use Cognitive Service‘?

Cognitive Services bring AI within reach of every developer, power-user, IT-admin —without requiring machine-learning expertise. All it takes is an API call to embed the ability to see, hear, speak, search, understand and accelerate decision-making into your apps.

In this blog we focus on Text Analytics API – Using this AI service we can uncovers insights such as sentiment, entities and key phrases in unstructured text.

I would recommend you visiting the page and try out Text Analytics by typing yourself using the User-Interface tool provide my Microsoft(screenshot below).

In the above screenshot you see that I have typed and  Azure Microsoft Cognitive Services: Text Analytics API has analyzed the text and provided us a with a response.

In the response we it has detected(Three Aspects of Text Analytics API).

  • Sentiment: Use sentiment analysis to find out what customers think of your brand or topic by analyzing raw text for clues about positive or negative sentiment. This API returns a sentiment score between 0 and 1 for each document, where 1 is the most positive.

In the above example, sentence we typed “This is good blog and I highly recommend it!”, the system would detect that my sentiment score was high, meaning very close to 1, maybe like. 95, and that would indicate to me that this sentence has a positive sentiment.

  • Key Phrases: Automatically extract key phrases to quickly identify the main points. For example, for the input text “The food was delicious and there were wonderful staff”, the API returns the main talking points: “food” and “wonderful staff”.

Again, in the above example, sentence we typed “This is good blog and I highly recommend it!” We would  want to know what specifically you are happy about. In this case, it would be the “good blog”.

  • Language: You can detect which language the input text is written in and report a single language code for every document submitted on the request in a wide range of languages, variants, dialects, and some regional/cultural languages. The language code is paired with a score indicating the strength of the score.

Now that we have a basic high level understand of Text Analytics API, let explore it:

POSTMAN Example:

To better understand Azure Microsoft Cognitive Services: Text Analytics API let use “Postman“. 

Accept:application/json
Ocp-Apim-Subscription-Key:xxx-xxxx-xxxx-xxxxx
  • Body:
	{
	  "documents": [
	    {
	      "countryHint": "US",
	      "id": "1",
	      "text": "This is good blog and I highly recommend it!"
	    }
	  ]
}
  • Click on “Send“.

Postman Response:

You can similar try the “Language” and “Key Phrase” with reference to the artilce.


PowerShell Sample:

  • Copy and paste the above script in your PowerShell ISE
	# Rest API Method
	$url = "https://textanalytics-xxxx.cognitiveservices.azure.com/text/analytics/v2.1/sentiment"
	
	# APIkey to Invoke Azure Cognitive Service
	$key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
	
	# JSON Body
	$documents = @()
	$jsonInnerbody = @{“language” = “en”; “id” = “1”; “text” = “This is good blog and I highly recommend it!” };
	$documents += $jsonInnerbody
	$jsonBody = @{documents = $documents}
	$jsonPayload = ConvertTo-Json $jsonBody
	
	# Headers to pass to Rest API
	$header = @{ “Ocp-Apim-Subscription-Key” = $key }
	
	#Invoke the REST API
Invoke-RestMethod -Method Post -Uri $url -Header $header -Body $jsonPayload -ContentType “application/json” -Verbose
  • You should get the Output below:

I hope after this example you have a better understanding of Text Analytics API, and you’ve understood the basic’s of using it with PostMan and PowerShell.

In the next blog we will understand how to use the Text Analytics API with Power Apps and Power Automate.

Subscribe to this blog for the latest updates about SharePoint Online, Microsoft Flow, Power Apps and document conversion and manipulation.