Last active
August 24, 2020 21:45
-
-
Save sevperez/1bd0b378ac02e6ea4017864ea477db71 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sentiment_descriptor(sentence): | |
""" | |
- Parameters: sentence (a Stanza Sentence object) | |
- Returns: A string descriptor for the sentiment value of sentence. | |
""" | |
sentiment_value = sentence.sentiment | |
if (sentiment_value == 0): | |
return "negative" | |
elif (sentiment_value == 1): | |
return "neutral" | |
else: | |
return "positive" | |
print(sentiment_descriptor(moby_p1.sentences[0])) | |
# neutral | |
def sentence_sentiment_df(doc): | |
""" | |
- Parameters: doc (a Stanza Document object) | |
- Returns: A Pandas DataFrame with one row for each sentence in doc, | |
and columns for the sentence text and sentiment descriptor. | |
""" | |
rows = [] | |
for sentence in doc.sentences: | |
row = { | |
"text": sentence.text, | |
"sentiment": sentiment_descriptor(sentence) | |
} | |
rows.append(row) | |
return pd.DataFrame(rows) | |
sentence_sentiment_df(moby_p1) | |
# text sentiment | |
# 0 Call me Ishmael. neutral | |
# 1 Some years ago—never mind how long precisely—h... neutral | |
# 2 It is a way I have of driving off the spleen a... neutral | |
# 3 Whenever I find myself growing grim about the ... negative | |
# 4 This is my substitute for pistol and ball. neutral | |
# 5 With a philosophical flourish Cato throws hims... neutral | |
# 6 There is nothing surprising in this. neutral | |
# 7 If they but knew it, almost all men in their d... neutral |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment