-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment_analysis.py
178 lines (141 loc) · 4.76 KB
/
sentiment_analysis.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
# https://github.com/alisoltanirad/sentiment-analysis
# Dependencies: numpy, pandas, nltk, sk-learn, keras, reason
import ssl
import numpy as np
import pandas as pd
import nltk
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from keras import Sequential
from keras.layers import Embedding, Dense, Flatten
from keras.preprocessing import sequence
from keras.datasets import imdb
from reason.stem import PorterStemmer
from reason.tokenize import WordTokenizer
from reason.metrics import accuracy
def main():
parameters = set_processing_parameters()
network_weights = train_imdb_network(parameters)
analyze_dataset(parameters, network_weights)
def set_processing_parameters():
parameters = {
'vocabulary_size': 5000,
'max_words': 500
}
return parameters
def train_imdb_network(parameters):
(x, y), (_, __) = load_imdb_dataset(parameters['vocabulary_size'])
x = sequence.pad_sequences(x, maxlen=parameters['max_words'])
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)
classifier = Sequential()
classifier.add(
Embedding(
input_dim=parameters['vocabulary_size'],
output_dim=64,
input_length=parameters['max_words']
)
)
classifier.add(Dense(units=64, activation='relu'))
classifier.add(Dense(units=32, activation='relu'))
classifier.add(Flatten())
classifier.add(Dense(units=1, activation='sigmoid'))
classifier.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
classifier.fit(
x=x_train,
y=y_train,
validation_data=(x_test, y_test),
batch_size=32,
epochs=5,
verbose=2
)
weights = [
classifier.layers[i].get_weights()
for i in range(1, (len(classifier.layers) - 2))
]
return weights
def load_imdb_dataset(vocabulary_size):
temp = np.load
np.load = lambda *a, **k: temp(*a, allow_pickle=True)
(x, y), (_, __) = imdb.load_data(path='imdb.npz', num_words=vocabulary_size)
np.load = temp
return (x, y), (_, __)
def analyze_dataset(parameters, weights):
dataset = pd.read_csv(
'https://raw.githubusercontent.com/alisoltanirad'
'/Sentiment-Analysis-Farsi-Dataset/master'
'/TranslatedDigikalaDataset.csv',
sep=','
)
y = dataset.iloc[:, 1].values
x = preprocess_text(dataset['Comment'], parameters)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
corpus = {
'x_train': x_train,
'y_train': y_train,
'x_test': x_test
}
y_prediction = classify_translated_data(parameters, weights, corpus)
evaluate_classifier(y_test, y_prediction)
def preprocess_text(corpus, parameters):
nltk.download('stopwords')
stopwords = set(nltk.corpus.stopwords.words('english'))
stemmer = PorterStemmer()
tokenizer = WordTokenizer()
x = []
for text in corpus:
tokenized_text = tokenizer.tokenize(text)
useful_words = [
stemmer.stem(word)[0]
for word in tokenized_text
if word not in stopwords
]
preprocessed_text = ' '.join(useful_words)
x.append(preprocessed_text)
cv = CountVectorizer(max_features=parameters['max_words'])
x = cv.fit_transform(x).toarray()
return x
def classify_translated_data(parameters, weights, corpus):
classifier = Sequential()
classifier.add(
Embedding(
input_dim=parameters['vocabulary_size'],
output_dim=64,
input_length=parameters['max_words']
)
)
classifier.add(Dense(units=64, activation='relu'))
classifier.add(Dense(units=32, activation='relu'))
for i in range(1, len(classifier.layers)):
classifier.layers[i].set_weights(weights[i-1])
classifier.layers[i].trainable = False
classifier.add(Dense(units=32, activation='relu'))
classifier.add(Flatten())
classifier.add(Dense(units=1, activation='sigmoid'))
classifier.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
classifier.fit(
x=corpus['x_train'],
y=corpus['y_train'],
batch_size=1,
epochs=5,
verbose=2
)
y_prediction = (classifier.predict(corpus['x_test']) > 0.5)
return y_prediction
def evaluate_classifier(y_true, y_prediction):
print('* Accuracy: {:.2%}'.format(accuracy(y_true, y_prediction)))
if __name__ == '__main__':
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
main()