-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsenti_analysis_widget.py
142 lines (115 loc) · 5.28 KB
/
senti_analysis_widget.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from posixpath import join
import ipywidgets as widgets
from ipywidgets import VBox, HBox
import os
class SentiAnalysisWidget():
'''A sentiment analysis widget which classifies each user's message into one of five emotions.
'''
def __init__(self, sentiments=['joy', 'sadness', 'fear', 'anger', 'neutral'], pic_size=420):
'''Initalizes the sentiment analysis widget.
:param sentiments: list of sentiments, defaults to ['joy', 'sadness', 'fear', 'anger', 'neutral']
:type sentiments: list, optional
:param pic_size: The size of the picture, defaults to 420
:type pic_size: int, optional
'''
self.pic_size = pic_size
self.sentiments = sentiments
self.num_senti = len(self.sentiments)
self.cached_path = "./nlp_suite/sentiment_analysis/cached_user"
self.users = os.listdir(self.cached_path)
self.user_emo_html = """
<font size="5">
<p><b>Username:</b> {}</p>
<p><b>General emotion:</b> {}</p>
</font>
"""
self.user_emo = widgets.HTML(self.user_emo_html.format("Unknown", "Unknown"))
self.empty_graph = "./Graphics/loading2.png"
with open(self.empty_graph, "rb") as f_0:
self.emotion_distribution_pi = widgets.Image(
value=f_0.read(),
format=self.empty_graph[-3:],
width=self.pic_size,
height=self.pic_size
)
with open(self.empty_graph, "rb") as f_0:
self.emotion_distribution_radar = widgets.Image(
value=f_0.read(),
format=self.empty_graph[-3:],
width=self.pic_size,
height=self.pic_size
)
self.top5_title = widgets.HTML("""<font size="5"><p><b>Show Top 5 sentences of:</b></p></font>""")
self.top5_sent = [""] * 5
self.top5_widget_HTML = '<font size="4"><b>{}</b></font><br><font size="3"><ul>{}</ul></font><br>'
self.top5_widget = widgets.HTML("<p>No sentence found</p>")
self.senti_boxes = [
widgets.Checkbox(
description=sent.capitalize(),
disabled=True,
indent=True,
value=False
) for sent in self.sentiments
]
for i, box in enumerate(self.senti_boxes):
box.observe(self.update_selection)
self.widget = VBox([
self.user_emo,
widgets.HTML('<font size="5"> <br> <b> Emotion distribution charts (Pie, Radar) </b> <br> <br> </font>'),
HBox([
self.emotion_distribution_pi,
self.emotion_distribution_radar
]),
widgets.HTML('<br>'),
widgets.HTML('<br>'),
self.top5_title,
HBox(self.senti_boxes),
self.top5_widget
])
def get_widget(self):
"""Returns the underlying ipywidget
:return: the sentiment analysis ipywidget
:rtype: ipywidgets.HTML
"""
return self.widget
def update_selection(self, button):
'''Updates the sentiment selection.
:param button: A button instance object.
:type button: ipywidgets.Button
'''
top5 = ""
for i, box in enumerate(self.senti_boxes):
if box.value:
if box.description == self.gen_emo.capitalize():
top5 = top5 + "<p>" + self.top5_widget_HTML.format(
self.sentiments[i].capitalize() + " <font color='blue'>(main sentiment)</font>",
"<li>" + "</li> <li>".join(self.top5_sent[i]) + "</li>"
) + "</p>"
else:
top5 = top5 + "<p>" + self.top5_widget_HTML.format(
self.sentiments[i].capitalize(),
"<li>" + "</li> <li>".join(self.top5_sent[i]) + "</li>"
) + "</p>"
self.top5_widget.value = top5
def init_widget_data(self, user_info):
'''Initalizes the chatbot with user-specific info.
:param user_info: Pandas Dataframe
:type user_info: pd.DataFrame
'''
assert user_info["user_name"] in self.users, "unknown user"
user_path = os.path.join(self.cached_path, user_info["user_name"])
with open(os.path.join(user_path, "label_result.txt"), "r") as f_eg:
gen_emo = f_eg.readline().strip()
with open(os.path.join(user_path, "pie_result.png"), "rb") as f_ed:
self.emotion_distribution_pi.value = f_ed.read()
with open(os.path.join(user_path, "radar_result.png"), "rb") as f_er:
self.emotion_distribution_radar.value = f_er.read()
with open(os.path.join(user_path, "top5_result.txt"), "r") as f_t5s:
top5_sent = [line.strip() for line in f_t5s.readlines()]
self.top5_sent = [top5_sent[i + 1 : i + self.num_senti + 1] for i in range(0, len(top5_sent), self.num_senti + 1)]
self.user_emo.value = self.user_emo_html.format(user_info["user_name"], gen_emo.capitalize())
self.gen_emo = gen_emo
for i, box in enumerate(self.senti_boxes):
box.disabled = False
if box.description == gen_emo.capitalize():
box.value = True