This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clasificador_seleccionado.py
146 lines (105 loc) · 4.87 KB
/
clasificador_seleccionado.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
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 26 18:08:31 2022
@author: pablo
"""
from nltk.probability import FreqDist
from nltk.lm.vocabulary import Vocabulary
import nltk
from sklearn import preprocessing
from sklearn.feature_extraction.text import CountVectorizer
from sklearn import naive_bayes
import re
import unicodedata
import pathlib
################################## Funciones ###########################################################
def es_mensaje_no_deseado(ruta):
# Lectura de los corpus
file_legitimo = open("./corpusLegitimo.txt", "r")
list_corpus_legitimo = eval(file_legitimo.read())
file_legitimo.close()
file_spam = open("./corpusSpam.txt", "r")
list_corpus_spam = eval(file_spam.read())
file_spam.close()
file_dataset = open("./dataset.txt", "r", encoding="utf-8")
dataset = eval(file_dataset.read())
file_dataset.close()
# Obtención del vocabulario
vocab = crear_vocab(list_corpus_legitimo, list_corpus_spam)
# Obtenemos y codificamos las clases de los correos de entrenamiento
codificador_clases = preprocessing.LabelEncoder()
clases_codificadas = codificar_objetivos(codificador_clases)
# Entrenamiento de los modelos
countvectorizer = CountVectorizer(vocabulary=vocab)
count_entrenamiento = countvectorizer.fit_transform(dataset)
k = 3 # Hiperarámetro de suavizado
multinomial_NB = naive_bayes.MultinomialNB(alpha=k)
multinomial_NB.fit(count_entrenamiento.toarray(), clases_codificadas)
# Leer el correo y normalizarlo
file_correo_prueba = open(ruta, "r")
correo_prueba = file_correo_prueba.read()
file_correo_prueba.close()
list_correo_prueba_tokenized = nltk.word_tokenize(correo_prueba)
list_correo_prueba_normalized = normalize(list_correo_prueba_tokenized)
correo_prueba_normalized = [" ".join(list_correo_prueba_normalized)]
# Predicción de la clase asignada al correo
count_prueba = countvectorizer.transform(correo_prueba_normalized)
prediccion_NB = multinomial_NB.predict(count_prueba.toarray())
prediccion_NB = codificador_clases.inverse_transform(prediccion_NB)
return prediccion_NB[0] == 'Spam'
def crear_vocab(list_corpus_legitimo, list_corpus_spam):
# Calculamos las 500 palabras más comunes de cada tipo de correo
fdist_legitimo = FreqDist(list_corpus_legitimo)
most_common_legitimo = fdist_legitimo.most_common(500)
fdist_spam = FreqDist(list_corpus_spam)
most_common_spam = fdist_spam.most_common(500)
# Creamos el vocabulario
vocabulary_terms_repeated = [word[0] for word in most_common_spam] + [word[0] for word in most_common_legitimo]
vocabulary_terms = [word[0] for word in most_common_spam] + [word[0] for word in most_common_legitimo]
# Eliminamos aquellas palabras que aparezcan tanto en los no deseados como
# en los legitimos, ya que no nos serviran para diferenciar los correos
for word in vocabulary_terms_repeated:
if vocabulary_terms.count(word) > 1:
vocabulary_terms.remove(word)
vocab = Vocabulary(vocabulary_terms)
return vocab
def codificar_objetivos(codificador_clases):
clases_entrenamiento = []
for path in pathlib.Path("./Enron-Spam/subconjuntos/train/legítimo").iterdir():
clases_entrenamiento.append('Legitimo')
for path in pathlib.Path("./Enron-Spam/subconjuntos/train/no_deseado").iterdir():
clases_entrenamiento.append('Spam')
clases_codificadas = codificador_clases.fit_transform(clases_entrenamiento)
return clases_codificadas
def remove_non_ascii(words):
"""Remove non-ASCII characters from list of tokenized words"""
new_words = []
for word in words:
new_word = unicodedata.normalize('NFKD', word).encode('ascii', 'ignore').decode('utf-8', 'ignore')
new_words.append(new_word)
return new_words
def to_lowercase(words):
"""Convert all characters to lowercase from list of tokenized words"""
new_words = []
for word in words:
new_word = word.lower()
new_words.append(new_word)
return new_words
def remove_punctuation(words):
"""Remove punctuation from list of tokenized words"""
new_words = []
for word in words:
new_word = re.sub(r'[^\w\s]', '', word)
if new_word != '':
new_words.append(new_word)
return new_words
def normalize(words):
words = remove_non_ascii(words)
words = to_lowercase(words)
words = remove_punctuation(words)
return words
################################## Filtro ###########################################################
ruta = "./Enron-Spam/subconjuntos/val/no_deseado/297" # Modificar este valor a la ruta del correo a clasificar
clasificado_como_spam = es_mensaje_no_deseado(ruta)
print("¿Ha sido el correo marcado como spam?")
print(clasificado_como_spam)