-
Data Reading and Cleaning
- The datasets are read from CSV files, combined, and filtered into two main categories: left and right-leaning subreddits. Data from 'other' subreddits is excluded from the analysis. The data is also organized by necessary columns, including comment text, subreddit, time of post, and sentiment-related fields.
-
Sentiment Analysis
- Using NLTK's built-in VADER SentimentIntensityAnalyzer, we perform sentiment analysis on each comment to obtain the following sentiment scores:
- Negative sentiment (comment_neg)
- Neutral sentiment (comment_neu)
- Positive sentiment (comment_pos)
- Compound score, which combines the previous scores into a single overall sentiment (comment_compound).
- Using NLTK's built-in VADER SentimentIntensityAnalyzer, we perform sentiment analysis on each comment to obtain the following sentiment scores:
-
Emotion Detection
- We apply an emotion detection model to a random sample of comments from each political group (left and right) for each month. The model predicts the dominant emotion in the text, which is stored in a new DataFrame along with a score for each emotion. This model is computationally intensive, so the data is sampled, and results are saved to ensure efficiency.
-
Data Manipulation and Visualization
- The data is aggregated and visualized, showcasing the sentiment and emotion trends over time for both left and right-leaning subreddits. Key visualizations include:
- Sentiment Over Time: Plots showing the sentiment trends over time (monthly and daily) for both political sides, with significant events like Biden’s election and the United States’ support for Ukraine.
- Emotion Frequency: Visualizations of the frequency of different emotions (e.g., anger, joy, sadness) in the left and right-leaning subreddits, normalized for comparison.
- Emotion Over Time: Plots of how emotions (excluding neutral) change over time, again divided by political side.
- The data is aggregated and visualized, showcasing the sentiment and emotion trends over time for both left and right-leaning subreddits. Key visualizations include:
pip install pandas numpy matplotlib nltk transformersIf you're working on Google Colab(recommended), you can directly mount your Google Drive to access the datasets using the following code:
from google.colab import drive
drive.mount('/content/drive', force_remount=True)- reddit_opinion_democrats.csv: Data for left-leaning subreddits
- reddit_opinion_republican.csv: Data for right-leaning subreddits
You can find these datasets in the shared Google Drive folder here.
- Mount Google Drive: Ensure your Google Drive is mounted if you're using Colab.
- Read and Clean Data: Load the datasets and combine them into a single DataFrame.
- Run Sentiment Analysis: Apply the VADER sentiment model to the text of the comments.
- Run Emotion Detection: Apply the emotion detection model to a random sample of the comments.
- Visualize the Data: Aggregate and visualize the sentiment and emotion trends over time using the provided plots.
- Sentiment Over Time: Graphs illustrating how sentiment fluctuates over time, with notable spikes around political events such as elections.
- Emotion Frequency: A comparison of emotional responses in left and right subreddits, showing differences in emotional tone.
- Emotion Over Time: Insights into how emotional responses change over time, including the exclusion of neutral emotions to focus on stronger feelings like joy or anger.