Perform sentiment analysis (Python Tutorial)

Learn Python Sentiment Analysis (Quick Tutorial)

The script I use in the video tutorial is below.

#if you haven't installed these packages then you need to install them
#pip install nltk
#pip install pandas

#import packages
import nltk
nltk.download('vader_lexicon')
from nltk.sentiment.vader import SentimentIntensityAnalyzer as SIA
import pandas as pd

#open csv file
df = pd.read_csv(r"C:\temp\data.csv")

#remove empty lines and set data types
df = df.dropna(subset=["date"])
df['date'] = df['date'].astype('datetime64[ns]')
df['text'] = df['text'].astype(str)
df['state'] = df['state'].astype('category')

print(df.head)

#calculate sentiment
analyzer = SIA()
df['compound'] = [analyzer.polarity_scores(x)['compound'] for x in df['text']]
df['negative'] = [analyzer.polarity_scores(x)['neg'] for x in df['text']]
df['neutral'] = [analyzer.polarity_scores(x)['neu'] for x in df['text']]
df['positive'] = [analyzer.polarity_scores(x)['pos'] for x in df['text']]

#create columns
df['sentiment']='neutral'
df.loc[df.compound>0.05,'sentiment']='positive'
df.loc[df.compound<-0.05,'sentiment']='negative'
df.head()

#on-screen summary
print(df['sentiment'].value_counts()['neutral'])
print(df['sentiment'].value_counts()['positive'])
print(df['sentiment'].value_counts()['negative'])

#save to csv file
df.to_csv(r"C:\temp\data-with-sentiment.csv", index=False)