-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecisionTreeClassifier.java
195 lines (156 loc) · 5.75 KB
/
DecisionTreeClassifier.java
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
import java.io.*;
import java.util.*;
class DecisionTreeClassifier{
public static void main(String[] args){
Hashtable<String, Word> words = new Hashtable<String, Word>();
String trainingFile = "input.txt";
String testingFile = "testing.txt";
if(args.length >= 0)
trainingFile = args[0]; //file which contains training data
if(args.length >= 1)
testingFile = args[1]; // file which contains testing data
String line = null; //contains one line at a time
int positiveReviewCount = 0;
int negativeReviewCount = 0;
final long startTraining = System.currentTimeMillis();
/* CREATE HASHTABLE OF ALL WORDS */
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(trainingFile);
// wraps FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
//handles each review one by one
while((line = bufferedReader.readLine()) != null) {
int reviewType = Integer.parseInt("" + line.charAt(0)); //contains sentiment of review
String review = line.substring(2); //contains text of the review
review = review.toLowerCase();
review = review.replace("<br />", " <br/> ");
review = review.replaceAll("[,.;!?*/:\"()\\]\\[<>&]", "");
HashSet<String> uniqueWords = new HashSet<String>(); //used to check if word has already been added
for(String newWord: review.split(" ")){
if(uniqueWords.add(newWord)){ //word has not been added this review
if(words.containsKey(newWord)){ //word has already been added
if(reviewType == 1){
words.get(newWord).positiveCount ++;
}else{
words.get(newWord).negativeCount ++;
}
}else{ // word has not yet been added to hashtable
//creates new word object and adds it to words hashtable
Word tmpWord = new Word(reviewType);
words.put(newWord, tmpWord);
}
}
}
if (reviewType == 0){
negativeReviewCount++;
}else{
positiveReviewCount++;
}
}
bufferedReader.close();
}
catch(FileNotFoundException ex){
System.out.println(
"Unable to open file '" +
trainingFile + "'");
}
catch(IOException ex){
System.out.println(
"Error reading file '"
+ trainingFile + "'");
}
/*Populate hashtable with probability values*/
Set<String> keys = words.keySet();
for(String key: keys){
words.get(key).positiveProb = (double) words.get(key).positiveCount/ positiveReviewCount;
words.get(key).negativeProb = (double) words.get(key).negativeCount/ negativeReviewCount;
}
final long endTraining = System.currentTimeMillis();
/*Classify the testing set*/
double trainingResults = classifyData(trainingFile, words, false);
double testingResults = classifyData(testingFile, words, true);
final long endTesting = System.currentTimeMillis();
int trainingTime = (int)((endTraining - startTraining)/ 1000);
int testingTime = (int)((endTesting - endTraining) / 1000);
System.out.println("" + trainingTime + " seconds (training)");
System.out.println("" + testingTime + " seconds (labeling)");
System.out.println("" + trainingResults + " (training)");
System.out.println("" + testingResults + " (testing)");
}
public static double classifyData(String file, Hashtable<String, Word> words, boolean outputEachClassification){
int correct = 0;
int incorrect = 0;
String line = null;
double percentageCorrect = 0.0;
try {
FileReader fileReader2 =
new FileReader(file);
BufferedReader bufferedReader2 =
new BufferedReader(fileReader2);
//classifies each review one by one
while((line = bufferedReader2.readLine()) != null) {
int reviewType = Integer.parseInt("" + line.charAt(0)); //contains sentiment of review
String review = line.substring(2); //contains text of the review
review = review.toLowerCase();
review = review.replace("<br />", " <br/> ");
review = review.replaceAll("[,.;!?*/:\"()\\]\\[<>&]", "");
double positiveProbability = 0.0;
double negativeProbability = 0.0;
int myClassification = 0;
HashSet<String> uniqueWords = new HashSet<String>(); //used to check if word has already been added
for(String currentWord: review.split(" ")){
int wordOccurances = 0;
if(words.containsKey(currentWord)){
wordOccurances = words.get(currentWord).positiveCount + words.get(currentWord).negativeCount;
}
if(wordOccurances > 12){
if(uniqueWords.add(currentWord)){ //word has not already been calculated for this review
if(words.containsKey(currentWord)){
Word current = words.get(currentWord);
if(current.positiveProb != 0){
positiveProbability += (double)Math.log(current.positiveProb);
}else{
positiveProbability += -7.5;
}
if(current.negativeProb != 0){
negativeProbability += (double)Math.log(current.negativeProb);
}else{
negativeProbability += -7.5;
}
}
}
}
}
if(positiveProbability > negativeProbability){
myClassification = 1;
}else{
myClassification = 0;
}
if(outputEachClassification == true){
System.out.println("" + myClassification);
}
if(myClassification == reviewType){
correct++;
}else{
incorrect++;
}
}
bufferedReader2.close();
percentageCorrect = ((double)(correct) / (double)(incorrect + correct));
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
file + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ file + "'");
}
return percentageCorrect;
}
}