-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmotionAnalysis.py
35 lines (27 loc) · 1.26 KB
/
EmotionAnalysis.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
from nrclex import NRCLex
import json
import pandas as pd
import plotly
import plotly.express as px
from plotly.graph_objs import *
def emotionalanalysis(data):
# converting df to json format
json = data.to_json()
# instantiate NRCLex object in json format
text_object = NRCLex(json)
# utilise the library to extract raw emotion score from data
emotion = text_object.raw_emotion_scores
# converting emotion scores into a data frame
emotion_df = pd.DataFrame.from_dict(emotion, orient='index')
emotion_df = emotion_df.reset_index()
emotion_df = emotion_df.rename(columns={'index': 'Emotion Classification', 0: 'Emotion Count'})
emotion_df = emotion_df.sort_values(by=['Emotion Count'], ascending=False)
# sorting and visualising the scores into a bar graph
fig = px.bar(emotion_df, x='Emotion Classification', y='Emotion Count', color='Emotion Classification',
orientation='v', width=800, height=400)
# direc="static/bubble.png"
direc = "templates/emotions.html"
plotly.io.orca.config.executable = r"C:\Users\glady\AppData\Local\Programs\orca\orca.exe"
plotly.io.orca.config.save()
fig.write_html(direc)
# figure.write_image(direc,"png",engine="orca")