-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment_grapher.py
169 lines (135 loc) · 4.93 KB
/
sentiment_grapher.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State, Event
import pandas as pd
import sqlite3
from functools import reduce
import ast
#from fetch_active_adsets import fetch_ads_comments
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
conn = sqlite3.connect('adcomments.db',check_same_thread=False)
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
df = pd.read_sql_query('SELECT * FROM adcomments', conn)
def fetch_latest_comments():
print('fetching latest comments')
conn = sqlite3.connect('adcomments.db',check_same_thread=False)
fetch_ads_comments(['live_booker'])
df = pd.read_sql_query('SELECT * FROM adcomments', conn)
print('latest comments fetched')
dropdown_list = []
i = 1
def get_dropdown_list(df):
for index, row in df.iterrows():
dropdown_list.append({'label':row['adset_name'].replace('_',' '),'value':index + 1})
print(dropdown_list)
return dropdown_list
polarity_counts_list = []
def get_polarity_counts(df):
for index, row in df.iterrows():
polarity_counts_list.append([row['number_neg_comments'], row['number_pos_comments'], row['number_neutral_comments']])
print(polarity_counts_list)
return polarity_counts_list
def fetch_negative_comments(df, value):
sentences = df.iloc[value -1]["negative_messages_list"]
print(sentences)
neg_comments_list = ast.literal_eval(sentences)
return neg_comments_list
def fetch_positive_comments(df, value):
sentences = df.iloc[value -1]['positive_messages_list']
pos_comments_list = ast.literal_eval(sentences)
return pos_comments_list
"""
App layout
"""
# html.Button('Fetch latest comments', id='fetch-comments-button',type='submit',style={"background-color":"rgb(119,136,153)", "color":"white", "margin-top":"15px",}),
app.layout = html.Div(children=[
html.Div(id='target',
className='container'),
html.H1(children="Ads Sentiment Analyzer",className='container',style={"margin-top":"25px", "margin-bottom":"25px"}),
html.Div(
dcc.Dropdown(
id='ad-dropdown',
options=get_dropdown_list(df),
style={'margin-bottom':'35px',"margin-left":"auto","margin-right":"auto","width":"1000px"},
value=1)),
html.Div(id='output',className='container'),
])
"""
App callbacks
"""
@app.callback(
Output(component_id='output', component_property='children'),
[Input(component_id='ad-dropdown', component_property='value')])
def update_graph(value):
data = [
{
'values': get_polarity_counts(df)[int(value)-1],
'type':'pie',
"marker": {
"colors": ['rgb(178,34,34)','rgb(107,142,35)','rgb(119,136,153)']
},
"hole": .5,
"labels":['Negative comments','Positive comments','Neutral comments'],
"textfont": {
"size":18
},
'textposition':'inside',
},
]
graphs = []
graphs.append(html.Div([
dcc.Graph(
id='graph',
figure={
'data': data,
'layout': {
'margin': {
'l': 30,
'r': 0,
'b': 30,
't': 0
},
"annotations": [
{
"font": {
"size": 20
},
"showarrow": False,
"text": str(reduce(lambda x, y: x + y, get_polarity_counts(df)[int(value)-1])) + " Comments",
}
]
}
}
)
]))
# for i in range(len(fetch_negative_comments(df, value)[:3])):
# graphs.append(html.Div(children=[
# html.P(
# id="text-area-neg",
# children=fetch_negative_comments(df,value)[i],
# style={
# "color":"rgb(178,34,34)"
# },
# )
# ]))
# for i in range(len(fetch_positive_comments(df, value)[:3])):
# graphs.append(html.Div(children=[
# html.P(
# id="text-area-pos",
# children=fetch_positive_comments(df,value)[i],
# style={
# "color":"rgb(107,142,35)"
# },
# )
# ]))
return html.Div(graphs)
# @app.callback(Output('target', 'children'), [Input('fetch-comments-button','n_clicks')], [State('fetch-comments-button', 'value')], [Event('fetch-comments-button', 'click')])
# def on_click(n_clicks,input1):
# if n_clicks is None:
# nclicks_bt1 = 0
# else:
# return fetch_latest_comments()
if __name__ == '__main__':
app.run_server(debug=True)