Skip to content

Instantly share code, notes, and snippets.

@sevperez
Created August 24, 2020 21:51
Show Gist options
  • Save sevperez/964d5bcb7d8fa0953cf895b93fd5b803 to your computer and use it in GitHub Desktop.
Save sevperez/964d5bcb7d8fa0953cf895b93fd5b803 to your computer and use it in GitHub Desktop.
# What is the sentiment surrounding each character?
def sentiment_descriptor_to_val(descriptor):
"""
- Parameters: descriptor ("negative", "neutral", or "positive")
- Returns: -1 for "negative", 0 for "neutral", 1 for "positive"
"""
if descriptor == "negative":
return -1
elif descriptor == "neutral":
return 0
else:
return 1
def character_sentiment(df):
"""
- Parameters: df (Pandas DataFrame)
- df must contain "text" and "sentiment_descriptor" columns.
- Returns:
"""
sentiment = df.copy()
sentiment["sentence_sentiment"] = [
sentiment_descriptor_to_val(s) for s
in sentiment["sentence_sentiment"]
]
sentiment = sentiment[["text", "sentence_sentiment"]]
sentiment = sentiment.groupby("text").sum().reset_index()
return sentiment.sort_values("sentence_sentiment")
sentiment_df = character_sentiment(characters)
print("Characters in the most negative settings.")
display(sentiment_df.head(5))
# Characters in the most negative settings.
# text sentence_sentiment
# 6 Ahab -42
# 508 Queequeg -24
# 317 Jonah -18
# 588 Stubb -11
# 468 Pequod -10
print("Characters in the most positive settings")
display(sentiment_df.tail(5))
# Characters in the most positive settings
# text sentence_sentiment
# 1 Abraham 2
# 424 Monsieur 3
# 218 Gabriel 3
# 401 Mary 4
# 93 Bunger 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment