DEV Community

Cover image for Implement Serverless Text Sentiment Analysis using Azure Text Analysis API
Chidera
Chidera

Posted on

Implement Serverless Text Sentiment Analysis using Azure Text Analysis API

As part of the day 5 task for the Microsoft Azure #25daysofserverless challenge on twitter, I implemented language and sentiment analysis using Azure Text Analytics API.

For the task, you are expected to determine if a child has been nice or naughty based on messages sent to Santa. Of course, these are children from different parts of the world hence different countries. In this article, you will learn "how to implement text sentiment analysis" using "Azure Text Analytics".

We would be using:

  • Nodejs
  • Visual Studio Code
  • Azure Functions
  • Azure Text Analytics
  • Postman for testing
  • Axios for https requests

First, we need to create a serverless function using Azure. For more details on how to do this using VSCode, visit this link.

Next, we obtain our configuration keys from the Azure portal. The keys are required to access the Text Analytics API. To get this key, you can:

Create a cognitive service resource: analytics through the Azure portal.
Cognitve service dashboard

or follow this link to cognitive services and generate your trial key valid for 7 days.

Then add these keys to your env file or configuration file.

config file

Now that we are done with the setup, we will proceed to create the functions that will handle the analysis. They are divided into two:

  • Determine the language
  • Determine the sentiment

To store this functions, I created a different file named congnitives.js so as to abstract these functions from my main code.

First, I ensure that I have the proper keys to hit my api endpoint. If the key is missing, an error is thrown and we assign the path for fetching languages and sentiments from the API.

I created an asynchronous function get_language that takes in an object with a property 'documents' that holds the text to be analysed in JSON format as a parameter.

Then specified the endpoint to be used for the request. Next, I set the headers with content-type and my subscription key.

NB: Ensure that the content-type set is of JSON format as that is what the text analytics API accepts.

Then, using axios, I await my post request to the URL, specifying the text as the body and then set the headers.

Subsequently, I create another asynchronous function that analyses a given document and provides the sentiment. the sentiment score ranges from 0 to 1 with numbers closer to 1 being positive while the ones closer to zero are negative. Then I export the two functions.

Implementation

I import the two functions I had created in cognitives.js using object destructuring.

I fetch the list of messages to be analyzed, in my case, using a URL which returned them as an array of objects.
Then I looped through the array to obtain the particular data needed for the language analysis and stored them in the format shown below:
Next, I call the get_language function, which I await because the result is needed for further processing. A successful response is returned in JSON, as shown in the following example:

get_language response

I proceed to do store the data in the format required for sentiment analysis, call the get_sentiment function and expect a response as shown:
get_sentiment response

After sentiment analysis, using the results, I can now classify the letters and kids as naughty or nice. Sentiment is positive if closer to 1 and negative if close to 0. Hence if score is greater than or equal to 0.5, it is classified as nice. Otherwise, it will be classified as naughty.
.
This I send back to SantaπŸŽ… and Santa is happy 🀩🀩.

If you have any questions or comments, feel free. Don't forget to leave thumbs up if you found the article helpful.

Follow me on Twitter @dera_jo

Top comments (2)

Collapse
 
vikramchandra profile image
Vikram Sharma

Azuere is a good platform for quick development. However other platforms may be better suited in some situations. I worked on a NLP medicine app and used Amazon. It motivated me to create a list of best sentiment analysis apis. I hope it is useful.

Collapse
 
patrixr profile image
Patrick R

Both interesting and appropriate to the holiday season. well done !