-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_emotion.py
54 lines (35 loc) · 1.17 KB
/
set_emotion.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# importing pandas as pd
import pandas as pd
from nrclex import NRCLex
# Creating the sentiment
def e_emote(text):
# Create object
emotion = NRCLex(text).raw_emotion_scores
if(len(emotion) > 0):
highest = analysis(emotion)
return str(highest)
return "0"
# function to analyze the reviews
def analysis(score):
# taking list of car values in v
v = list(score.values())
# taking list of car keys in v
k = list(score.keys())
# get largest score in dictionary
score = k[v.index(max(v))]
if(score == "positive"):
score = "anticipation"
elif(score == "negative"):
score = "sadness"
return str(score)
if __name__ == "__main__":
df = pd.DataFrame(pd.read_csv("headLines.csv"))
# creating a data frame
#df = pd.DataFrame([csv_reader], index = None)
#df.drop(columns = "no", axis="columns", inplace=True)
df["Emotion"] = df.apply(lambda row: e_emote(row.Headline), axis="columns")
print(df.head)
# Print the dataframe
print(df.head())
# saving the dataframe
df.to_csv('file3.csv', index=False)