-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopWords.java
More file actions
88 lines (74 loc) · 2.17 KB
/
TopWords.java
File metadata and controls
88 lines (74 loc) · 2.17 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
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.HashMap;
import java.util.Map.Entry;
// Retreival class to extract the top 10 Spam and Ham words from the HashMap
// Jamie Henson
public class TopWords {
private static HashMap<String,double[]> hash = new HashMap<String,double[]>();
@SuppressWarnings("unchecked")
private void readHash()
{
try
{
File file = new File("hash_all.jh0422");
FileInputStream f = new FileInputStream(file);
ObjectInputStream s = new ObjectInputStream(f);
hash = (HashMap<String,double[]>)s.readObject();
s.close();
} catch (Exception e)
{
System.err.println("Reading failed.");
}
}
private void printArray(String[][] leaders)
{
for (int i = 0; i<10; i++)
{
System.out.println(i+1 + ") " + leaders[i][0]);
}
}
public static void main (String args[])
{
TopWords top = new TopWords();
top.readHash();
String[][] leaders = new String[10][2];
String[][] leadersH = new String[10][2];
double max = 0, spam = 0, ham = 0;
String maxWord = null;
hash.remove("#@/%PROB");
HashMap<String,double[]> hash2 = hash;
for (int i = 0; i<10; i++)
{
for(Entry<String, double[]> entry : hash.entrySet()) {
double[] pair = entry.getValue();
spam = pair[0];
if (max == 0 || spam > max) {
max = spam;
maxWord = entry.getKey();
}
}
leaders[i][0] = maxWord;
leaders[i][1] = Double.toString(Math.log(spam));
hash.remove(maxWord);
max = 0;
for(Entry<String, double[]> entry : hash2.entrySet()) {
double[] pair = entry.getValue();
ham = pair[1];
if (max == 0 || ham > max) {
max = ham;
maxWord = entry.getKey();
}
}
leadersH[i][0] = maxWord;
leadersH[i][1] = Double.toString(Math.log(spam));
hash2.remove(maxWord);
max = 0;
}
System.out.println("Top 10 Spam words!\n--------------------------------");
top.printArray(leaders);
System.out.println("\nTop 10 Ham words!\n--------------------------------");
top.printArray(leadersH);
}
}