Working with Sentiment Analysis for Swahili Language using Nena API
Natural Language Processing, better known by its acronym NLP, is taking the world by storm. After the major strides taken by researchers all over the world in the field of Computer Vision, lessons learnt are now being transferred over to the field of NLP and quickly ushering a language and automation revolution. In this article we will talk about how you can do emotion and sentiment analysis using the new (currently invite only Alpha) Nena API endpoints.
Nena, meaning “speak” or “say” in Swahili, is a project and initiative by the Mary.Africa team to automate, support, scale and empower Swahili voices and conversations in the digital world. The team has released REST API as a means of interfacing with the platform. To know more about Nena and updates, be sure to follow the Mary team on twitter.
Currently, the Nena API Service provides a set of services like Tokenization, Parts of Speech Tagging (POS), Named Entity Recognition (NER), and Sentiment & Emotion analysis. In the near future, the team will release language models and fully fledged chatbot services and endpoints.
Sentiment Analysis
In this article, we will dive in-depth on how to use the Nena API Service to perform Sentiment Analysis. We will be testing the application endpoints through Postman, and even be using the API in a simple React application.
As a pre-requisite:
- Make sure you have an API Key by filling out this form: https://forms.gle/qmJaL5tdcexUJ2ft5.
*The platform is currently in invite only phase so you might have to give it a few hours/a day to get your API key updates.
You have the key? Alright, let’s analyze some Swahili!
The documentation for the Nena API Service currently uses the postman generator, which makes it relatively easy to try out the different endpoints. You can visit the documentation here.
Using the Sentiment Analysis Service
Nena allows users to make sentiment analysis from 2 sets of classes.
base
sentiment set — The classes within this set haspositive
,negative
andneutral
sentimentsemotion
sentiment set — The classes within this set hashappy
,sad
,angry
andfearful
sentiments
We will be making simple calls for the base
sentiment set, and an example Github repository, it will include a simple application that makes use of the emotion
sentiment service.
Suppose you are looking to make a simple request to the Sentiment Analysis service provided by the Nena API to determine the base
sentiment of the sentence:
Mimi nimefurafi sana
Here is how you can go about it. You can open up your terminal (making sure you have installed cURL
, or follow the instructions here), and enter the following command. Make sure you replace the<API_KEY>
with your actual API Key.
curl -L -X POST -H “Content-Type: application/json” https://api.nena.mary.africa/api/tasks/sentiment/base' \
— data-raw ‘{
“apiKey”: “<API_KEY>”,
“payload”: {
“texts”: [
“Mimi nimefurahi sana”
]
}
}’
In the example above, we are using only one string of text, but you can pass up to 5 currently for processing.
You should see the response look something like this:
{"output":{"info":{"labels":["neutral","positive","negative"]},"metadata":{"confidence":[0.9641],"logits":[[0.04213,0.9641,0.01467]]},"results":[1]}}
Interpreting the Response Shape
When the made request is successful, you will receive a response of the following shape:
https://gist.github.com/iam-kevin/68bd4b856fa7fa83045f9dc5ebbc349b
This shape is common to most of the NLP endpoints. a quick TLDR for fields under the output
object
info
— this object is always present and it describes the overall information for the task. For the Sentiment Analysis service, this object would contain thelabels
field, which is a list of all the possible labels that can apply to the text provided. In this case, the possible labels areneutral
,positive
, ornegative
metadata
— this object contains additional information about the results of the task. This field is not always present. For this service, it would carry information about the overall confidence of the sentiments predicted (under themetadata.confidence
field) as well as the probabilities for each sentiment corresponding to the label (under themetadata.logits
field)results
— this field, that is always present in all services, holds results for the particular service. For sentiment analysis, the result is a list of integers from0
to2
which is thezero-index
corresponding to items in theoutput.info.label
.
Knowing now how to interpret the shape, it appears that for the sentence “Mimi nimefurahi sana”, The base
set sentiment result is positive (~96.41%)
Depending on the service, the shape of the output will differ, so please bear in mind the type of service you are using. Documentation for the API, in full, will be rolled out soon.
The Mary team at Inspired Ideas is very excited to get feedback on the Nena service and how it could be a technology for good at scale. If you have any thoughts or suggestions please reach out!
Here is a example project hosted over github that makes use of the Nena Emotion Sentiment Analysis Service. It is neatly wrapped in a front-end to show how well this application can be used.
Please feel free to say hi over on Twitter.