Analyzing voter sentiment for the Kiambaa by-election using Twitter data and Natural Language Processing

kevin mirera
Analytics Vidhya
Published in
5 min readJul 27, 2021

I’m going to focus on three things in this article; Sentiment analysis, Natural language processing and the amazing twitter API- Application programming interface for those who do not know. The data I am displaying is from twitter users and is not an expression of my views. Let’s get to it!

Sentiment analysis

You may probably be asking yourself what is Sentiment Analysis. Sentiment analysis also known as opinion mining is a technique used to analyze whether data is positive, negative or neutral. Textual data is fed into the computer and using some crafted rules the computer program is able to identify the subjectivity of the sentence, for example if a sentence contains words like good, great, best, amazing, beautiful the program will be able to know where to categorize this sentence.

Natural Language Processing

Computers however cannot understand words like we do and hence very smart people invented Natural Language Processing NLP in short which is a branch of Artificial Intelligence concerned with giving computers the ability to understand text and spoken words in much the same way human beings can.

Twitter api

I know everyone reading this knows what twitter is , you know, that place we go to to get our daily dose of gossip and our neighbor’s opinion on how yesterday’s football match should have gone. For this project I utilized Twitter’s amazing API (description of what an api is at the bottom) to get data on the recent hotly contested Kiambaa by election here in Kenya, so lets get to it!

Top ten most liked tweets

I picked the last 1000 tweets that had a mention of the word Kiambaa. I saved that data and made some analysis and this is what I found

The most liked tweet of the thousands of processed tweets was by Ahmednasir Abdullahi SC which had just over 470 likes. Honorable Junet Mohamed CBS tweet followed with 355 likes,a twitter user by the name DIKEMBE’s tweet came in third with 329 likes. Everyone’s favorite political analyst and twitter user Mutahi ngunyi was fourth while a twitter user named Daniel Njaga came in fifth with 98 likes on his tweet.

Top 5 tweets about the concluded Kiambaa by elections

Top Words overall

The top words that seemed to pop out of the thousands of tweets fetched on the Kiambaa Elections are :

The word Kiambaa appeared over 400 times with DP William Ruto’s name popping up the second most more than 100 times, the church remains at the center of Kenyan politics being mentioned close to 100 times. Other key words were vote, win , jubilee, people and MutahiNgunyi.

Top Organizations overall

As for organizations, the top mentioned organizations were as follows :

top organizations overall

Some of the top mentioned organizations were UDA, DP Rutos party, The Kiambaa Church, Dennis Itumbi the self declared Hustler Nation’s spokesperson was also high up in the list. Other organizations like Uhuru’s Jubilee Party , ODM , ICC ,WSR and Youtube were also highly mentioned by Kenyans on Twitter.

Top People mentioned

As for the top most mentioned people this is how the list went:

Top people mentioned

DP William Ruto’s account was mentioned over 20 times with Raila Odinga following him closely. Other mentions were Paul Gicheru, Kariri Njama who was the Jubilee candidate, Uhuru Kenyatta, BelieveKinuthia was also highly mentioned in the tweets. In a surprise turn of events the actual winner of the elections Njuguna Wanjiku of UDA did not appear anywhere in the top mentions, begging me to questions what we as Kenyans talk about when we’re having political conversations , is it the people who are running and their policies or is it the political parties that they represent.

That is all, until next time my precious readers, read below for details on what an API is using the menu, restaurant metaphor.

API

For this project I utilized the Twitter API short for Application programming Interface. Basically what an API does is it helps me fetch information from another program like Twitter or Spotify and insert it into my own program. For further clarification take this for example ;

You’re home, you’re hungry, you’re lazy. Time to order some food.

You decide to order from Jack’s Burgers, so you look through their menu to see the options, then request a specific dish by placing an order. The order is received at Jack’s Burgers, passed along to the kitchen, and a series of steps are performed to create the dish. That dish is delivered to you and is quickly devoured.

Yum.

Ok you’re still hungry. But you don’t want another burger. You decide to go with Jill’s Pizza this time.

When you call Jill’s Pizza to place your order, what do you think would happen if you tried to order off the menu for Jack’s Burgers? You’d probably be met with confusion. You certainly wouldn’t get the dish you tried to order.

In the above analogy, you’re a program and the restaurants are programs that you want to interact with. To receive what you want (food) from the other program (restaurant), you need to make a request (place an order) in a specific way (defined by the menu). If you try to request in the wrong way, you won’t get what you want.

APIs are like menus. Menus define a list of dishes and, when someone orders one of those dishes, the restaurant does a bunch of stuff and then sends the requested dish back. APIs define a list of commands and, when a program uses one of those commands, the other program does a bunch of stuff and then sends back what was requested by that command (usually some kind of data). Hope that clarifies things 😊.

--

--