-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.java
More file actions
283 lines (247 loc) · 8.6 KB
/
Index.java
File metadata and controls
283 lines (247 loc) · 8.6 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.*;
/**
* @author poorvitusam
* This is the class for multinomial Naive Bayes
* Classification model using laplace 1 smoothing
*
*/
public class Index {
/* Directory to maintain words */
public static LinkedHashMap<String, Integer> vocabDir = new LinkedHashMap<String, Integer>();
public static LinkedHashMap<String, Integer> hamVocabDir = new LinkedHashMap<String, Integer>();
public static LinkedHashMap<String, Integer> spamVocabDir = new LinkedHashMap<String, Integer>();
/* Directory for test data */
public static LinkedHashMap<String, Integer> testHamVocabDir = new LinkedHashMap<String, Integer>();
public static LinkedHashMap<String, Integer> testSpamVocabDir = new LinkedHashMap<String, Integer>();
/* List to maintain all conditional probablity */
public static LinkedHashMap<String, Double> hamConditionalProb = new LinkedHashMap<String, Double>();
public static LinkedHashMap<String, Double> spamConditionalProb = new LinkedHashMap<String, Double>();
/* List for storing the documents segregated as per the folder */
public static ArrayList<File> allDocs = new ArrayList<File>();
public static ArrayList<File> hamDocs = new ArrayList<File>();
public static ArrayList<File> spamDocs = new ArrayList<File>();
/* Doc list for test data */
public static ArrayList<File> test_hamDocs = new ArrayList<File>();
public static ArrayList<File> test_spamDocs = new ArrayList<File>();
/* Doc counts */
public static int n_ham_count = 0;
public static int n_spam_count = 0;
public static int n_count = 0;
public static int n_test_count = 0;
/* Priors */
public static double prior_ham = 0;
public static double prior_spam = 0;
public static int word_count_ham = 0;
public static int word_count_spam = 0;
public static int distinct_word_count = 0;
public static String TEXTFILE=".txt";
public static String HAM="ham";
public static String SPAM="spam";
/* Initialise accuracy */
public static int accurracy = 0;
public static void main(String args[]) {
try {
String mainDir = args[0];
String testDir = args[1];
readTrainingData(mainDir);
trainmMultinomialNB();
checkWithTest(testDir);
System.out.
println((double) accurracy * 100 / ((double) test_hamDocs.size() +
(double) test_spamDocs.size()));
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* Reading the Training data
*/
private static void readTrainingData(String mainDir) {
n_count = listf(mainDir, allDocs);
extractAllVocab(allDocs, vocabDir, true);
/* Run for all sub folders for train data */
File directory = new File(mainDir);
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.isDirectory()) {
if (file.getName().equals("ham")) {
n_ham_count = listf(file.getPath(), hamDocs);
word_count_ham = extractAllVocab(hamDocs, hamVocabDir, false);
} else {
n_spam_count = listf(file.getPath(), spamDocs);
word_count_spam = extractAllVocab(spamDocs, spamVocabDir, false);
}
}
}
}
private static void trainmMultinomialNB() {
prior_ham = (double) n_ham_count / (double) n_count;
calculateConditionalProbablity(vocabDir, hamConditionalProb, hamVocabDir, word_count_ham);
prior_spam = (double) n_spam_count / (double) n_count;
calculateConditionalProbablity(vocabDir, spamConditionalProb, spamVocabDir, word_count_spam);
}
private static void checkWithTest(String testDir) {
File test_directory = new File(testDir);
File[] test_fList = test_directory.listFiles();
for (File file : test_fList) {
if (file.isDirectory()) {
if (file.getName().equals(HAM)) {
listf(file.getPath(), test_hamDocs);
for (int i = 0; i < test_hamDocs.size(); i++) {
try {
FileReader test_file = new FileReader(test_hamDocs.get(i));
BufferedReader br = new BufferedReader(test_file);
getData(br, testHamVocabDir, false);
calculateAccuracy(testHamVocabDir, HAM);
} catch (Exception e) {
e.printStackTrace();
}
testHamVocabDir.clear();
}
} else {
listf(file.getPath(), test_spamDocs);
for (int i = 0; i < test_spamDocs.size(); i++) {
try {
FileReader test_file = new FileReader(test_spamDocs.get(i));
BufferedReader br = new BufferedReader(test_file);
getData(br, testSpamVocabDir, false);
calculateAccuracy(testSpamVocabDir, SPAM);
} catch (Exception e) {
e.printStackTrace();
}
testSpamVocabDir.clear();
}
}
}
} /*end runtest */
}
/*
* Helper function to extract all files from a directory returning the file count
*
*/
private static int listf(String directoryName, ArrayList<File> files) {
File directory = new File(directoryName);
int n = 0;
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.getName().contains(TEXTFILE)) {
n++;
files.add(file);
} else if (file.isDirectory()) {
n += listf(file.getAbsolutePath(), files);
}
}
return n;
}
/**
* Scans each line from all files in the given folder passed in parameter dir
* @param docs:Documents which are scanned
* @param dir: Store data extracted from documents in this hashmap
* @param calculateDistinct: Count for distinct words
* @return
*/
private static int extractAllVocab(ArrayList<File> docs, LinkedHashMap<String, Integer> dir,
boolean calculateDistinct) {
int count = 0;
for (int i = 0; i < docs.size(); i++) {
try {
FileReader file = new FileReader(docs.get(i));
BufferedReader br = new BufferedReader(file);
count += getData(br, dir, calculateDistinct);
} catch (Exception e) {
e.printStackTrace();
}
}
return count;
}
/**
* Extracts each line passed by extractAllVocab for words and stores them in the hashmap
* @param br:Line passed from extractAllVocab
* @param dir:Store in this hashmap
* @param calculateDistinct: Count for distinct words
* @return
*/
private static int getData(BufferedReader br, LinkedHashMap<String, Integer> dir, boolean calculateDistinct) {
String line;
int count = 0;
String regex = "[\\w']+";
try {
while ((line = br.readLine()) != null) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
count++;
if (dir.containsKey(matcher.group())) {
for (Entry<String, Integer> entry : dir.entrySet()) {
if (entry.getKey().equals(matcher.group())) {
entry.setValue(entry.getValue() + 1); /* If word is found increment */
break;
}
}
} else {
dir.put(matcher.group(), 1); /* If word not present then add the word in the directory */
if (calculateDistinct) {
distinct_word_count++;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
private static void calculateConditionalProbablity(LinkedHashMap<String, Integer> main,
LinkedHashMap<String, Double> conditionalProb, LinkedHashMap<String, Integer> dir, int word_count) {
for (Entry<String, Integer> entry : main.entrySet()) {
int val = 0;
if (dir.containsKey(entry.getKey())) {
for (Entry<String, Integer> e : dir.entrySet()) {
if (e.getKey().equals(entry.getKey())) {
val = e.getValue();
}
}
}
double prob = ((double) val + 1) / ((double) distinct_word_count + (double) word_count);
conditionalProb.put(entry.getKey(), prob);
}
}
private static void calculateAccuracy(LinkedHashMap<String, Integer> testVocabDir, String doc_class) {
double hamLikelihood = Math.log(prior_ham);
double spamLikelihood = Math.log(prior_spam);
for (Entry<String, Integer> entry : testVocabDir.entrySet()) {
hamLikelihood = hamLikelihood + getLikelihoodTerm(hamConditionalProb, entry.getKey(), entry.getValue());
spamLikelihood = spamLikelihood + getLikelihoodTerm(spamConditionalProb, entry.getKey(), entry.getValue());
}
String predict_class = "";
if (hamLikelihood > spamLikelihood) {
predict_class = HAM;
} else {
predict_class = SPAM;
}
if (predict_class == doc_class) {
accurracy++;
}
}
private static double getLikelihoodTerm(LinkedHashMap<String, Double> dir, String word_name, int word_count) {
double score = 0;
for (Entry<String, Double> entry : dir.entrySet()) {
if (entry.getKey().equals(word_name)) {
for (int i = 0; i < word_count; i++) {
score += Math.log(entry.getValue());
}
break;
}
}
return score;
}
}