-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
898 lines (813 loc) · 32.9 KB
/
app.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np
from dash.dependencies import Output, State, Input
import dash_table
import base64
import datetime
import io
from datetime import datetime
from sentimentAnalysisUtil import stemmed_words,removeStopwords
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
categories = ['all','cameras', 'laptops', 'mobile phone']
tableView = pd.DataFrame(columns=['review', 'category', 'sentiment'])
brands = ['sony', 'nokia','samsung']
allBrands = []
displaySentByCategoryX = []
displaySentByCategoryY = []
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
colors = {
'background': '#111111',
'text': '#7FDBFF'
}
app.layout = html.Div(style={'backgroundColor': colors['background']}, children=[
html.H1(
children='Text-Mining Dashboard',
style={
'textAlign': 'center',
'color': colors['text']
}),
html.Div([
html.H2(
children='Enter Review:',
style={
'textAlign': 'center',
'color': colors['text']
}),
dcc.Input(id='review', value='', type='text'),
html.Button(id='submit-button', type='submit', children='Submit'),
html.Div(id='output_div')
],
style = {
'textAlign': 'center'
}
),
# html.Div(children = [
# html.H4(children='Output'),
# generateTable(tableView)
# ]),
html.Div([
html.H2(
children='Upload CSV File: ',
style={
'textAlign': 'center',
'color': colors['text']
}
),
dcc.Upload(
id='upload-data',
children=html.Div([
'Drag and Drop or ',
html.A('Select Files')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px',
'color': 'white'
},
# Allow multiple files to be uploaded
multiple=True
),
html.Div(id='output-data-upload')
]),
# dcc.Dropdown(
# id='category_dropdown_bar',
# options=[{'label':category, 'value':category} for category in categories],
# value=categories[0]
# ),
# dcc.Graph(
# id='sentiment analysis by category',
# figure={
# 'data' : [
# go.Bar(
# {'x': categories, 'y': [20, 14, 23, 40], 'name': 'positive sentiment'}
# ),
# go.Bar(
# {'x': categories, 'y': [-15, -7, -13, -20], 'name': 'negative sentiment'}
# )
# ],
# 'layout': {
# 'barmode': 'group',
# 'title': 'Dash Data Visualization',
# 'plot_bgcolor': colors['background'],
# 'paper_bgcolor': colors['background'],
# 'font': {
# 'color': colors['text']
# }
# }
# }
# ),
# drop down list to select which category do you want to look at
html.Div([
html.H2(
children = 'Select Your Category: ',
style={
'textAlign': 'center',
'color': colors['text']
}
),
dcc.Dropdown(
id='category_dropdown',
options=[{'label':category, 'value':category} for category in categories],
value=categories[0]
)
]),
dcc.Graph(
id='sentiment analysis time series analysis (Laptop)',
),
dcc.Graph(
id='Pie Chart',
)
# dcc.Graph(
# id='example-graph-2',
# figure={
# 'data': [
# {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
# {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
# ],
# 'layout': {
# 'title': 'Dash Data Visualization',
# 'plot_bgcolor': colors['background'],
# 'paper_bgcolor': colors['background'],
# 'font': {
# 'color': colors['text']
# }
# }
# }
# )
])
# tableView = pd.DataFrame(columns=['review', 'category', 'sentiment'])
@app.callback(Output('sentiment analysis time series analysis (Laptop)', 'figure'),
[Input('category_dropdown', 'value')],
)
def update_graph(selected_dropdown_value):
main_df = pd.read_csv("data/preprocessed_reviewinfo.csv", index_col=False)
main_df['new_Date'] = pd.to_datetime(main_df["Date"], format = '%B %d, %Y')
if selected_dropdown_value !="all":
main_df= main_df[main_df['category']== selected_dropdown_value]
positive_df = main_df.loc[main_df['polarity'] == 1].groupby(main_df.new_Date).agg('count')['Date'].reset_index()
negative_df = main_df.loc[main_df['polarity'] == 0].groupby(main_df.new_Date).agg('count')['Date'].reset_index()
return {
'data' : [
{'x':positive_df['new_Date'], 'y': positive_df['Date'] ,'name':'positive sentiment'}
,{'x':negative_df['new_Date'], 'y': negative_df['Date'] ,'name':'negative sentiment'}
],
'layout': {
'barmode': 'group',
'title': 'Time Series Sentiment Analysis (' +selected_dropdown_value+")",
'plot_bgcolor': colors['background'],
'paper_bgcolor': colors['background'],
'font': {
'color': colors['text']
}
}
}
@app.callback(Output('Pie Chart', 'figure'),
[Input('category_dropdown', 'value')],
)
def updatePieChart(value):
main_df = pd.read_csv("data/preprocessed_reviewinfo.csv", index_col=False)
if value !="all":
main_df= main_df[main_df['category']== value]
positive_df = main_df.loc[main_df['polarity'] == 1].agg('count')['Content']
negative_df = main_df.loc[main_df['polarity'] == 0].agg('count')['Content']
print(positive_df)
return {
'data': [
go.Pie(
values=[positive_df, negative_df],
labels=['positive','negative']
)],
'layout': {
'title': 'Pie Chart (' +value+")",
'plot_bgcolor': colors['background'],
'paper_bgcolor': colors['background'],
'font': {
'color': colors['text']
}
}
}
# tableView = pd.DataFrame(columns=['review', 'category', 'sentiment'])
@app.callback(Output('output_div', 'children'),
[Input('submit-button', 'n_clicks')],
[State('review', 'value')]
# [State('review', 'value')]
# [Event('submit-button', 'click')]
)
def update_output(n_clicks, review):
# tableView = pd.DataFrame(columns=['review', 'category', 'sentiment'])
if review == '':
return
# run review through sentiment
sentiment = runModelSentiment(review)
# # run review through category classification
category = runModelCategoryClassification(review)
# print('table view: ')
# print(tableView.columns)
row = pd.Series([review, category, sentiment], index=tableView.columns)
view = tableView.append(row, ignore_index = True)
print(row)
table = html.Div([
dash_table.DataTable(
data=view.to_dict('rows'),
columns=[{'id': c, 'name': c} for c in view.columns],
style_as_list_view=True,
# n_fixed_columns=3,
style_cell={
'padding': '5px',
'backgroundColor': '#111111',
'textAlign': 'left',
'color': '#7FDBFF',
},
style_header={
'backgroundColor': '#111111',
'fontWeight': 'bold',
'color': '#7FDBFF',
'maxWidth': '180px'
},
style_cell_conditional=[
{
'backgroundColor': '#111111',
'if': {'column_id': c},
'textAlign': 'left',
'color': '#7FDBFF'
} for c in view.columns
],
style_table={
'maxHeight': '300'
},
)
])
return table
def parse_contents(contents, filename, date):
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
try:
if 'csv' in filename:
# Assume that the user uploaded a CSV file
df = pd.read_csv(
io.StringIO(decoded.decode('utf-8')))
print('df:')
print(df.columns)
elif 'xls' in filename:
# Assume that the user uploaded an excel file
df = pd.read_excel(io.BytesIO(decoded))
except Exception as e:
print(e)
return html.Div([
'There was an error processing this file.'
])
positive = {}
negative = {}
# intiate the categories count in both positive and negative dict
for category in categories:
positive[category] = 0
negative[category] = 0
positiveReviews=[]
negativeReviews=[]
# allBrands={}
# for brand in brands:
# allBrands[brand]={}
# allBrands[brand]['positive'] = pd.DataFrame(columns=['Review'])
# allBrands[brand]['negative'] = pd.DataFrame(columns=['Review'])
for row in df.iterrows():
review = row[1]['Content']
sentiment = runModelSentiment(review)
category = runModelCategoryClassification(review)
if sentiment == 'positive':
currentNumInCategory = positive[category]
positive[category] = currentNumInCategory + 1
positiveReviews.append(review)
# updatedReview = review.lower()
# for brand in brands:
# b = allBrands[brand]
# if brand in updatedReview:
# b['positive'].append(pd.Series([review], index=b['positive'].columns),ignore_index = True)
# print('positive')
else:
currentNumInCategory = negative[category]
negative[category] = currentNumInCategory - 1
negativeReviews.append(review)
# updatedReview = review.lower()
# for brand in brands:
# b = allBrands[brand]
# if brand in updatedReview:
# b['negative'].append(pd.Series([review], index=b['negative'].columns),ignore_index = True)
# print('negative')
# print('all brands: ')
# print(allBrands['sony']['positive'])
numSampleReviews = 5
positiveReviewsToBeDisplayed = pd.DataFrame(columns=['Review'])
negativeReviewsToBeDisplayed = pd.DataFrame(columns=['Review'])
# displaying entered number of Sample Reviews for both positive and negative sentiments
for review in positiveReviews[0:numSampleReviews]:
row = pd.Series([review], index=positiveReviewsToBeDisplayed.columns)
positiveReviewsToBeDisplayed = positiveReviewsToBeDisplayed.append(row,ignore_index = True)
for review in negativeReviews[0:numSampleReviews]:
row = pd.Series([review], index=negativeReviewsToBeDisplayed.columns)
negativeReviewsToBeDisplayed = negativeReviewsToBeDisplayed.append(row,ignore_index = True)
#Analysing features of positive and negative reviews
positiveReviews = removeStopwords(positiveReviews)
negativeReviews = removeStopwords(negativeReviews)
positive_features_df = pd.DataFrame(columns=['term','weight'])
negative_features_df = pd.DataFrame(columns=['term','weight'])
if len(positiveReviews) > 0:
#for positive reviews
count_vect_pos = CountVectorizer(max_features=5000, lowercase=True, ngram_range=(1,2))
vectorizer_matrix_pos = count_vect_pos.fit_transform(positiveReviews)
tfidf_transformer_pos = TfidfTransformer(use_idf=True, smooth_idf=True)
tfidf_pos = tfidf_transformer_pos.fit_transform(vectorizer_matrix_pos)
# df = pd.DataFrame(tfidf.toarray(), columns = count_vect.get_feature_names())
# print(df)
weights_pos = np.asarray(tfidf_pos.mean(axis=0)).ravel().tolist()
weights_df_pos = pd.DataFrame({'term': count_vect_pos.get_feature_names(), 'weight': weights_pos})
positive_features_df =(weights_df_pos.sort_values(by='weight', ascending=False).head(20))
# print('positive: ')
# print(positive_features_df)
if len(negativeReviews) > 0:
#for negative reviews
count_vect_neg = CountVectorizer(max_features=5000, lowercase=True, ngram_range=(1,2))
vectorizer_matrix_neg = count_vect_neg.fit_transform(negativeReviews)
tfidf_transformer_neg = TfidfTransformer(use_idf=True, smooth_idf=True)
tfidf_neg = tfidf_transformer_neg.fit_transform(vectorizer_matrix_neg)
# df = pd.DataFrame(tfidf.toarray(), columns = count_vect.get_feature_names())
# print(df)
weights_neg = np.asarray(tfidf_neg.mean(axis=0)).ravel().tolist()
weights_df_neg = pd.DataFrame({'term': count_vect_neg.get_feature_names(), 'weight': weights_neg})
negative_features_df =(weights_df_neg.sort_values(by='weight', ascending=False).head(20))
categoryFeatures_df = getCategoryFeatures(numSampleReviews)
# print('negative: ')
# print(negative_features_df)
# print("Positive Features:")
# print(positive_features_df)
# print("Negative Features:")
# print(negative_features_df)
# dictToReturn = {}
# dictToReturn['positive'] = positive
# dictToReturn['negative'] = negative
return positive,negative,positive_features_df,negative_features_df,positiveReviewsToBeDisplayed,negativeReviewsToBeDisplayed, categoryFeatures_df, allBrands
# return html.Div([
# html.H5(filename),
# html.H6(datetime.datetime.fromtimestamp(date)),
# dash_table.DataTable(
# data=df.to_dict('rows'),
# columns=[{'name': i, 'id': i} for i in df.columns]
# html.Hr(), # horizontal line
# # For debugging, display the raw contents provided by the web browser
# html.Div('Raw Content'),
# html.Pre(contents[0:200] + '...', style={
# 'whiteSpace': 'pre-wrap',
# 'wordBreak': 'break-all'
# })
# ])
@app.callback(Output('output-data-upload', 'children'),
[Input('upload-data', 'contents')],
[State('upload-data', 'filename'),
State('upload-data', 'last_modified')])
def update_upload(list_of_contents, list_of_names, list_of_dates):
if list_of_contents is not None:
children = [
parse_contents(c, n, d) for c, n, d in zip(list_of_contents, list_of_names, list_of_dates)]
# print(children)
return generateDisplay(children[0][0],children[0][1], children[0][2], children[0][3],children[0][4],children[0][5],children[0][6],children[0][7])
def generateDisplay(positive,negative,positive_features_df,negative_features_df,positiveReviewsToBeDisplayed,negativeReviewsToBeDisplayed,categoryFeatures_df,allBrands):
positive_values = [positive[category] for category in categories]
negative_values = [negative[category] for category in categories]
# print(positive)
# print('---------------------')
# print(negative)
# return 'nothing'
return html.Div([
dcc.Graph(
id='sentiment analysis by category',
figure = {
'data' : [
go.Bar(
{'x': categories[1:], 'y': positive_values[1:], 'name': 'positive sentiment'}
),
go.Bar(
{'x': categories[1:], 'y': negative_values[1:], 'name': 'negative sentiment'}
)
],
'layout': {
'barmode': 'group',
'title': 'Sentiment Distribution by Category',
'plot_bgcolor': colors['background'],
'paper_bgcolor': colors['background'],
'font': {
'color': colors['text']
}
}
}
),
html.Div([
html.H2(
children='Sample Positive Reviews',
style={
'textAlign': 'center',
'color': colors['text']
}),
dash_table.DataTable(
data=positiveReviewsToBeDisplayed.to_dict('rows'),
columns=[{'id': c, 'name': c} for c in positiveReviewsToBeDisplayed.columns],
style_as_list_view=True,
# n_fixed_columns=3,
style_cell={
'padding': '5px',
'backgroundColor': '#111111',
'textAlign': 'left',
'color': '#7FDBFF',
'textOverflow': 'ellipsis'
},
style_header={
'backgroundColor': '#111111',
'fontWeight': 'bold',
'color': '#7FDBFF',
'maxWidth': '180px'
},
style_table={
'maxHeight': '500',
'overflowY': 'scroll'
}
# style_cell_conditional=[
# {
# 'backgroundColor': '#111111',
# 'if': {'column_id': c},
# 'textAlign': 'left',
# 'color': '#7FDBFF'
# } for c in positive_features_df.columns
# ]
)
]),
html.Div([
html.H2(
children='Sample Negative Reviews',
style={
'textAlign': 'center',
'color': colors['text']
}),
dash_table.DataTable(
data=negativeReviewsToBeDisplayed.to_dict('rows'),
columns=[{'id': c, 'name': c} for c in negativeReviewsToBeDisplayed.columns],
style_as_list_view=True,
# n_fixed_columns=3,
style_cell={
'padding': '5px',
'backgroundColor': '#111111',
'textAlign': 'left',
'color': '#7FDBFF',
'textOverflow': 'ellipsis'
},
style_header={
'backgroundColor': '#111111',
'fontWeight': 'bold',
'color': '#7FDBFF',
'maxWidth': '180px'
},
style_table={
'maxHeight': '500',
'overflowY': 'scroll'
}
# style_cell_conditional=[
# {
# 'backgroundColor': '#111111',
# 'if': {'column_id': c},
# 'textAlign': 'left',
# 'color': '#7FDBFF'
# } for c in positive_features_df.columns
# ]
)
]),
# data table for positive features
html.Div([
html.H2(
children='Positive Features',
style={
'textAlign': 'center',
'color': colors['text']
}),
dash_table.DataTable(
data=positive_features_df.to_dict('rows'),
columns=[{'id': c, 'name': c} for c in positive_features_df.columns],
style_as_list_view=True,
# n_fixed_columns=3,
style_cell={
'padding': '5px',
'backgroundColor': '#111111',
'textAlign': 'left',
'color': '#7FDBFF',
'maxWidth': '180px'
},
style_header={
'backgroundColor': '#111111',
'fontWeight': 'bold',
'color': '#7FDBFF',
'maxWidth': '180px'
},
style_table={
'maxHeight': '500',
'overflowY': 'scroll'
}
# style_cell_conditional=[
# {
# 'backgroundColor': '#111111',
# 'if': {'column_id': c},
# 'textAlign': 'left',
# 'color': '#7FDBFF'
# } for c in positive_features_df.columns
# ]
)
],
style = {'display':'inline-block', 'width': '50%'}
),
html.Div([
html.H2(
children='Negative Features',
style={
'textAlign': 'center',
'color': colors['text']
}),
dash_table.DataTable(
data=negative_features_df.to_dict('rows'),
columns=[{'id': c, 'name': c} for c in negative_features_df.columns],
style_as_list_view=True,
# n_fixed_columns=3,
style_cell={
'padding': '5px',
'backgroundColor': '#111111',
'textAlign': 'left',
'color': '#7FDBFF',
'maxWidth': '180px'
},
style_header={
'backgroundColor': '#111111',
'fontWeight': 'bold',
'color': '#7FDBFF',
'maxWidth': '180px'
},
style_table={
'maxHeight': '500',
'overflowY': 'scroll'
}
# style_cell_conditional=[
# {
# 'backgroundColor': '#111111',
# 'if': {'column_id': c},
# 'textAlign': 'left',
# 'color': '#7FDBFF'
# } for c in positive_features_df.columns
# ]
)
],
style = {'display':'inline-block', 'width': '50%'}
),
html.Div([
html.H2(
children='Sample Category Reviews',
style={
'textAlign': 'center',
'color': colors['text']
}),
dash_table.DataTable(
data=categoryFeatures_df.to_dict('rows'),
columns=[{'id': c, 'name': c} for c in categoryFeatures_df.columns],
style_as_list_view=True,
# n_fixed_columns=3,
style_cell={
'padding': '5px',
'backgroundColor': '#111111',
'textAlign': 'left',
'color': '#7FDBFF',
'textOverflow': 'ellipsis'
},
style_header={
'backgroundColor': '#111111',
'fontWeight': 'bold',
'color': '#7FDBFF',
'maxWidth': '180px'
},
style_table={
'maxHeight': '500',
'overflowY': 'scroll'
}
# style_cell_conditional=[
# {
# 'backgroundColor': '#111111',
# 'if': {'column_id': c},
# 'textAlign': 'left',
# 'color': '#7FDBFF'
# } for c in positive_features_df.columns
# ]
)
]),
# html.Div([
# html.H2(
# children = 'Select Your Brand: ',
# style={
# 'textAlign': 'center',
# 'color': colors['text']
# }
# ),
# dcc.Dropdown(
# id='brand_dropdown',
# options=[{'label':brand, 'value':brand} for brand in brands],
# value=brands[0]
# ),
# html.Div(id='brand_table')
# ])
])
# @app.callback(Output('brand_table', 'children'),
# [Input('brand_dropdown', 'value')],
# )
# def getBrandTable(input_value):
# return html.Div([
# html.H2(
# children='Positive Reviews in selected brand',
# style={
# 'textAlign': 'center',
# 'color': colors['text']
# }),
# dash_table.DataTable(
# data=allBrands[input_value]['positive'].to_dict('rows'),
# columns=[{'id': c, 'name': c} for c in allBrands[input_value][positive].columns],
# style_as_list_view=True,
# # n_fixed_columns=3,
# style_cell={
# 'padding': '5px',
# 'backgroundColor': '#111111',
# 'textAlign': 'left',
# 'color': '#7FDBFF',
# 'maxWidth': '180px'
# },
# style_header={
# 'backgroundColor': '#111111',
# 'fontWeight': 'bold',
# 'color': '#7FDBFF',
# 'maxWidth': '180px'
# },
# style_table={
# 'maxHeight': '500',
# 'overflowY': 'scroll'
# }
# # style_cell_conditional=[
# # {
# # 'backgroundColor': '#111111',
# # 'if': {'column_id': c},
# # 'textAlign': 'left',
# # 'color': '#7FDBFF'
# # } for c in positive_features_df.columns
# # ]
# )
# ])
def getCategoryFeatures(numSampleReviews):
from gensim import corpora
import sklearn
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split,ShuffleSplit
from sklearn.linear_model import LogisticRegression
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn import model_selection, naive_bayes, svm
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
import numpy as np
from sklearn.naive_bayes import MultinomialNB
from sklearn import svm
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score
import pickle
import pandas as pd
import re
import statistics
import random
#Load the saved feature extraction
classifier_saved = open("model_classification/FeatureExtraction.pickle", "rb") #binary read
classifier = pickle.load(classifier_saved)
classifier_saved.close()
# Extract the key features and put into dataframe
list_1 = classifier.most_informative_features(numSampleReviews)
df_important_features = pd.DataFrame(columns=['Feature','Cat1_Cat0','Ratio_1'])
for (fname, fval) in list_1:
cpdist = classifier._feature_probdist
def labelprob(l):
return cpdist[l, fname].prob(fval)
labels = sorted(
[l for l in classifier._labels if fval in cpdist[l, fname].samples()],
key=labelprob
)
if len(labels) == 1:
continue
l0 = labels[0]
l1 = labels[-1]
if cpdist[l0, fname].prob(fval) == 0:
ratio = 'INF'
else:
ratio = round(cpdist[l1, fname].prob(fval) / cpdist[l0, fname].prob(fval), 1)
fname = fname.replace('contains(','')
fname = fname.replace(')','')
df_important_features.loc[len(df_important_features)] = [fname, l1+" : "+l0,
str(ratio)+" : 1.0"]
print('category features: ')
print(df_important_features)
return df_important_features
def runModelSentiment(input_value):
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
import pickle
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import LogisticRegression
from sklearn import svm
import numpy as np
from sentimentAnalysisUtil import stemmed_words,removeStopwords
#preprocess
stop_words = nltk.corpus.stopwords.words('english')
stop_words += ['phone','laptop','mobile','camera','the','phones','cameras', 'laptops']
stopped_review =""
for word in input_value.split():
if word.lower() not in stop_words:
stopped_review += word+ " "
#filename = 'model_sentiment/logistic_regression_model.pk'
#filename = 'model_sentiment/nb_model.pk
filename = 'model_sentiment/svm_model.pk'
tfidf = pickle.load(open('model_sentiment/tfidf_trans.pk','rb'))
count = pickle.load(open('model_sentiment/count_vert.pk','rb'))
# load the model from disk
model = pickle.load(open(filename, 'rb'))
prediction = model.predict(tfidf.transform(count.transform([stopped_review])))
# print('predicting sentiment')
if (prediction[0] == 0):
return 'negative'
return 'positive'
def runModelCategoryClassification(input_value):
import sklearn
from sklearn.model_selection import train_test_split,ShuffleSplit
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn import model_selection, naive_bayes, svm
import numpy as np
from sklearn import svm
import pickle
import pandas as pd
# Load the classifier
classifier_saved = open("model_classification/CategoryClassifier.pickle", "rb")
classifier = pickle.load(classifier_saved)
classifier_saved.close()
#Load the saved classifier
classifier_saved = open("model_classification/TFIDF_Reviews_Category.pickle", "rb") #binary read
TFIDF_vect = pickle.load(classifier_saved)
classifier_saved.close()
#process input
review_holder = np.array([input_value])
review_holder_1 = pd.Series(review_holder)
# Transform reviews into TFIDF
test_TFIDF = TFIDF_vect.transform(review_holder_1)
prediction = classifier.predict(test_TFIDF)
# print('predicting category')
if prediction[0] == 0:
return "cameras"
elif prediction[0] == 1:
return "laptops"
else:
return "mobile phone"
# import sklearn
# from sklearn.model_selection import train_test_split,ShuffleSplit
# from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
# from sklearn.feature_extraction.text import TfidfVectorizer
# from sklearn import model_selection, naive_bayes, svm
# import numpy as np
# from sklearn import svm
# import pickle
# import pandas as pd
# # Load the classifier
# classifier_saved = open("model_classification/CategoryClassifier.pickle", "rb")
# classifier = pickle.load(classifier_saved)
# classifier_saved.close()
# #Load the saved classifier
# classifier_saved = open("model_classification/TFIDF_Reviews_Category.pickle", "rb") #binary read
# TFIDF_vect = pickle.load(classifier_saved)
# classifier_saved.close()
# #process input
# review_holder = np.array([input_value])
# review_holder_1 = pd.Series(review_holder)
# # Transform reviews into TFIDF
# review_TFIDF = TFIDF_vect.transform(review_holder_1)
# prediction = classifier.predict(review_TFIDF)
# print('predicting category')
# if prediction[0] == 0:
# return 'cameras'
# elif prediction[0] == 1:
# return 'laptops'
# else:
# return 'mobile phone'
if __name__ == '__main__':
app.run_server(debug=True)