Skip to content


Repository files navigation


This repository contains code for the paper:

Demszky, D., Garg, N., Voigt, R., Zou, J., Gentzkow, M., Shapiro, J. & Jurafsky, D. (2019). Analyzing Polarization in Social Media: Method and Application to Tweets on 21 Mass Shootings. In 17th Annual Conference of the North American Chapter of the Association for Computational Linguistics (NAACL).

 author = {Demszky, Dorottya and Garg, Nikhil and Voigt, Rob and Zou, James and Gentzkow, Matthew and Shapiro, Jesse and Jurafsky, Dan},
 booktitle = {17th Annual Conference of the North American Chapter of the Association for Computational Linguistics (NAACL)},
 title = {{Analyzing Polarization in Social Media: Method and Application to Tweets on 21 Mass Shootings}},
 url = {},
 year = {2019}

All the results as well as the plots in the paper were generated using the scripts in this repository. Due to Twitter's privacy policy, we are not able to share the original tweets, but we are sharing the tweet IDs under data/tweet_ids and the scripts for obtaining the tweets and the followers of politicians (see below).

Obtaining Data via the Twitter API

I added scripts that can be used for obtaining tweets and followers for the politicians in obtain_tweets_and_followers. These can be used with minimal modification!

The following scripts are included in there:

  • Obtain tweets by ID. Use this to get the tweets, based on the tweet ids in data/tweet_ids.
  • Download follower ids for politicians. Since getting the followees (friends) of each Twitter user takes a very long time, we instead take the reverse approach: take the followers of all politicians and check if the users in our data are among the followers.
  • Get tweets for particular users (we don't use this in our paper).
  • Stream tweets for a particular event (Thousand Oaks, Pittsburgh.)
  • Lookup users (whether they have been deactivated or not).


Make sure to use Python3 when running the scripts. The package requirements can be obtained by running pip install -r requirements.txt.



The paper as well as plots and tables.


  • tweet_ids: ids of the tweets we used (unfiltered)
  • input: inputs for the scripts; this folder also includes all the handles of Democrat and Republican politicians we used to determine partisanship (all_dems.txt and all_reps.txt)
  • output: outputs of the scripts
  • topic_eval: the results of our topic model evaluation


Code and data for verifying our method for partisanship assignment (Section 2 in the paper).


Code for preprocessing the data -- i.e. removing retweets, building vocabularies and cleaning up tweets.


Scripts for performing the topic clustering, step by step. The scripts need to be used in ordered sequence:

  • compute word co-occurrence matrix to use as an input for GloVe
  • train GloVe using the Mittens package
  • construct tweet embeddings using Arora et al.'s (2017) method
  • compute cluster means using k-means with cosine distance, based on a sample of the data
  • compute the proximities of all tweets to each topic
  • For running BTM:
    • download the scripts from here
    • set the BTM directory path within config.json and within 2_topic_clustering/
    • follow the steps in pre-processing for BTM.ipynb
  • For MALLET:
    • download the MALLET binary
    • set the directory path within config.json
    • follows the steps in MALLET.ipynb


Scripts for measuring the polarization of tweets, based on Gentzkow et al. (2018). Note that this builds on topic assignments, therefore these scripts can be executed only once topics have been assigned.

  • compute the overall leave-out estimate for each event
  • compute the between-topic polarization for each event
  • compute the within-topic polarization for each event
  • compute within-topic polarization over time for particular events


Code for measuring and plotting the partisanship (log odds ratio) of words, phrases and semantic categories.

  • calculate the partisanship of all words for each event
  • plot word partisanship.ipynb: compare partisanship of individual words / phrases across events
  • event grounding.ipynb: measure and plot the partisanship of event grounding
  • modals.ipynb: measure and plot the partisanship of modals
  • pronouns.ipynb: measure and plot the partisanship of pronouns


Measure the partisanship of affect categories.

  • affect_parisanship.ipynb:
    • construct affect lexicon based on the NRC Emotion Lexicon, by filtering it for our own domain via GloVe embeddings
    • plot the partisanship of affect categories
  • collect affect features for each event based on our affect lexicon


Notebooks for looking at the data and the embeddings.


Code for the paper "Analyzing Polarization in Social Media: Method and Application to Tweets on 21 Mass Shootings"






No releases published


No packages published