OneCompiler

covid 2021.csv

145

Importpandasaspd
Importnltk
Fromnltk.sentiment.vaderimportSentimentIntensityAnalyzer
#readthedataset
Df=pd.read_csv(‘covid_2021_1.csv’)
#removenullvaluesandduplicates
Df.dropna(inplace=True)
Df.drop_duplicates(subset=’Comment’,inplace=True)
#tokenizecommentsinwords
Nltk.download(‘punkt’)
Df[‘tokens’]=df[‘Comment’].apply(nltk.word_tokenize)
#performsentimentanalysis
Nltk.download(‘vader_lexicon’)
Sia=SentimentIntensityAnalyzer()
Df[‘sentiment’]=df[‘Comment’].apply(lambdax:sia.polarity_scores(x)[‘compound’])
#calculatepercentageofpositive,negative,andneutralcomments
Total_comments=len(df)
Positive_comments=len(df[df[‘sentiment’]>0])
Negative_comments=len(df[df[‘sentiment’]<0])
Neutral_comments=len(df[df[‘sentiment’]==0])
Positive_percentage=(positive_comments/total_comments)*100
Negative_percentage=(negative_comments/total_comments)*100
Neutral_percentage=(neutral_comments/total_comments)*100
#printtheresults
Print(‘TotalComments:’,total_comments)
Print(‘PositiveComments:’,positive_comments,‘(‘,positive_percentage,‘%)’)
Print(‘NegativeComments:’,negative_comments,‘(‘,negative_percentage,‘%)’)
Print(‘NeutralComments:’,neutral_comments,‘(‘,neutral_percentage,‘%)’