-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
73 lines (57 loc) · 1.91 KB
/
test.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
import os, io
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
#FONCTIONS
def lectureContenuMailSansHeader(nom):
contenueMail = False
ligne = []
descriptionFichier = io.open(nom, 'r', encoding='latin1')
for line in descriptionFichier:
if contenueMail:
ligne.append(line)
elif line == '\n':
contenueMail = True
message = '\n'.join(ligne)
descriptionFichier.close()
return message
def lectureMailDossier(chemin, classification):
os.chdir(chemin)
fichiers = os.listdir(chemin)
for fichier in fichiers:
message = lectureContenuMailSansHeader(fichier)
X.append(message)
Y.append(classification)
#DATA
## dossier
dossierHam = r'C:\Users\Harrylepap\PycharmProjects\anti-spam-machine-learning\emails\ham'
dossierSpam = r'C:\Users\Harrylepap\PycharmProjects\anti-spam-machine-learning\emails\spam'
## type de mail
spam_type = "SPAM"
ham_type = "HAM | Message normale"
## initialisation
i=0
##les tableaux X et Y seront de la meme taille et ordonnes
### represente l'input Data (ici les mails)
X = []
### indique s'il s'agit d'un mail ou non
### etiquettes (labels) pour la matrice
Y = []
#TRAITEMENTS
lectureMailDossier(dossierHam, ham_type)
lectureMailDossier(dossierSpam, spam_type)
matrice = pd.DataFrame({'X': X, 'Y': Y})
vecteur = CountVectorizer()
counts = vecteur.fit_transform(matrice['X'].values)
classifier = MultinomialNB()
targets = matrice['Y'].values
classifier.fit(counts, targets)
TableauDePhrase = ['Free Viagra now!!!', "Bonjour Sandrine, comment ca va? je t'aime", "Ndao hilely fory be"]
compteurVecteur = vecteur.transform(TableauDePhrase)
predictions = classifier.predict(compteurVecteur)
nombres = len(TableauDePhrase)
nombres = int(nombres)
#SORTIE
for message in TableauDePhrase:
print(message, " => ", predictions[i])
i+=1