Open-source Xenophobic Tweet Classifier

We will periodically release an open-source AI model on a relevant topic for your personal projects or hackathons. We start with a xenophobic tweet detector.

Abraham Starosta
Sculpt
7 min readJul 29, 2019

--

By Abraham Starosta and Tanner Gilligan

Introduction

When building new applications in today’s quickly changing world, machine learning is often necessary to provide users with power insights. Unfortunately, for many of the people developing these applications, AI can be difficult to implement, and expensive to outsource. Several of our tech friends have said that while working on personal projects or at hackathons, they would have liked to include machine learning. Typically, even if they knew how to implement the model, there wasn’t an existing dataset they could use for their application and creating a dataset from scratch would have been a very laborious process.

To help address this gap in the developer and AI community, we have decided to create and release a series of publicly available classifiers that anyone can use. We will periodically release an open-source AI model on an interesting and relevant topic, and users can either download the model/data for local use, or simply use our API.

For our first model, we created a model to detect xenophobic tweets.

Xenophobia on Twitter

Detecting hate speech in general is a very important problem and AI will be instrumental in fighting it on social media platforms. Despite the AI advancements made in recent years, we still have a long way to go on this problem. Louise Matsakis of Wired explains that only 38% of hate-speech posts that Facebook removes are detected by AI. This is mainly because there are so many types of hate speech, and the language used changes rapidly.

One type of hate speech that has been a hot topic in the news over the last week is xenophobia, which is prejudice against immigrants. Like many social media sites, Twitter has seen an influx of tweets on the topic, including many that could be considered hate speech. Luckily for us, Twitter also provides an API for developers to download tweets, so this was a great source to get relevant data from.

Here are a few example tweets that we’re looking to detect:

Implying all illegal immigrants are terrorists (ISIS loving)
Saying immigrants are invading the US
Implying that a congresswoman is undocumented just because of her looks or country of origin

Criteria

Once we had downloaded a substantial number of tweets, we needed to go through the process of creating a labeled dataset. To do this, we utilized a combination of manual annotation and weak supervision to label a total of 10,181 tweets, following a similar process to our older blog. At a high-level, we used topic detection and keyword searches to label data, and used that data to inform a weakly-supervised model. Some examples of concepts we used for identifying xenophobic tweets includes:

  • Using pejorative phrases like “illegal aliens” or “illegal criminals”
  • Telling immigrants to “go back to their country”
  • Saying immigrants are “invading” the US
  • Saying immigrants are anti-American
  • Saying there should be mass deportations of undocumented immigrants
  • Saying immigrants come to the US just to take advantage of the system

And this is the main criteria for non-xenophobic tweets:

  • Calling out racist comments or chants
  • Talking about the history of racism
  • Talking about legally acquiring citizenship
  • Saying American immigrants are also American
  • Various other topics that aren’t xenophobic

Model Architecture

After creating our dataset, the next step was to create a model out of it. We created a model using TF-IDF vectorizer with logistic-regression predictor. Tweets are split using a tokenizer, and only the top 2000 tokens are used as features. All steps were done using Scikit-Learn, so you can try training a model for yourself if interested.

Quantitative evaluation

The following graph shows the precision-recall curve, with an area under the curve of 0.82 (validated with ~150 examples).

Precision-Recall Curve

The model returns the probability that a tweets is xenophobic. Therefore, if you want high precision (you don’t want to make false positive mistakes), then make sure you use a high probability threshold of 0.9 or more. In other words, when you compute the probability with the model, it should be higher than 0.9 for you to classify the tweet as xenophobe.

You can also use the table below to choose your threshold based on your desired precision and recall:

Precision and Recall at different probability thresholds

Instructions for using the model and downloading the dataset

You can copy paste the code in our Google Colab notebook into your own local or Colab notebook. Just to make it easier, we’ll also add our code here.

Step 1: Initial tweet filtering

We only want to run the model on tweets that match the search queries we used to build our tweet dataset. That way we’ll be safer from data mismatch. Otherwise, the model can get confused when it sees tweets that look significantly different from the ones it was trained on. You can use the function below:

Step 2 — option 1: Using our API

If a tweet matches one of our search queries, then we can call the API with the following code. If a tweet doesn’t match one of the search queries then the API will return an error message saying “Tweet doesn’t match search terms.”

Note for Windows users: because the models were serialized in a Mac machine, deserializing them in a Windows machine can cause problems. Therefore, if you have a Windows machine then we highly suggest you use the API.

Step 2 — option 2: Using models locally

Or, you can download the models and use them locally.

If you get an SSL error when downloading the models then run this command (on mac):

Downloading the dataset

If you want, you can also download the zipped labeled training and test datasets here to train your own model and hopefully beat our logistic regression.

Project ideas

If you’d like some inspiration we’ve written below a few project / hackathon ideas the model could help with:

  • Visualize xenophobic tweets on a map (could be done with folium in a Jupyter notebook)
  • Where in the USA is there more xenophobia?
  • Find different topics in xenophobic tweets (you can find our data below)
  • Train a BERT model with our data
  • Does it perform better than our logistic regression?
  • A small app that shows a few xenophobic tweets every day
  • A dashboard that tracks the number of xenophobic tweets per day and plots a graph
  • An app that calls out users who said potentially xenophobic tweets

How to build your own twitter dataset

You need to make a free Twitter developer account and get your access token and consumer key. Then, if you run our script overnight you can download tweets from the past 7 days that match a query. You can also use GNIP you’d like to get access to a real-time stream of tweets, although they might charge for that.

Conclusion

We hope you get to try our model and help fight hate speech. If you would like to partner on building a model please email abraham@sculptintel.com.

Disclaimer: xenophobia is a complex and often emotional topic. We’re computer science experts, not xenophobia experts. We built this model based on our personal research and we’re not making any political statement whatsoever.

--

--