Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.apache.mahout.classifier.bayes;
- import java.io.File;
- import java.io.FilenameFilter;
- import java.io.IOException;
- import java.nio.charset.Charset;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Map;
- import org.apache.commons.cli2.CommandLine;
- import org.apache.commons.cli2.Group;
- import org.apache.commons.cli2.Option;
- import org.apache.commons.cli2.OptionException;
- import org.apache.commons.cli2.builder.ArgumentBuilder;
- import org.apache.commons.cli2.builder.DefaultOptionBuilder;
- import org.apache.commons.cli2.builder.GroupBuilder;
- import org.apache.commons.cli2.commandline.Parser;
- import org.apache.mahout.classifier.ClassifierResult;
- import org.apache.mahout.classifier.ResultAnalyzer;
- import org.apache.mahout.classifier.bayes.algorithm.BayesAlgorithm;
- import org.apache.mahout.classifier.bayes.algorithm.CBayesAlgorithm;
- import org.apache.mahout.classifier.bayes.common.BayesParameters;
- import org.apache.mahout.classifier.bayes.datastore.HBaseBayesDatastore;
- import org.apache.mahout.classifier.bayes.datastore.InMemoryBayesDatastore;
- import org.apache.mahout.classifier.bayes.exceptions.InvalidDatastoreException;
- import org.apache.mahout.classifier.bayes.interfaces.Algorithm;
- import org.apache.mahout.classifier.bayes.interfaces.Datastore;
- import org.apache.mahout.classifier.bayes.mapreduce.bayes.BayesClassifierDriver;
- import org.apache.mahout.classifier.bayes.model.ClassifierContext;
- import org.apache.mahout.common.CommandLineUtil;
- import org.apache.mahout.common.FileLineIterable;
- import org.apache.mahout.common.HadoopUtil;
- import org.apache.mahout.common.TimingStatistics;
- import org.apache.mahout.common.commandline.DefaultOptionCreator;
- import org.apache.mahout.common.nlp.NGrams;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /**
- * Test the Naive Bayes classifier with improved weighting
- * <p/>
- * To run the twenty newsgroups example: refer
- http://cwiki.apache.org/MAHOUT/twentynewsgroups.html
- */
- public final class SinglyClassifier2 {
- private static ClassifierContext classifier;
- private static final Logger log =
- LoggerFactory.getLogger(SinglyClassifier2.class);
- Algorithm algorithm;
- Datastore datastore;
- /**
- * main method
- */
- public static void main(String[] args) throws Exception {
- String[] doc = {"mspublisher", "parallax", "polaroid", "corel", "illustrator", "coreldraw"};
- SinglyClassifier2 sc = new SinglyClassifier2();
- List<ClassifierResult> results = sc.classifyDocument(doc, "comp.graphics", 47);
- System.out.println(results);
- // System.err.println("keys: "+sc.datastore.getKeys("weight"));
- // double weight = sc.datastore.getWeight("weight", "味噌_名詞", "愛知県");
- // System.err.println("weight: "+weight);
- }
- /**
- * constructor
- */
- public SinglyClassifier2() throws Exception {
- BayesParameters params = new BayesParameters();
- params.setGramSize(1);
- params.set("basePath", "/user/toracluster/scope/run_24/model");
- params.set("classifierType", "cbayes");
- params.set("dataSource", "hdfs");
- params.set("defaultCat", "comp.graphics");
- params.set("encoding", "UTF-8");
- params.set("alpha_i", "1.0");
- params.setMinDF(1);
- params.set("verbose", "true");
- log.info("Loading model from: {}", params.print());
- boolean verbose = Boolean.valueOf(params.get("verbose"));
- if (params.get("dataSource").equals("hdfs")) {
- if (params.get("classifierType").equalsIgnoreCase("bayes")) {
- log.info("Testing Bayes Classifier");
- algorithm = new BayesAlgorithm();
- datastore = new InMemoryBayesDatastore(params);
- } else if (params.get("classifierType").equalsIgnoreCase("cbayes")) {
- log.info("Testing Complementary Bayes Classifier");
- algorithm = new CBayesAlgorithm();
- datastore = new InMemoryBayesDatastore(params);
- } else {
- throw new IllegalArgumentException("Unrecognized classifier type: " + params.get("classifierType"));
- }
- } else if (params.get("dataSource").equals("hbase")) {
- if (params.get("classifierType").equalsIgnoreCase("bayes")) {
- log.info("Testing Bayes Classifier");
- algorithm = new BayesAlgorithm();
- datastore = new HBaseBayesDatastore(params.get("basePath"), params);
- } else if (params.get("classifierType").equalsIgnoreCase("cbayes")) {
- log.info("Testing Complementary Bayes Classifier");
- algorithm = new CBayesAlgorithm();
- datastore = new HBaseBayesDatastore(params.get("basePath"), params);
- } else {
- throw new IllegalArgumentException("Unrecognized classifier type: " + params.get("classifierType"));
- }
- } else {
- throw new IllegalArgumentException("Unrecognized dataSource type: " + params.get("dataSource"));
- }
- classifier = new ClassifierContext(algorithm, datastore);
- //classifier.initialize();
- datastore.initialize();
- }
- /**
- *
- */
- public List<ClassifierResult> classifyDocument(String[] doc, String defaultCategory, int numResults) throws InvalidDatastoreException {
- algorithm.initialize(datastore);
- ClassifierResult[] results = classifier.classifyDocument(doc, defaultCategory, numResults);
- return Arrays.asList(results);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment