blob: 7e0e74355323256df0ea95130d641360db1f2ab9 (
plain)
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
|
package Chapter4.classification.bayes;
import java.io.FileReader;
import java.io.IOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonStreamParser;
public class TestNBC {
public static void main(String[] args){
String filename = args.length >= 1 ? args[0] : "owsemoticons.json";
//initialize the sentiment classifier
NaiveBayesSentimentClassifier nbsc = new NaiveBayesSentimentClassifier();
try {
//read the file, and train each document
JsonStreamParser parser = new JsonStreamParser(new FileReader(filename));
JsonObject elem;
String text;
while (parser.hasNext()) {
elem = parser.next().getAsJsonObject();
text = elem.get("text").getAsString();
nbsc.trainInstance(text);
}
//print out the positive and negative dictionary
System.out.println("=== Positive Dictionary ===");
System.out.println(nbsc.printWordOccurs(0, 25));
System.out.println("=== Negative Dictionary ===");
System.out.println(nbsc.printWordOccurs(1, 25));
//now go through and classify each line as positive or negative
// parser = new JsonStreamParser(new FileReader(filename));
// while (parser.hasNext()) {
// elem = parser.next().getAsJsonObject();
// text = elem.get("text").getAsString();
// Classification c = nbsc.classify(text);
// System.out.println(c + " -> " + text);
// }
System.out.println(nbsc.classify("I love new york"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
|