diff options
Diffstat (limited to 'src/Chapter4/classification/bayes/TestNBC.java')
-rw-r--r-- | src/Chapter4/classification/bayes/TestNBC.java | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/Chapter4/classification/bayes/TestNBC.java b/src/Chapter4/classification/bayes/TestNBC.java new file mode 100644 index 0000000..7e0e743 --- /dev/null +++ b/src/Chapter4/classification/bayes/TestNBC.java @@ -0,0 +1,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(); + } + + } +} |