Using Artificial Intelligence to Understand Emotions in Tweets
Tags: artificialintelligence, ai, machinelearning
Published: 2019-08-27 19:01:22

---
title: Using Artificial Intelligence to Understand Emotions in Tweets
published: true
description: 
tags: artificial-intelligence, AI, machine learning, 
---

Hello, internet! Yesterday I introduced [Feelsbot](https://www.feelsbot.me/). Today I'll discuss how Feelsbot uses artificial intelligence to analyze emotional sentiment in Tweets. Feelsbot can analyze Tweets of any geographical location or any public Twitter profile.

![Gif of Feelsbot](https://thepracticaldev.s3.amazonaws.com/i/jhfrck232gln92bg7wkt.gif)

### What is Artificial Intelligence and Machine Learning?

**Artificial Intelligence (AI)** seems to be all over the place these days, but what exactly is it? Feelsbot, which uses AI to understand tweets, is a great example to help answer this question.   

AI is a field of computer science dedicated to creating machines that can make intelligent decisions. You can think of artificial intelligence as a goal. We can reach that goal through machine learning. **Machine learning** is a way to teach machines how to make predictions or decisions without being explicitly programmed.

Machine learning models are said to *learn* because instead of being programmed to do things, they are *trained*. A model is trained through being shown lots of example data points and looking for patterns in the examples. Once identified, these patterns will be used to make decisions about new pieces of data.

Feelsbot uses a machine learning model created by IBM that categorizes text as being either joyful, sad, angry, disgusted, or fearful. This model was trained to detect emotion by being shown lots of examples of joyful, sad, angry, disgusted and fearful text. The patterns the model detected may involve keywords, such as “cry” or “cheer”, or they may involve the number of words in each sentence. Whatever the patterns are, they all come together to form the machine learning model. 

**Natural language processing** is a subset of AI that aims to understand human language. The machine learning model that powers Feelsbot can also be called natural language processing.

###Inspiration for Creating Feelsbot

There are a few reasons why I decided to create Feelsbot. First off, I was inspired by how easy it is to hop in and use some of the machine learning APIs from IBM and Google. Additionally, I wanted to create a software project to help people better understand how AI works. 

Feelsbot highlights a couple of important things about AI and natural language processing.

* Many machine learning models have a rather simplistic view of emotion, and can’t pick up on subtle things, such as sarcasm.
* Sometimes figures of speech can throw off the model, for example, "I'm so happy I could cry" is detected as sad with a 65% confidence.

Although Feelsbot is not perfect, it’s more accurate than you might expect!  I am often surprised by how well it can categorize more ambiguous tweets. Try it out to see for yourself.

![Image of cartoon Robot crying, celebrating and short-circuiting.](https://thepracticaldev.s3.amazonaws.com/i/29zbbmkkxz399inhufd6.png)

*Note: Tweets are short and often use slang which doesn’t always work well with a machine learning model trained with regular text excerpts. For this project, I did not train my own model, but it would be interesting to create a machine learning model that is trained using only tweets.*


### How Feelsbot Finds and Categorizes Tweets

As mentioned before, Feelsbot is using a machine learning model from IBM to analyze emotions in tweets. Using this model, Feelsbot put tweets into five categories: joy, sadness, anger, fear, and disgust. This model only works in English, limiting the number of tweets Feelsbot can analyze. Each tweet receives a score, called a confidence score, of how strongly it matches one of those categories. Feelsbot puts tweets that have a confidence score higher than 65% into each category. Once the tweets have been categorized, the joy meter is calculated as the percentage of joyful tweets versus every other emotion.

When using the map, Feelsbot uses Twitter's API to fetch the last 100 tweets that are geotagged near the location entered. If there are not many recent geotagged tweets near that location, Twitter fetches tweets of users whose profile locations are nearby. When analyzing tweets by a specific Twitter account, Feelsbot fetches the last 150 tweets by that account. Twitter profiles need to be public for Feelsbot to work.

### What’s next for Feelsbot?

Check out Feelsbot and let me know what you think! By request, a hashtag search is currently in the works, so keep your eyes peeled for that update. 

Also, I only touched the surface of what machine learning and AI are, but if you enjoyed this post and would like a more in-depth explanation, please leave a comment below!

### Contributors
* Big thanks to [Novvum](https://www.novvum.io/) for supporting the development of Feelsbot.
* Feelsbot was created by [Allison Colyer](https://twitter.com/allicolyer).
* Robot drawings were created by [Ruby Ríos](https://rubyrios.com/).