Skip to content

Instantly share code, notes, and snippets.

@john-adeojo
Created March 30, 2023 23:00
Show Gist options
  • Save john-adeojo/09cd66aba53ce8b721bc9ae3df83883e to your computer and use it in GitHub Desktop.
Save john-adeojo/09cd66aba53ce8b721bc9ae3df83883e to your computer and use it in GitHub Desktop.
# sentiment analyser, specify model
analyzer = SentimentAnalyzer('cardiffnlp/twitter-roberta-base-sentiment-latest')
# Get sentiment analysis
tweets_with_sentiment = analyzer.get_sentiment(tweets_df)
# emotion analyser, specify model
analyzer = SentimentAnalyzer('cardiffnlp/twitter-roberta-base-emotion', emotion=True)
# Get emotion analysis
tweets_with_sentiment = analyzer.get_sentiment(tweets_with_sentiment)
# Generate emotion and sentiment distributions at the user_id level
sentiment_ratios = pd.crosstab(
index=tweets_with_sentiment['user_id'],
columns= tweets_with_sentiment['sentiment'],
normalize='index'
).reset_index()
emotion_ratios = pd.crosstab(
index=tweets_with_sentiment['user_id'],
columns= tweets_with_sentiment['emotion'],
normalize='index'
).reset_index()
ratios_emotion_sentiment = emotion_ratios.merge(sentiment_ratios, on='user_id')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment