Created
January 27, 2020 14:04
-
-
Save gracecarrillo/2eb646cad60a146d5b93fa2e3c6213fb to your computer and use it in GitHub Desktop.
This file contains 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
#----------------- FEATURE ENGINEERING ------------------------# | |
#---------- Sentiment Score with Vader -----------------# | |
# Instantiate Vader | |
analyser = SentimentIntensityAnalyzer() | |
def polarity_scores_all(tweet): | |
''' | |
Takes string of text to: | |
1. Gets sentiment metrics | |
2. Returns negative, neutral, positive | |
and compound scores as lists. | |
''' | |
neg, neu, pos, compound = [], [], [], [] | |
analyser = SentimentIntensityAnalyzer() | |
for text in tweet: | |
dict_ = analyser.polarity_scores(text) | |
neg.append(dict_['neg']) | |
neu.append(dict_['neu']) | |
pos.append(dict_['pos']) | |
compound.append(dict_['compound']) | |
return neg, neu, pos, compound | |
# Append to your dataset | |
all_scores = polarity_scores_all(train.tidy_tweet.values) | |
train['neg_scores'] = all_scores[0] | |
train['neu_scores'] = all_scores[1] | |
train['pos_scores'] = all_scores[2] | |
train['compound_scores'] = all_scores[3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment