Guest User

Untitled

a guest
Mar 1st, 2011
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. package org.apache.mahout.classifier.bayes;
  2.  
  3. import java.io.File;
  4. import java.io.FilenameFilter;
  5. import java.io.IOException;
  6. import java.nio.charset.Charset;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. import java.util.Map;
  10.  
  11. import org.apache.commons.cli2.CommandLine;
  12. import org.apache.commons.cli2.Group;
  13. import org.apache.commons.cli2.Option;
  14. import org.apache.commons.cli2.OptionException;
  15. import org.apache.commons.cli2.builder.ArgumentBuilder;
  16. import org.apache.commons.cli2.builder.DefaultOptionBuilder;
  17. import org.apache.commons.cli2.builder.GroupBuilder;
  18. import org.apache.commons.cli2.commandline.Parser;
  19. import org.apache.mahout.classifier.ClassifierResult;
  20. import org.apache.mahout.classifier.ResultAnalyzer;
  21. import org.apache.mahout.classifier.bayes.algorithm.BayesAlgorithm;
  22. import org.apache.mahout.classifier.bayes.algorithm.CBayesAlgorithm;
  23. import org.apache.mahout.classifier.bayes.common.BayesParameters;
  24. import org.apache.mahout.classifier.bayes.datastore.HBaseBayesDatastore;
  25. import org.apache.mahout.classifier.bayes.datastore.InMemoryBayesDatastore;
  26. import org.apache.mahout.classifier.bayes.exceptions.InvalidDatastoreException;
  27. import org.apache.mahout.classifier.bayes.interfaces.Algorithm;
  28. import org.apache.mahout.classifier.bayes.interfaces.Datastore;
  29. import org.apache.mahout.classifier.bayes.mapreduce.bayes.BayesClassifierDriver;
  30. import org.apache.mahout.classifier.bayes.model.ClassifierContext;
  31. import org.apache.mahout.common.CommandLineUtil;
  32. import org.apache.mahout.common.FileLineIterable;
  33. import org.apache.mahout.common.HadoopUtil;
  34. import org.apache.mahout.common.TimingStatistics;
  35. import org.apache.mahout.common.commandline.DefaultOptionCreator;
  36. import org.apache.mahout.common.nlp.NGrams;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39.  
  40. /**
  41. * Test the Naive Bayes classifier with improved weighting
  42. * <p/>
  43. * To run the twenty newsgroups example: refer
  44. http://cwiki.apache.org/MAHOUT/twentynewsgroups.html
  45. */
  46. public final class SinglyClassifier2 {
  47.     private static ClassifierContext classifier;
  48.     private static final Logger log =
  49.         LoggerFactory.getLogger(SinglyClassifier2.class);
  50.     Algorithm algorithm;
  51.     Datastore datastore;
  52.    
  53.     /**
  54.      * main method
  55.      */
  56.     public static void main(String[] args) throws Exception {
  57.         String[] doc = {"mspublisher", "parallax", "polaroid", "corel", "illustrator", "coreldraw"};
  58.         SinglyClassifier2 sc = new SinglyClassifier2();
  59.        
  60.         List<ClassifierResult> results = sc.classifyDocument(doc, "comp.graphics", 47);
  61.         System.out.println(results);
  62.        
  63. //        System.err.println("keys: "+sc.datastore.getKeys("weight"));
  64. //        double weight = sc.datastore.getWeight("weight", "味噌_名詞", "愛知県");
  65. //        System.err.println("weight: "+weight);
  66.     }
  67.  
  68.     /**
  69.      * constructor
  70.      */
  71.     public SinglyClassifier2() throws Exception {
  72.  
  73.         BayesParameters params = new BayesParameters();
  74.         params.setGramSize(1);
  75.         params.set("basePath", "/user/toracluster/scope/run_24/model");
  76.         params.set("classifierType", "cbayes");
  77.         params.set("dataSource", "hdfs");
  78.         params.set("defaultCat", "comp.graphics");
  79.         params.set("encoding", "UTF-8");
  80.         params.set("alpha_i", "1.0");
  81.         params.setMinDF(1);
  82.         params.set("verbose", "true");
  83.  
  84.         log.info("Loading model from: {}", params.print());
  85.         boolean verbose = Boolean.valueOf(params.get("verbose"));
  86.  
  87.         if (params.get("dataSource").equals("hdfs")) {
  88.             if (params.get("classifierType").equalsIgnoreCase("bayes")) {
  89.                 log.info("Testing Bayes Classifier");
  90.                 algorithm = new BayesAlgorithm();
  91.                 datastore = new InMemoryBayesDatastore(params);
  92.             } else if (params.get("classifierType").equalsIgnoreCase("cbayes")) {
  93.                 log.info("Testing Complementary Bayes Classifier");
  94.                 algorithm = new CBayesAlgorithm();
  95.                 datastore = new InMemoryBayesDatastore(params);
  96.             } else {
  97.                 throw new IllegalArgumentException("Unrecognized classifier type: " + params.get("classifierType"));
  98.             }
  99.            
  100.         } else if (params.get("dataSource").equals("hbase")) {
  101.             if (params.get("classifierType").equalsIgnoreCase("bayes")) {
  102.                 log.info("Testing Bayes Classifier");
  103.                 algorithm = new BayesAlgorithm();
  104.                 datastore = new HBaseBayesDatastore(params.get("basePath"), params);
  105.             } else if (params.get("classifierType").equalsIgnoreCase("cbayes")) {
  106.                 log.info("Testing Complementary Bayes Classifier");
  107.                 algorithm = new CBayesAlgorithm();
  108.                 datastore = new HBaseBayesDatastore(params.get("basePath"), params);
  109.             } else {
  110.                 throw new IllegalArgumentException("Unrecognized classifier type: " + params.get("classifierType"));
  111.             }
  112.            
  113.         } else {
  114.             throw new IllegalArgumentException("Unrecognized dataSource type: " + params.get("dataSource"));
  115.         }
  116.         classifier = new ClassifierContext(algorithm, datastore);
  117.         //classifier.initialize();
  118.         datastore.initialize();
  119.     }
  120.    
  121.     /**
  122.      *
  123.      */
  124.     public List<ClassifierResult> classifyDocument(String[] doc, String defaultCategory, int numResults) throws InvalidDatastoreException {
  125.         algorithm.initialize(datastore);
  126.         ClassifierResult[] results = classifier.classifyDocument(doc, defaultCategory, numResults);
  127.         return Arrays.asList(results);
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment