-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolarityBasic.java
executable file
·99 lines (87 loc) · 3.52 KB
/
PolarityBasic.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
import com.aliasi.util.Files;
import com.aliasi.classify.Classification;
import com.aliasi.classify.Classified;
import com.aliasi.classify.DynamicLMClassifier;
import com.aliasi.lm.NGramProcessLM;
import java.io.File;
import java.io.IOException;
public class PolarityBasic {
File mPolarityDir;
String[] mCategories;
DynamicLMClassifier<NGramProcessLM> mClassifier;
PolarityBasic(String[] args) {
System.out.println("\nBASIC POLARITY2 DEMO");
mPolarityDir = new File("txt_sentoken");
System.out.println("\nData Directory=" + mPolarityDir);
mCategories = mPolarityDir.list();
int nGram = 8;
mClassifier
= DynamicLMClassifier
.createNGramProcess(mCategories,nGram);
}
void run() throws ClassNotFoundException, IOException {
train();
evaluate();
}
boolean isTrainingFile(File file) {
return file.getName().charAt(2) != '9'; // test on fold 9
}
void train() throws IOException {
int numTrainingCases = 0;
int numTrainingChars = 0;
System.out.println("\nTraining.");
for (int i = 0; i < mCategories.length; ++i) {
String category = mCategories[i];
Classification classification
= new Classification(category);
File file = new File(mPolarityDir,mCategories[i]);
File[] trainFiles = file.listFiles();
for (int j = 0; j < trainFiles.length; ++j) {
File trainFile = trainFiles[j];
if (isTrainingFile(trainFile)) {
++numTrainingCases;
String review = Files.readFromFile(trainFile,"ISO-8859-1");
numTrainingChars += review.length();
Classified<CharSequence> classified
= new Classified<CharSequence>(review,classification);
mClassifier.handle(classified);
}
}
}
System.out.println(" # Training Cases=" + numTrainingCases);
System.out.println(" # Training Chars=" + numTrainingChars);
}
void evaluate() throws IOException {
System.out.println("\nEvaluating.");
int numTests = 0;
int numCorrect = 0;
for (int i = 0; i < mCategories.length; ++i) {
String category = mCategories[i];
File file = new File(mPolarityDir,mCategories[i]);
File[] trainFiles = file.listFiles();
for (int j = 0; j < trainFiles.length; ++j) {
File trainFile = trainFiles[j];
if (!isTrainingFile(trainFile)) {
String review = Files.readFromFile(trainFile,"ISO-8859-1");
++numTests;
Classification classification
= mClassifier.classify(review);
if (classification.bestCategory().equals(category))
++numCorrect;
}
}
}
System.out.println(" # Test Cases=" + numTests);
System.out.println(" # Correct=" + numCorrect);
System.out.println(" % Correct="
+ ((double)numCorrect)/(double)numTests);
}
public static void main(String[] args) {
try {
new PolarityBasic(args).run();
} catch (Throwable t) {
System.out.println("Thrown: " + t);
t.printStackTrace(System.out);
}
}
}