-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAntispam.java
More file actions
294 lines (203 loc) · 7.8 KB
/
Antispam.java
File metadata and controls
294 lines (203 loc) · 7.8 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package antispam;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Dictionary;
import java.util.Scanner;
public class Antispam {
public static int TAILLE_DICTIONNAIRE;
public String[] dictionnaire;
public Antispam(){
}
public String[] charger_dictionnaire(String dico) throws IOException{
BufferedReader br = null;
FileReader fn;
FileReader fn2;
String res[] = null;
int nbLines = 0;
fn = new FileReader(dico);
fn2 = new FileReader(dico);
String ligne2;
BufferedReader reader = new BufferedReader(fn2);
while ((ligne2 = reader.readLine()) != null)
if (ligne2.length() > 2) {
nbLines++;
}
reader.close();
fn2.close();
TAILLE_DICTIONNAIRE = nbLines;
res = new String[TAILLE_DICTIONNAIRE];
br = new BufferedReader(fn);
System.out.println("Lecture dictionnaire Ok");
String ligne;
int cmpt = 0;
while((ligne = br.readLine()) != null){
if (ligne.length() > 2) {
res[cmpt] = ligne;
cmpt++;
}
}
br.close();
System.out.println("Remplissage tableau dictionnaire Ok");
return res;
}
public int[] lire_message(String mail){
int[] X = new int[TAILLE_DICTIONNAIRE];
BufferedReader br = null;
String ligne;
try {
br = new BufferedReader(new FileReader(mail));
//System.out.println("Lecture mail Ok");
} catch (FileNotFoundException e) {
System.out.println("ERREUR OUVERTURE MAIL");
e.printStackTrace();
}
try {
int cmpt = 0;
while ((ligne = br.readLine()) != null) {
ligne = nettoyer_ligne(ligne);
// System.out.println(ligne);
String[] mots = ligne.split(" ");
for (int i = 0; i < mots.length; i++) {
// System.out.println("mon mot:" + mots[i] + ":"+i);
int tmp = isInDictionnary(mots[i]);
if (tmp != -1) {
// System.out.println("Mot dans le dico à l'indice "+tmp);
X[tmp] = 1;
}
}
cmpt++;
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return X;
}
private String nettoyer_ligne(String lignemail) {
for (int j = 0; j < lignemail.length(); j++) {
if(toASCII(lignemail.charAt(j)) > 32 && toASCII(lignemail.charAt(j)) < 65){
StringBuffer buffer = new StringBuffer(lignemail);
buffer.setCharAt(j, ' ');
lignemail = buffer.toString();
}
if(toASCII(lignemail.charAt(j)) > 90 && toASCII(lignemail.charAt(j)) < 97){
StringBuffer buffer = new StringBuffer(lignemail);
buffer.setCharAt(j, ' ');
lignemail = buffer.toString();
}
if(toASCII(lignemail.charAt(j)) > 122){
StringBuffer buffer = new StringBuffer(lignemail);
buffer.setCharAt(j, ' ');
lignemail = buffer.toString();
}
}
return lignemail;
}
public int isInDictionnary(String mot){
for (int i = 0; i < TAILLE_DICTIONNAIRE; i++) {
if(dictionnaire[i].equalsIgnoreCase(mot)){
//System.out.println("je compare :"+mot+": avec :"+dictionnaire[i]+": à la pos "+i);
return i;
}
}
return -1;
}
public void afficheTab(int[] tab){
for (int i = 0; i < tab.length; i++) {
System.out.println("indice : "+i+" valeur : "+tab[i]);
}
}
public static int toASCII(char lettre)
{
return (int)lettre;
}
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
public static void main(String[] args){
int nbSpamTest = Integer.parseInt(args[1]);
int nbHamTest = Integer.parseInt(args[2]);
String dossierbasetest = args[0];
Scanner sc = new Scanner(System.in);
System.out.println("Combien de Spam dans la base d'apprentissage ? ");
String str = sc.nextLine();
System.out.println("Combien de Ham dans la base d'apprentissage ? ");
String str2 = sc.nextLine();
int nbSpamApp = Integer.parseInt(str);
int nbHamApp = Integer.parseInt(str2);
Antispam as = new Antispam();
Classifieur c = new Classifieur(as);
try {
as.dictionnaire = as.charger_dictionnaire("dictionnaire1000en.txt");
System.out.println(" Apprentissage . . .");
int Epsilon = 1;
double[] bjSpam = c.getBJSpam(as, nbSpamApp, Epsilon);
// System.out.println("TATA");
/*for (int i = 0; i < bjSpam.length; i++) {
System.out.println(" Spam :"+bjSpam[i]);
}*/
double[] bjHam = c.getBJHam(as, nbHamApp, Epsilon);
/* for (int i = 0; i < bjHam.length; i++) {
System.out.println(" Ham :"+bjHam[i]);
}*/
int nbExemple = nbSpamTest+nbHamTest;
double PspamApriori = (double)((double)nbSpamTest/(double)nbExemple);
double PhamApriori = (double)((double)nbHamTest/(double)nbExemple);
int sommeindicatricespam = 0;
String prediction;
String prediction2;
System.out.println("Test :");
for (int i = 0; i < nbSpamTest; i++) {
double probabspam = c.Probabilite(dossierbasetest+"/spam/"+i+".txt",bjSpam);
double probabham = c.Probabilite(dossierbasetest+"/spam/"+i+".txt",bjHam);
double Pxx = (double)((double)((double)Math.exp(probabspam)*(double)Math.exp(PspamApriori))+(double)((double)Math.exp(probabham)*(double)Math.exp(PhamApriori)));
Pxx = (double)((double)1/(double)Pxx);
Pxx = (double)Math.log(Pxx);
double probaPosterioriSpam =(double)((double)((double)probabspam+Math.log((double)PspamApriori))+(double)Pxx);
double probaPosterioriHam =(double)((double)((double)probabham+Math.log((double)PhamApriori))+(double)Pxx);
if(probaPosterioriHam> probaPosterioriSpam){
sommeindicatricespam++;
prediction = "HAM *** erreur ***";
}else{
prediction = "SPAM";
}
System.out.println("le SPAM numero "+i+" : P(Y=Spam| X=x) = "+Math.exp(probaPosterioriSpam)+", P(Y=Ham|X=x) = "+Math.exp(probaPosterioriHam)+"");
System.out.println(" == > identifie comme un "+prediction);
}
int sommeindicatriceham = 0;
for (int i = 0; i < nbHamTest; i++) {
double probabspam = c.Probabilite(dossierbasetest+"/ham/"+i+".txt",bjSpam);
double probabham = c.Probabilite(dossierbasetest+"/ham/"+i+".txt",bjHam);
double Pxx = (double)((double)((double)Math.exp(probabspam)*(double)Math.exp(PspamApriori))+(double)((double)Math.exp(probabham)*(double)Math.exp(PhamApriori)));
Pxx = (double)((double)1/(double)Pxx);
Pxx = (double)Math.log(Pxx);
double probaPosterioriSpam =(double)((double)((double)probabspam+Math.log((double)PspamApriori))+(double)Pxx);
double probaPosterioriHam =(double)((double)((double)probabham+Math.log((double)PhamApriori))+(double)Pxx);
if(probaPosterioriSpam> probaPosterioriHam){
sommeindicatriceham++;
prediction2 = "SPAM *** erreur ***";
}else{
prediction2 = "HAM";
}
System.out.println("le HAM numero "+i+" : P(Y=Spam| X=x) = "+Math.exp(probaPosterioriSpam)+", P(Y=Ham|X=x) = "+Math.exp(probaPosterioriHam)+"");
System.out.println(" == > identifie comme un "+prediction2);
}
double Remp = (double)((double)sommeindicatricespam*100/(double)nbSpamTest);
double Remp2 = (double)((double)sommeindicatriceham*100/(double)nbHamTest);
System.out.println("Taux d'erreur sur les "+nbSpamTest+" SPAM :"+round(Remp,1)+"%");
System.out.println("Taux d'erreur sur les "+nbHamTest+" HAM :"+round(Remp2,1)+"%");
double Remptotal = (double)((double)((double)sommeindicatriceham+(double)sommeindicatricespam)*100/(double)nbExemple);
System.out.println("Taux d'erreur global sur "+nbExemple+" mails :"+round(Remptotal,1)+"%");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}