-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentiment_analysis.py
35 lines (27 loc) · 1.01 KB
/
sentiment_analysis.py
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
27
28
29
30
31
32
33
34
35
import pandas as pd
import numpy as np
'''
This function uses the split data (train, validate, test)
and turn the overall rating to sentiment values.
Convert the sentiment values to sentiment statements
whether they are positive, neutral, or negative.
Returns each split data with the new sentiment statements
as a new column.
Inputs:
train, validate, test
Outputs:
train, validate, test
'''
def sentiment_analysis(rating):
if (rating == 5) or (rating == 4):
return 'Positive'
elif rating == 3:
return 'Neutral'
elif (rating == 2) or (rating == 1):
return 'Negative'
def apply_sentiment(train, validate, test):
# Add sentiments to the data, both our train and test data
train['sentiment'] = train['df.overall'].apply(sentiment_analysis)
validate['sentiment'] = validate['df.overall'].apply(sentiment_analysis)
test['sentiment'] = test['df.overall'].apply(sentiment_analysis)
return train, validate, test