-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplication_ces.py
More file actions
272 lines (213 loc) · 8.59 KB
/
application_ces.py
File metadata and controls
272 lines (213 loc) · 8.59 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# -*- coding: utf-8 -*-
import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output, State, Event
import plotly.graph_objs as go
import plotly.figure_factory as ff
import pandas as pd
import numpy as np
import pickle
# application = dash.Dash(__name__)
# server = application.server
app = dash.Dash(__name__)
application = app.server
#import flask
#server = flask.Flask(__name__)
#app = dash.Dash(__name__, server=server)
app.config.supress_callback_exceptions=True
app.title='Gutenberg App (Clarence Stephen)'
############################################################################
# Load the initial data from pickle
# Set the book that initially loads
initial_novel_index=0
# pickloader = open("./04_Project_4/clean_books.pkl","rb")
# clean_books = pickle.load(pickloader)
# pickloader.close()
pickloader = open('book_titles'+'.pkl', 'rb')
novel_names = pickle.load(pickloader)
pickloader.close()
# pickloader = open('/books/'+'complexity'+'.pkl', 'rb')
# complexities = pickle.load(pickloader)
# pickloader.close()
pickloader = open('book_clusters'+'.pkl', 'rb')
book_clusters = pickle.load(pickloader)
pickloader.close()
pickloader = open('reccomendation'+'.pkl', 'rb')
reccomendation_df = pickle.load(pickloader)
pickloader.close()
# hist_data = [complexities]
# group_labels = ['distplot']
# fig = ff.create_distplot(hist_data, group_labels)
# fig.layout.update({'title': 'Distribution of total complexity scores'})
# min_complexity = min(complexities)
# max_complexity = max(complexities)
############################################################################
app.layout = html.Div(className="gallery", children=[
# Header:
html.Div([
html.H1('Project Gutenberg (Clarence Stephen)',style={'textAlign': 'center'}),
html.Div(
html.H2(['Visualize narrative patterns in ',
html.A('Project Gutenberg' , href='http://www.gutenberg.org/'),
' books.'],style={'textAlign': 'center'}),
)
]),
# Dropdown:
html.Div([
dcc.Location(id='dropdown_selection', refresh=False),
html.Div(id='page-content')
]),
# Spacer:
html.Div([
html.H3(' ',style={'textAlign': 'center'})
]),
html.Div([
dcc.Location(id='complexity-message-div', refresh=False),
html.Div(id='page-content-complexity')
]),
# Spacer:
html.Div([
html.H3(' ',style={'textAlign': 'center'})
]),
html.Div([
dcc.Location(id='genre-message-div', refresh=False),
html.Div(id='page-content-genre')
]),
# Spacer:
html.Div([
html.H3(' ',style={'textAlign': 'center'})
]),
html.Div([
dcc.Location(id='random-books-message-div', refresh=False),
html.Div(id='page-content-book-suggest', )
]),
# Graph 1:
html.Div([
dcc.Graph(id='sentiment-graph')
]),
# Graph 2:
html.Div([
dcc.Graph(id='topic-graph')
]),
# # Graph 3:
# html.Div([
# dcc.Graph(id='complexity-histogram-graph',
# figure=fig
# )
# ]),
# Footer:
html.Div([
dcc.Markdown('''
#### Project Gutenberg
This project visualizes narrative patterns in [Project Gutenberg](http://www.gutenberg.org/) books.
**clarence stephen**
Check out my [blog](http://wwww.cognosis.solutions).
Check out my [Github](https://github.com/clarencestephen).
Check out my [LinkedIn](https://www.linkedin.com/in/clarencestephen/).
''')
],style={'textAlign': 'right'}),
])
##############################################################################
# html.Div([html.H1(print('Total "complexity":', sum([delta-0.85 if delta>0 else 0 for delta in topic_deltas])))])
@app.callback(Output('page-content', 'children'),[Input('dropdown_selection', 'pathname')])
def generate_layout(url):
return html.Div([
html.Label('Select the book you wish to view:'),
dcc.Dropdown(
options=[{"label": novel_names[i],"value": i} for i in range(len(novel_names))],
value=initial_novel_index,
multi=False,
id='input'
)
])
@app.callback(Output('page-content-complexity', 'children'),[Input('complexity-message-div', 'pathname')])
def generate_layout(url):
return html.Div([
html.Div(id='complexity-message')
])
@app.callback(Output('page-content-genre', 'children'),[Input('genre-message-div', 'pathname')])
def generate_layout(url):
return html.Div([
html.Div(id='genre-message')
])
@app.callback(Output('page-content-book-suggest', 'children'),[Input('random-books-message-div', 'pathname')])
def generate_layout(url):
return html.Div([
html.Div(id='random-books-message')
])
# @app.callback(Output('page-content2', 'children'),[Input('dropdown_selection', 'pathname')])
# def generate_layout():
# return html.Div([
# html.Div(id='complexity-message')
# ])
@app.callback(Output('random-books-message', 'children'), [Input('input', 'value')])
def display_output(novel_index):
bookslist = list(reccomendation_df[novel_names[novel_index]].sort_values(ascending=False)[1:6].index)
return(f"Books like this one: {(', ').join(bookslist)}\n")
# @app.callback(Output('random-books-message', 'children'), [Input('input', 'value')])
# def display_output(novel_index):
# currentcluster = int(book_clusters[book_clusters['book'] == novel_names[novel_index]]['cluster'])
# bookslist = list(book_clusters[book_clusters['cluster']==currentcluster].sample(5).book)
# return(f"Five random books from this genre: {(', ').join(bookslist)}\n")
@app.callback(Output('genre-message', 'children'), [Input('input', 'value')])
def display_output(novel_index):
wordslist = list(book_clusters[book_clusters['book'] == novel_names[novel_index]]['max_topic_cluster_words'])[0]
return(f"Words associated with this genre: {(', ').join(wordslist)}\n")
# @app.callback(Output('output object id', 'children'), [Input('input object id', 'variable from input object id')])
@app.callback(Output('complexity-message', 'children'), [Input('input', 'value')])
def display_output(novel_index):
pickloader = open('./books/'+str(novel_index)+'/topic_deltas'+'.pkl', 'rb')
topic_deltas = pickle.load(pickloader)
pickloader.close()
sumcomplexity = sum([delta-0.75 if delta>0 else 0 for delta in topic_deltas])
return(f'Total "complexity": %.3f (range %.3f - %.3f)' % (sumcomplexity,min_complexity,max_complexity) )
@app.callback(Output('topic-graph','figure'),[Input('input','value')])
def update_graph(novel_index):
# print(book_name)
# selected_index = novel_names.index(book_name)
# choosing the book:
# novel_index=selected_index
# print('Novel name:', novel_names[novel_index])
pickloader = open('./books/'+str(novel_index)+'/topic_deltas'+'.pkl', 'rb')
topic_deltas = pickle.load(pickloader)
pickloader.close()
y_pos_topic = np.arange(len(topic_deltas))
# print('Total "complexity":', sum([delta-0.85 if delta>0 else 0 for delta in topic_deltas]))
return {'data': [
go.Bar(
x=list(y_pos_topic),
y=[delta-0.75 if delta>0 else 0 for delta in topic_deltas],
opacity=0.7,
)],
'layout': go.Layout(
title=f'Topic change distance for {novel_names[novel_index]}',
xaxis={'title': 'Exposition --> Rising --> Climax --> Falling --> Resolution'},
yaxis={'title': "Topic change distance"},
hovermode='closest'
)}
@app.callback(Output('sentiment-graph','figure'),[Input('input','value')])
def update_graph(novel_index):
# Updating the sentiment graph:
# selected_index = novel_names.index(book_name)
# novel_index=selected_index
pickloader = open('./books/'+str(novel_index)+'/sentiments_binned'+'.pkl', 'rb')
sentiments_binned = pickle.load(pickloader)
pickloader.close()
y_pos = np.arange(len(sentiments_binned))
return {'data': [
go.Bar(
x=list(y_pos),
y=sentiments_binned,
opacity=0.7,
)],
'layout': go.Layout(
title=f'Sentiment shape over the course of the narrative: {novel_names[novel_index]}',
xaxis={'title': 'Exposition --> Rising --> Climax --> Falling --> Resolution'},
yaxis={'title': 'Sentiment polarity'},
hovermode='closest'
)}
if __name__ == '__main__':
# change host from the default to '0.0.0.0' to make it publicly available
app.server.run(port=8484, host='0.0.0.0')
application.run(debug=True)