-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentiment_analysis.py
More file actions
105 lines (87 loc) · 3.34 KB
/
Copy pathSentiment_analysis.py
File metadata and controls
105 lines (87 loc) · 3.34 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
"""
Scrip file for doing sentiment analysis.
A Recurrent Neural Network model is loaded from saved model file 'models' folder.
Model is trained with 1.6M tweets.
MaxLen of sequence for analysis is 24.
predict function predicts the sentiment of a single line text.
Summary of accuracies of model:
Accuracy of NeuralNet on validation set is 81.62
Accuracy of NeuralNet on test set is 80.92
"""
# Import libraries
import os # Some OS operations
from time import perf_counter as timer
import pandas as pd # Manipulating data
# Keras libraries for Neural Networks
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.models import load_model
import h5py
import pickle
from utilities import *
# ===============================
# Load tokenizer and model
t_start = timer() # Clock start
MaxLen = 24 # Do not change because model already trained with these parameters
# Load tokenizer
with open('models/tokenizer_10k.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
# Load Keras model
model_name = "rnn_glove_model"
model_name = "rnn_emb_split01_model"
model_name = "rnn_emb_model"
# model_name = "nn_w2v_model"
# model_name = "emb_model"
model = load_model("models/" + model_name + ".h5")
print(model.summary())
# ===================================
# Function for predicting sentiment of sentence
def predict(input_text, thresh=0.5, max_length=24):
text = text_clean(input_text)
test_seq = tokenizer.texts_to_sequences(text)
test = pad_sequences(test_seq, maxlen=max_length)
s = model.predict(test)
if s > thresh:
print("Positive")
else:
print("Negative")
return s
# ===============================
# Test some cases
print(predict("You have written a fantastic review about movie but things have changed."))
print(predict("I have to say your review is not good."))
print(predict("I have to say your review is nt good."))
print(predict("I have to say your review is not bad."))
# In case, interested in model summary
# print(rnn_emb_model.summary())
# ===================================
# Output in terminal
# Positive
# [[0.8607207]]
# Negative
# [[0.1286704]]
# Negative
# [[0.36957595]]
# Positive
# [[0.75851214]]
# Model: "sequential_1"
# _________________________________________________________________
# Layer (type) Output Shape Param #
# =================================================================
# embedding_1 (Embedding) (None, 24, 50) 500000
# _________________________________________________________________
# bidirectional_1 (Bidirection (None, 24, 128) 58880
# _________________________________________________________________
# bidirectional_2 (Bidirection (None, 64) 41216
# _________________________________________________________________
# dense_1 (Dense) (None, 64) 4160
# _________________________________________________________________
# dropout_1 (Dropout) (None, 64) 0
# _________________________________________________________________
# dense_2 (Dense) (None, 1) 65
# =================================================================
# Total params: 604,321
# Trainable params: 604,321
# Non-trainable params: 0
# _________________________________________________________________
# None