-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvader_classifier.py
More file actions
34 lines (22 loc) · 940 Bytes
/
Copy pathvader_classifier.py
File metadata and controls
34 lines (22 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import nltk
import pandas as pd
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
from nltk.sentiment.vader import SentimentIntensityAnalyzer
sia= SentimentIntensityAnalyzer()
df= pd.read_csv("amazonreviews.tsv", sep= '\t')
df.dropna(inplace=True)
spaces=[]
for index, label, review in df.itertuples():
if type(review)==str:
if review.isspace():
spaces.append(review)
df.drop(spaces, inplace=True)
#check polarity score for first review
print(sia.polarity_scores(df.iloc[0]['review']))
df['scores']= df['review'].apply(lambda review: sia.polarity_scores(review))
df['compound']= df['scores'].apply(lambda d:d['compound'])
df['polarity']= df['compound'].apply(lambda compound: 'pos' if compound>=0 else 'neg')
print(df.head())
print(accuracy_score(df['label'], df['polarity']))
print(confusion_matrix(df['label'], df['polarity']))
print(classification_report(df['label'], df['polarity']))