1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.ai.classic;
21
22 import java.io.IOException;
23 import java.io.Reader;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.SortedSet;
32 import java.util.TreeSet;
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 public class BayesianAnalyzer {
88
89
90
91
92
93 private final static int MAX_INTERESTING_TOKENS = 15;
94
95
96
97
98
99 private final static double INTERESTINGNESS_THRESHOLD = 0.46;
100
101
102
103
104
105 private final static double DEFAULT_TOKEN_PROBABILITY = 0.4;
106
107
108 private Map<String, Integer> hamTokenCounts = new HashMap<String, Integer>();
109
110
111 private Map<String, Integer> spamTokenCounts = new HashMap<String, Integer>();
112
113
114 private int hamMessageCount = 0;
115
116
117 private int spamMessageCount = 0;
118
119
120 private Map<String, Double> corpus = new HashMap<String, Double>();
121
122
123
124
125
126
127
128
129
130
131 private class TokenProbabilityStrength implements Comparable<TokenProbabilityStrength> {
132
133
134
135 String token = null;
136
137
138
139
140 double strength = Math.abs(0.5 - DEFAULT_TOKEN_PROBABILITY);
141
142
143
144
145
146
147
148
149
150
151 public final int compareTo(TokenProbabilityStrength anotherTokenProbabilityStrength) {
152 int result = (int) ((((TokenProbabilityStrength) anotherTokenProbabilityStrength).strength - strength) * 1000000);
153 if (result == 0) {
154 return this.token.compareTo(((TokenProbabilityStrength) anotherTokenProbabilityStrength).token);
155 } else {
156 return result;
157 }
158 }
159
160
161
162
163
164
165 public String toString() {
166 StringBuffer sb = new StringBuffer(30);
167
168 sb.append(token).append("=").append(strength);
169
170 return sb.toString();
171 }
172 }
173
174
175
176
177 public BayesianAnalyzer() {
178 }
179
180
181
182
183
184
185
186 public void setHamTokenCounts(Map<String, Integer> hamTokenCounts) {
187 this.hamTokenCounts = hamTokenCounts;
188 }
189
190
191
192
193 public Map<String, Integer> getHamTokenCounts() {
194 return this.hamTokenCounts;
195 }
196
197
198
199
200
201
202
203 public void setSpamTokenCounts(Map<String, Integer> spamTokenCounts) {
204 this.spamTokenCounts = spamTokenCounts;
205 }
206
207
208
209
210 public Map<String, Integer> getSpamTokenCounts() {
211 return this.spamTokenCounts;
212 }
213
214
215
216
217
218
219
220 public void setSpamMessageCount(int spamMessageCount) {
221 this.spamMessageCount = spamMessageCount;
222 }
223
224
225
226
227 public int getSpamMessageCount() {
228 return this.spamMessageCount;
229 }
230
231
232
233
234
235
236
237 public void setHamMessageCount(int hamMessageCount) {
238 this.hamMessageCount = hamMessageCount;
239 }
240
241
242
243
244 public int getHamMessageCount() {
245 return this.hamMessageCount;
246 }
247
248
249
250
251 public void clear() {
252 corpus.clear();
253
254 tokenCountsClear();
255
256 hamMessageCount = 0;
257 spamMessageCount = 0;
258 }
259
260
261
262
263 public void tokenCountsClear() {
264 hamTokenCounts.clear();
265 spamTokenCounts.clear();
266 }
267
268
269
270
271
272
273
274 public void setCorpus(Map<String, Double> corpus) {
275 this.corpus = corpus;
276 }
277
278
279
280
281 public Map<String, Double> getCorpus() {
282 return this.corpus;
283 }
284
285
286
287
288 public void buildCorpus() {
289
290 Set<String> set = new HashSet<String>(hamTokenCounts.size() + spamTokenCounts.size());
291 set.addAll(hamTokenCounts.keySet());
292 set.addAll(spamTokenCounts.keySet());
293 Map<String, Double> tempCorpus = new HashMap<String, Double>(set.size());
294
295
296
297 Iterator<String> i = set.iterator();
298 while (i.hasNext()) {
299 String token = i.next();
300 tempCorpus.put(token, new Double(computeProbability(token)));
301 }
302 setCorpus(tempCorpus);
303 }
304
305
306
307
308
309
310
311
312
313 public void addHam(Reader stream) throws java.io.IOException {
314 addTokenOccurrences(stream, hamTokenCounts);
315 hamMessageCount++;
316 }
317
318
319
320
321
322
323
324
325
326 public void addSpam(Reader stream) throws java.io.IOException {
327 addTokenOccurrences(stream, spamTokenCounts);
328 spamMessageCount++;
329 }
330
331
332
333
334
335
336
337
338
339
340 public double computeSpamProbability(Reader stream) throws java.io.IOException {
341
342 Set<String> tokens = parse(stream);
343
344
345
346 Map<String, Double> workCorpus = getCorpus();
347
348
349
350 SortedSet<TokenProbabilityStrength> tokenProbabilityStrengths = getTokenProbabilityStrengths(tokens, workCorpus);
351
352
353
354 return computeOverallProbability(tokenProbabilityStrengths, workCorpus);
355 }
356
357
358
359
360
361
362
363
364 private void addTokenOccurrences(Reader stream, Map<String, Integer> target) throws java.io.IOException {
365 String token;
366 String header = "";
367
368
369 while ((token = nextToken(stream)) != null) {
370 boolean endingLine = false;
371 if (token.length() > 0 && token.charAt(token.length() - 1) == '\n') {
372 endingLine = true;
373 token = token.substring(0, token.length() - 1);
374 }
375
376 if (token.length() > 0 && header.length() + token.length() < 90 && !allDigits(token)) {
377 if (token.equals("From:") || token.equals("Return-Path:") || token.equals("Subject:") || token.equals("To:")) {
378 header = token;
379 if (!endingLine) {
380 continue;
381 }
382 }
383
384 token = header + token;
385
386 Integer value = null;
387
388 if (target.containsKey(token)) {
389 value = Integer.valueOf(((Integer) target.get(token)).intValue() + 1);
390 } else {
391 value = Integer.valueOf(1);
392 }
393
394 target.put(token, value);
395 }
396
397 if (endingLine) {
398 header = "";
399 }
400 }
401 }
402
403
404
405
406
407
408
409
410 private Set<String> parse(Reader stream) throws java.io.IOException {
411 Set<String> tokens = new HashSet<String>();
412 String token;
413 String header = "";
414
415
416 while ((token = nextToken(stream)) != null) {
417 boolean endingLine = false;
418 if (token.length() > 0 && token.charAt(token.length() - 1) == '\n') {
419 endingLine = true;
420 token = token.substring(0, token.length() - 1);
421 }
422
423 if (token.length() > 0 && header.length() + token.length() < 90 && !allDigits(token)) {
424 if (token.equals("From:") || token.equals("Return-Path:") || token.equals("Subject:") || token.equals("To:")) {
425 header = token;
426 if (!endingLine) {
427 continue;
428 }
429 }
430
431 token = header + token;
432
433 tokens.add(token);
434 }
435
436 if (endingLine) {
437 header = "";
438 }
439 }
440
441
442 return tokens;
443 }
444
445 private String nextToken(Reader reader) throws java.io.IOException {
446 StringBuffer token = new StringBuffer();
447 int i;
448 char ch, ch2;
449 boolean previousWasDigit = false;
450 boolean tokenCharFound = false;
451
452 if (!reader.ready()) {
453 return null;
454 }
455
456 while ((i = reader.read()) != -1) {
457
458 ch = (char) i;
459
460 if (ch == ':') {
461 String tokenString = token.toString() + ':';
462 if (tokenString.equals("From:") || tokenString.equals("Return-Path:") || tokenString.equals("Subject:") || tokenString.equals("To:")) {
463 return tokenString;
464 }
465 }
466
467 if (Character.isLetter(ch) || ch == '-' || ch == '$' || ch == '\u20AC'
468
469
470 || ch == '!' || ch == '\'') {
471 tokenCharFound = true;
472 previousWasDigit = false;
473 token.append(ch);
474 } else if (Character.isDigit(ch)) {
475 tokenCharFound = true;
476 previousWasDigit = true;
477 token.append(ch);
478 } else if (previousWasDigit && (ch == '.' || ch == ',')) {
479 reader.mark(1);
480 previousWasDigit = false;
481 i = reader.read();
482 if (i == -1) {
483 break;
484 }
485 ch2 = (char) i;
486 if (Character.isDigit(ch2)) {
487 tokenCharFound = true;
488 previousWasDigit = true;
489 token.append(ch);
490 token.append(ch2);
491 } else {
492 reader.reset();
493 break;
494 }
495 } else if (ch == '\r') {
496
497 } else if (ch == '\n') {
498
499 tokenCharFound = true;
500 previousWasDigit = false;
501 token.append(ch);
502 break;
503 } else if (tokenCharFound) {
504 break;
505 }
506 }
507
508 if (tokenCharFound) {
509
510 return token.toString();
511 } else {
512 return null;
513 }
514 }
515
516
517
518
519
520
521
522 private double computeProbability(String token) {
523 double hamFactor = 0;
524 double spamFactor = 0;
525
526 boolean foundInHam = false;
527 boolean foundInSpam = false;
528
529 double minThreshold = 0.01;
530 double maxThreshold = 0.99;
531
532 if (hamTokenCounts.containsKey(token)) {
533 foundInHam = true;
534 }
535
536 if (spamTokenCounts.containsKey(token)) {
537 foundInSpam = true;
538 }
539
540 if (foundInHam) {
541 hamFactor = 2 * ((Integer) hamTokenCounts.get(token)).doubleValue();
542 if (!foundInSpam) {
543 minThreshold = (hamFactor > 20) ? 0.0001 : 0.0002;
544 }
545 }
546
547 if (foundInSpam) {
548 spamFactor = ((Integer) spamTokenCounts.get(token)).doubleValue();
549 if (!foundInHam) {
550 maxThreshold = (spamFactor > 10) ? 0.9999 : 0.9998;
551 }
552 }
553
554 if ((hamFactor + spamFactor) < 5) {
555
556 return 0.4;
557 }
558
559 double spamFreq = Math.min(1.0, spamFactor / spamMessageCount);
560 double hamFreq = Math.min(1.0, hamFactor / hamMessageCount);
561
562 return Math.max(minThreshold, Math.min(maxThreshold, (spamFreq / (hamFreq + spamFreq))));
563 }
564
565
566
567
568
569
570
571
572
573
574 private SortedSet<TokenProbabilityStrength> getTokenProbabilityStrengths(Set<String> tokens, Map<String, Double> workCorpus) {
575
576 SortedSet<TokenProbabilityStrength> tokenProbabilityStrengths = new TreeSet<TokenProbabilityStrength>();
577
578 Iterator<String> i = tokens.iterator();
579 while (i.hasNext()) {
580 TokenProbabilityStrength tps = new TokenProbabilityStrength();
581
582 tps.token = (String) i.next();
583
584 if (workCorpus.containsKey(tps.token)) {
585 tps.strength = Math.abs(0.5 - ((Double) workCorpus.get(tps.token)).doubleValue());
586 } else {
587
588
589 Double corpusProbability = new Double(DEFAULT_TOKEN_PROBABILITY);
590 tps.strength = Math.abs(0.5 - DEFAULT_TOKEN_PROBABILITY);
591 boolean isTokenDegeneratedFound = false;
592
593 Collection<String> degeneratedTokens = buildDegenerated(tps.token);
594 Iterator<String> iDegenerated = degeneratedTokens.iterator();
595 String tokenDegenerated = null;
596 double strengthDegenerated;
597 while (iDegenerated.hasNext()) {
598 tokenDegenerated = (String) iDegenerated.next();
599 if (workCorpus.containsKey(tokenDegenerated)) {
600 Double probabilityTemp = (Double) workCorpus.get(tokenDegenerated);
601 strengthDegenerated = Math.abs(0.5 - probabilityTemp.doubleValue());
602 if (strengthDegenerated > tps.strength) {
603 isTokenDegeneratedFound = true;
604 tps.strength = strengthDegenerated;
605 corpusProbability = probabilityTemp;
606 }
607 }
608 }
609
610
611 if (isTokenDegeneratedFound) {
612 synchronized (workCorpus) {
613 workCorpus.put(tps.token, corpusProbability);
614 }
615 }
616 }
617
618 tokenProbabilityStrengths.add(tps);
619 }
620
621 return tokenProbabilityStrengths;
622 }
623
624 private Collection<String> buildDegenerated(String fullToken) {
625 ArrayList<String> tokens = new ArrayList<String>();
626 String header;
627 String token;
628 String tokenLower;
629
630
631 int headerEnd = fullToken.indexOf(':');
632 if (headerEnd >= 0) {
633 header = fullToken.substring(0, headerEnd);
634 token = fullToken.substring(headerEnd);
635 } else {
636 header = "";
637 token = fullToken;
638 }
639
640
641
642 tokenLower = token.toLowerCase();
643
644 int end = token.length();
645 do {
646 if (!token.substring(0, end).equals(tokenLower.substring(0, end))) {
647 tokens.add(header + tokenLower.substring(0, end));
648 if (header.length() > 0) {
649 tokens.add(tokenLower.substring(0, end));
650 }
651 }
652 if (end > 1 && token.charAt(0) >= 'A' && token.charAt(0) <= 'Z') {
653 tokens.add(header + token.charAt(0) + tokenLower.substring(1, end));
654 if (header.length() > 0) {
655 tokens.add(token.charAt(0) + tokenLower.substring(1, end));
656 }
657 }
658
659 if (token.charAt(end - 1) != '!') {
660 break;
661 }
662
663 end--;
664
665 tokens.add(header + token.substring(0, end));
666 if (header.length() > 0) {
667 tokens.add(token.substring(0, end));
668 }
669 } while (end > 0);
670
671 return tokens;
672 }
673
674
675
676
677
678
679
680
681
682 private double computeOverallProbability(SortedSet<TokenProbabilityStrength> tokenProbabilityStrengths, Map<String, Double> workCorpus) {
683 double p = 1.0;
684 double np = 1.0;
685 double tempStrength = 0.5;
686 int count = MAX_INTERESTING_TOKENS;
687 Iterator<TokenProbabilityStrength> iterator = tokenProbabilityStrengths.iterator();
688 while ((iterator.hasNext()) && (count-- > 0 || tempStrength >= INTERESTINGNESS_THRESHOLD)) {
689 TokenProbabilityStrength tps = iterator.next();
690 tempStrength = tps.strength;
691
692
693
694 double theDoubleValue = DEFAULT_TOKEN_PROBABILITY;
695
696 Double theDoubleObject = (Double) workCorpus.get(tps.token);
697
698
699 if (theDoubleObject != null) {
700 theDoubleValue = theDoubleObject.doubleValue();
701 }
702 p *= theDoubleValue;
703 np *= (1.0 - theDoubleValue);
704
705
706 }
707
708 return (p / (p + np));
709 }
710
711 private boolean allDigits(String s) {
712 for (int i = 0; i < s.length(); i++) {
713 if (!Character.isDigit(s.charAt(i))) {
714 return false;
715 }
716 }
717 return true;
718 }
719 }