|
1 | 1 | # Java-Naive-Bayes-LevelDB |
2 | 2 | A Java Naive Bayes Classifier that works on LevelDB or other key-value store |
| 3 | + |
| 4 | + |
| 5 | +Example |
| 6 | +------------------ |
| 7 | + |
| 8 | +Here is an excerpt from the example (inspired by https://github.com/ptnplanet/Java-Naive-Bayes-Classifier/). The classifier will classify sentences (arrays of features) as sentences with either positive or negative sentiment. |
| 9 | + |
| 10 | +```java |
| 11 | + |
| 12 | + String[] cats = {POSITIVE, NEGATIVE}; |
| 13 | + // Create a new bayes classifier with string categories and string features. |
| 14 | + INaiveBayesClassifier bayes = (USE_LEVELDB ? new NaiveBayesClassifierLevelDBImpl("sentiment", cats, ".", 100) : new NaiveBayesClassifierTransientImpl("sentiment", cats)); |
| 15 | + |
| 16 | +// Two examples to learn from. |
| 17 | + String[] positiveText = "I love sunny days".split("\\s"); |
| 18 | + String[] negativeText = "I hate rain".split("\\s"); |
| 19 | + |
| 20 | +// Learn by classifying examples. |
| 21 | +// New categories can be added on the fly, when they are first used. |
| 22 | +// A classification consists of a category and a list of features |
| 23 | +// that resulted in the classification in that category. |
| 24 | + bayes.learn(POSITIVE, new HashSet(Arrays.asList(positiveText))); |
| 25 | + bayes.learn(NEGATIVE, new HashSet(Arrays.asList(negativeText))); |
| 26 | + |
| 27 | +// Here are two unknown sentences to classify. |
| 28 | + String[] unknownText1 = "today is a sunny day".split("\\s"); |
| 29 | + String[] unknownText2 = "there will be rain".split("\\s"); |
| 30 | + StringWriter sw = new StringWriter(); |
| 31 | + //bayes.dumpDb(sw); |
| 32 | + System.out.println(sw); |
| 33 | + System.out.println( // will output "positive" |
| 34 | + bayes.classify(new HashSet(Arrays.asList(unknownText1)))[0].getCategory()); |
| 35 | + System.out.println( // will output "negative" |
| 36 | + bayes.classify(new HashSet(Arrays.asList(unknownText2)))[0].getCategory()); |
| 37 | + |
| 38 | + |
| 39 | +``` |
| 40 | + |
| 41 | +The GNU GPLv3 License |
| 42 | +------------------ |
| 43 | +Copyright (c) 2018 - Elian Carsenat, NamSor SAS |
| 44 | +https://www.gnu.org/licenses/gpl-3.0.en.html |
0 commit comments