Sunday, July 1, 2012


Mahout Classification Example 

package mia.classifier.ch14;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;

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.common.BayesParameters;
import org.apache.mahout.classifier.bayes.datastore.InMemoryBayesDatastore;
import org.apache.mahout.classifier.bayes.interfaces.Algorithm;
import org.apache.mahout.classifier.bayes.interfaces.Datastore;
import org.apache.mahout.classifier.bayes.model.ClassifierContext;
import org.apache.mahout.common.nlp.NGrams;

public class ClassifierDemo {

        public static BayesParameters setParams() {
                BayesParameters params = new BayesParameters();
                params.set("verbose", "false");
                // params.set("basePath", modString);// modelDir is the dir where the
                //params.set("classifierType", "bayes");
                params.set("classifierType", "cbayes");
                //Interchange the values for swap between byaes and cbayes classifier
                params.set("dataSource", "hdfs");
                params.set("defaultCat", "unknown");
                params.set("encoding", "UTF-8");
                params.set("alpha_i", "1.0");
               
                params.set("gramSize", "1");

                return params;

        }

        public static ClassifierContext getClassifer(String modString)
                        throws Exception {
                BayesParameters params = setParams();
                params.set("basePath", modString);
                Algorithm algorithm = new BayesAlgorithm();// Creating the instance of
                Datastore datastore = new InMemoryBayesDatastore(params);// Creating
                ClassifierContext classifier = new ClassifierContext(algorithm, datastore);
                classifier.initialize();

                return classifier;

        }

        public static ClassifierResult getClassFordoc(String fileContent,
                        ClassifierContext classifier) throws Exception
                     {
       
       
                BayesParameters params = setParams();
               
                //System.out.println("11");
               
                ClassifierContext myClassifir = classifier;
               
                //System.out.println("22");
               
                // Object sentRes[] = new Object[3];
                Collection<String> labels = myClassifir.getLabels();
               
                System.out.println("labels = "+labels);
               
                //System.out.println("33");
               
               
                ArrayList<String> labelTest = new ArrayList<String>(labels);
               
                //System.out.println("44");
               
                ResultAnalyzer resultAnalyzer = new ResultAnalyzer(classifier.getLabels(), params.get("defaultCat"));
               
                //System.out.println("55");

                System.out.println(params.get("gramSize"));
               
                List<String> document = new NGrams(fileContent, Integer.parseInt(params.get("gramSize"))).generateNGramsWithoutLabel();

                //System.out.println("66");
               
                ClassifierResult result = classifier.classifyDocument(
                                document.toArray(new String[document.size()]),
                                params.get("defaultCat"));

                //System.out.println("77");
               
                /*
                 * ClassifierResult[] resNew = classifier.classifyDocument(document
                 * .toArray(new String[document.size()]), params.get("defaultCat"), 2);
                 *
                 * Hashtable categVals = new Hashtable();
                 *
                 * for (ClassifierResult re : resNew) { categVals.put(re.getLabel(), new
                 * Double(re.getScore())); } double approve = ((Double)
                 * categVals.get("positive")).doubleValue(); double reject = ((Double)
                 * categVals.get("negative")).doubleValue();
                 */

                // String sentRes = result.getLabel().toString();

                // sentRes[1] = approve;
                // sentRes[2] = reject;

                return result;

        }

        /**
         * @param args
         * @throws Exception
         */
        public static void main(String[] args) throws Exception
        {
               // String test = "from wtm@uhura.neoucom.edu bill mayhew subject re mysterious tv problem source organization northeastern ohio universities college medicine lines 19 set direct line powered try checking likely hybrid regulator module down stream from 170 volt supply several sets i've looked use 135 volt regulator regulators have tendency short out making safety circuits shut down eht supply section try putting set variac adjustable transformer lower ac input voltage set about 90 volts set operates nromally you know you've got shorted regulator myriad other areas problems i've seen one above several times also set uses one trippler module may shot fairly common bill mayhew neoucom computer services department rootstown oh 44272-9995 usa phone 216-325-2511 wtm@uhura.neoucom.edu 140.220.1.1 146.580 n8wed ";
               
                String test = "இரண்டு நாள் பயணமாக பிரதமர் மன்மோகன் சிங் புதுச்சேரி வருகை: பலத்த பாதுகாப்பு டெல்லியில் இருந்து சென்னை வந்த மத்திய மந்திரி நாராயணசாமி விமான நிலையத்தில் நிருபர்களுக்கு பேட்டி அளித்தார். அப்போது அவர் கூறியதாவது: பிரதமர் மன்மோகன்சிங் 2 நாள் பயணமாக புதுச்சேரிக்கு வருகிற 29 ந்தேதி வருகிறார். ஜிப்மர் மருத்துவமனை மற்றும் பல்வேறு நிகழ்ச்சிகளில் கலந்துக் கொள்கிறார். இதற்காக பிரதமர் டெல்லியில் இருந்து சென்னை வந்து இங்கிருந்து ஹெலிகாப்டர் மூலம் புதுச்சேரி வருகிறார் என்றார். வருகிற 29, 30 ஆகிய 2 நாட்களிலும் புதுவையில் பிரதமர் மன்மோகன் சிங் இருப்பதை முன்னிட்டு பாதுகாப்பு ஏற்பாடுகள் தீவிரமாக முடுக்கி விடப்பட்டுள்ளது. பிரதமரின் 2 நாள் நிகழ்ச்சிகளும் உறுதி செய்யப்பட்டதில் இருந்தே புதுவையில் தங்கியுள்ள வெளிமாநிலத்தவர்கள் பற்றிய தகவல்களை போலீசார் சேகரிக்க தொடங்கியுள்ளனர்.";
                //String modelDir = "/home/jaganadhg/backup/Desktop/mahout-distribution-0.4/20cbayesn";
               
                //String modelDir = "/home/venkat/Desktop/mahout/mahout-distribution-0.5/examples/bin/work/20news-bydate";
                String modelDir = "/home/venkat/Desktop/mahout/mahout-distribution-0.5/examples/bin/work/20news-bydate/bayes-model";
               
                //String modelDir = "/home/jaganadhg/backup/Desktop/mahout-distribution-0.4/20bayes";
                //Interchange the values for swap between byaes and cbayes classifier
                // for bayes
               
                ClassifierContext bck9cl = getClassifer(modelDir);
               
              //  System.out.println("one");

                ClassifierResult res = getClassFordoc(test, bck9cl);
               
              //  System.out.println("two");
               
                System.out.println("Label: " + res.getLabel() + " Score: " + res.getScore());              
        }

}

No comments:

Post a Comment