-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.java
More file actions
147 lines (127 loc) · 3.74 KB
/
filter.java
File metadata and controls
147 lines (127 loc) · 3.74 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
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
// Spam Filter using a naive Bayesian classifier and Laplace smoothing
// Jamie Henson
public class filter
{
private HashMap<String, double[]> trainingHash = new HashMap<String,double[]>();
private String[] testingMail;
private String testingFileName;
private static int rightCount = 0;
@SuppressWarnings("unchecked")
private void readHashAll()
{
try
{
File file = new File("hash_all.jh0422");
FileInputStream f = new FileInputStream(file);
ObjectInputStream s = new ObjectInputStream(f);
trainingHash = (HashMap<String,double[]>)s.readObject();
s.close();
} catch (Exception e)
{
System.err.println("Reading failed.");
}
}
private void readTesting(String testingDir)
{
try
{
// Read in the testing file, split the contents down into
// individual words, and store in an array
String testingMailStr = "";
if (testingDir.endsWith(".txt"))
{
Scanner scanner = new Scanner(new File (testingDir));
while (scanner.hasNextLine())
{
String line = scanner.nextLine();
testingMailStr = testingMailStr + "\n" + line;
}
scanner.close();
}
testingFileName = testingDir.substring(11,testingDir.length());
testingMail = testingMailStr.split(" ");
}
catch (Exception e)
{
System.err.println(e);
}
}
private double[] checkWord(String word)
{
double[] results = new double[2];
word = word.replaceAll("(\\r|\\n)", "");
// Get number of occurrences of each word in known spam and ham emails
if (trainingHash.containsKey(word)){
results = trainingHash.get(word);
}
return results;
}
private void classify()
{
ArrayList<Double> wordProbsS = new ArrayList<Double>();
ArrayList<Double> wordProbsH = new ArrayList<Double>();
double spamProb = 0, hamProb = 0;
double[] shares = trainingHash.get("#@/%PROB");
// Check each word's probability, and store both ham and spam probabilities
// in an array
for (String word : testingMail)
{
double[] checkResults = checkWord(word);
wordProbsS.add(checkResults[0]);
wordProbsH.add(checkResults[1]);
}
// Change no-showers to 1, so that it has no bearing on the calculation and
// and keep a running product of the probability for the message
for (Double prob : wordProbsS)
{
if (prob == 0) prob = 1.0;
spamProb += Math.log(prob);
}
for (Double prob : wordProbsH)
{
if (prob == 0) prob = 1.0;
hamProb += Math.log(prob);
}
// Determine how many of all emails are spam and ham
double spamFrac = (double) shares[0] / (double) (shares[0] + shares[1]);
double hamFrac = (double) shares[1] / (double) (shares[0] + shares[1]);
spamProb += Math.log(spamFrac);
hamProb += Math.log(hamFrac);
// Compare probabilities. The higher one wins.
String result = (spamProb > hamProb) ? "spam" : "ham";
boolean match = (testingFileName.startsWith(result));
System.out.println(result + ", " + (filter250.rightCount + 1)+ ", " + match + ", " + testingFileName);
if (match)
{
rightCount++;
filter250.rightCount++;
}
return;
}
public static void main(String args[])
{
filter f = new filter();
// Run Cross Validation
if (args[0].equals("-cv"))
{
filterCV.doCV();
return;
}
// Run the 250-test run checker
else if (args[0].equals("-250"))
{
filter250.do250();
return;
}
else f.readHashAll();
// Go ahead with the classification of a single e-mail
f.readTesting(args[0]);
f.classify();
}
}