How to get Sentiment Score from Microsoft Text Analytics API
Microsoft Cognitive services provides lots of APIs.
One that caught my attention recently is Text Analytics API.
Below is a snippet for using the Text Analytics features to find the Sentiment score of the user input.
There are endless possibilities to use it.
Happy Coding
Parshuram
One that caught my attention recently is Text Analytics API.
Below is a snippet for using the Text Analytics features to find the Sentiment score of the user input.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string message = "I am feeling great"; | |
const string apiKey = "API Key From the Microsoft Congnitive services"; | |
const string queryUri = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment"; | |
var client = new HttpClient | |
{ | |
DefaultRequestHeaders = { | |
{"Ocp-Apim-Subscription-Key", apiKey}, | |
{"Accept", "application/json"} | |
} | |
}; | |
DocumentRequest test = new DocumentRequest { Id = "1", Language = "en", Text = message }; | |
var sentimentInput = new BatchInput | |
{ | |
documents = new List<DocumentInput> { | |
new DocumentInput { | |
id = 1, | |
text = message, | |
} | |
} | |
}; | |
var sentimentPost = await client.PostAsync(queryUri, new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(sentimentInput), Encoding.UTF8, "application/json")); | |
var sentimentRawResponse = await sentimentPost.Content.ReadAsStringAsync(); | |
var sentimentJsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<BatchResult>(sentimentRawResponse); | |
var sentimentScore = sentimentJsonResponse?.documents?.FirstOrDefault()?.score ?? 0; |
There are endless possibilities to use it.
Happy Coding
Parshuram
Comments
Text Mining Techniques and Ideas
Text Analytics using Python
Text Analytics Guide
Text Mining Techniques
Data Mining