Repository navigation
Expand file tree
/
Copy pathText_Classifier_Bot.py
More file actions
213 lines (164 loc) · 5.35 KB
/
Copy pathText_Classifier_Bot.py
File metadata and controls
213 lines (164 loc) · 5.35 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
# # Libraries to use
# Data extraction
import io
import os
import requests
import numpy as np
# Display function
from matplotlib import pyplot as plt
# For machine learning
# Using pip for instaling => !pip install tensorflow==2.8
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras import layers
import numpy
# For image
from PIL import Image
import cv2
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import models
import matplotlib.pyplot as plt
# # Variables and data
# Read data from a TXT file
with open('link_txt', 'r') as file:
lines = file.readlines()
# Split lines in text and labels
sentences = []
labelss = []
for line in lines:
parts = line.strip().split()
text = ' '.join(parts[1:])
label = str(parts[0])
sentences.append(text)
labelss.append(label)
# # Tokenization and text sequencing
max_words = 1000 # Maximum number of words in the vocabulary
tokenizer = Tokenizer(num_words=max_words)
tokenizer.fit_on_texts(sentences)
sequences = tokenizer.texts_to_sequences(sentences)
# # Padding sequences so they are the same length
max_sequence_length = max(len(seq) for seq in sequences)
sequences = pad_sequences(sequences, maxlen=max_sequence_length)
# New arrangement
labels = []
# Go to binary
for i in labelss:
if i == 'ham':
labels.append(0)
else:
labels.append(1)
# Transform arrays in numpy
sentences = np.array(sentences)
labels = np.array(labels)
# # Checking the variables
# print(sentences)
# print(len(labels))
# # Model creation function
def design_model(training_data):
print("\nBuilding model...")
# Create the text classification model
model = tf.keras.Sequential(name='LSTM_spam_AI')
model.add(Embedding(max_words, 64, input_length=max_sequence_length))
model.add(Bidirectional(LSTM(64)))
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Summarize model
model.summary()
return model
# Use model function
model = design_model(sentences)
# Train the model
history = model.fit(sequences, labels, epochs=6, validation_split=0.2)
# # Plot using the Matplotlib library
# Graph training metrics
acc = history.history['accuracy'] # Accuracy
val_acc = history.history['val_accuracy'] # Accuracy validation
loss = history.history['loss'] # Loss from accuracy
val_loss = history.history['val_loss'] # Loss from accuracy validation
epochs = range(1, len(acc) + 1) # Epochs for data
plt.figure(figsize=(12, 4)) # Graph size
plt.subplot(1, 2, 1) # The accuracy graph
plt.plot(epochs, acc, color='red', label='Accuracy') # Taking the data
plt.plot(epochs, val_acc, label='Validation accuracy') # Taking the data for validation
plt.title('Accuracy of training and validation') # Title
plt.xlabel('Epochs') # X-label Epochs
plt.ylabel('Accuracy') # Y-label Accuracy
plt.legend() # Legend for data
plt.subplot(1, 2, 2) # The loss graph
plt.plot(epochs, loss, color='red', label='Loss graph') # Taking the data
plt.plot(epochs, val_loss, label='Loss of validation') # Taking the data for validation
plt.title('Loss of training and validation') # Title
plt.xlabel('Epochs') # X-label Epochs
plt.ylabel('Accuracy') # Y-label Accuracy
plt.legend() # Legend for data
plt.show() # Show graph
# # Prediction function
# Read data from a TXT file
with open('link_2', 'r') as file:
lines = file.readlines()
# Split lines in text and labels
sentences_val = []
labelss_val = []
for line in lines:
parts = line.strip().split()
text = ' '.join(parts[1:])
label = str(parts[0])
sentences_val.append(text)
labelss_val.append(label)
# Re-definition
'''
sentences_val = sentences_val[0:100]
labelss_val = labelss_val[0:100]
'''
# Tokenization and text sequencing
max_words = 1000 # Maximum number of words in the vocabulary
tokenizer = Tokenizer(num_words=max_words)
tokenizer.fit_on_texts(sentences_val)
sequences = tokenizer.texts_to_sequences(sentences_val)
'''
# Padding sequences so they are the same length
max_sequence_length = max(len(seq) for seq in sentences_val)
sentences_val = pad_sequences(sequences, maxlen=max_sequence_length)
'''
# New arrangement validation
labels_val = []
# Go to binary
for i in labelss_val:
if i == 'ham':
labels_val.append(0)
else:
labels_val.append(1)
# Transform arrays in numpy
sentences_val = np.array(sentences_val)
labels_val = np.array(labels_val)
# # Checking the variables
# print(len(sentences_val))
# print(len(labels_val))
# # Classify new text
labels_val
# Predict results
new_sequence = tokenizer.texts_to_sequences(sentences_val)
new_sequence = pad_sequences(new_sequence, maxlen=171)
prediction = model.predict(new_sequence)
predictionBin = []
# Examine probability
for i in prediction:
if i < 0.5:
predictionBin.append(0)
else:
predictionBin.append(1)
# print("Prediction:", predictionBin)
# Examine validation
resultFin = []
for i in range(len(labels_val)):
if labels_val[0] == predictionBin[0]:
resultFin.append(labels_val[0])
# Final score
print('The probability for a good clasification is:', round(len(resultFin)/len(labels_val)*100,2))