Skip to content

Instantly share code, notes, and snippets.

@gracecarrillo
Created January 27, 2020 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gracecarrillo/2eb646cad60a146d5b93fa2e3c6213fb to your computer and use it in GitHub Desktop.
Save gracecarrillo/2eb646cad60a146d5b93fa2e3c6213fb to your computer and use it in GitHub Desktop.
#----------------- 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