gr.uoc.nlp.opinion.analysis.suggestion.AnalyzeSuggestions.java Source code

Java tutorial

Introduction

Here is the source code for gr.uoc.nlp.opinion.analysis.suggestion.AnalyzeSuggestions.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gr.uoc.nlp.opinion.analysis.suggestion;

import gr.uoc.nlp.opinion.analysis.WekaClassifiers;
import gr.uoc.nlp.opinion.analysis.arguments.AnalyzeArguments;
import gr.uoc.nlp.opinion.mysql.MysqlConnect;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.core.Instances;
import weka.experiment.InstanceQuery;

/**
 *
 * @author smyrgeorge
 */
public class AnalyzeSuggestions {

    private final MysqlConnect connection;
    private final Instances trainset;
    private String classifierOutput;

    /**
     *
     * @param con
     */
    public AnalyzeSuggestions(MysqlConnect con) {
        this.connection = con;
        this.trainset = this.retriveTrainSet();
    }

    private Instances retriveTrainSet() {
        System.out.println("Retrieving dataset from Database..");

        InstanceQuery query;

        try {
            //initialize database, weka api
            query = new InstanceQuery();

            //set database attributes, weka apit
            query.setDatabaseURL(this.connection.getJdbcUrl());
            query.setUsername(this.connection.getUsername());
            query.setPassword(this.connection.getPassword());
            query.setQuery(this.queryTrainset());

            //retrieve trainset
            Instances data = query.retrieveInstances();
            data.setClassIndex(data.numAttributes() - 1);

            System.out.println("Done retrieving dataset from Database!");

            return data;
        } catch (Exception ex) {
            System.err.println("Abort!");
            Logger.getLogger(AnalyzeArguments.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    /**
     *
     * @param classifier
     * @return
     */
    public Classifier classifier(String classifier) {

        Classifier clsr;

        //opportunity to select classifier, randomfores suggested for Suggestions
        switch (classifier) {
        case "smo":
            clsr = WekaClassifiers.classifierSMO(this.trainset);
            this.classifierOutput = WekaClassifiers.classifierOutput;
            return clsr;
        case "randomforest":
            clsr = WekaClassifiers.classifierRandomForest(this.trainset);
            this.classifierOutput = WekaClassifiers.classifierOutput;
            return clsr;
        }

        return null;
    }

    private String queryTrainset() {

        //query to get trainset from database
        String q = "SELECT\n" + "   opngv_suggestion.weight,\n" + "   opngv_suggestion.category,\n"
                + "   opngv_suggestion.total_words,\n" + "   opngv_trainset.Suggestion\n" + "FROM\n"
                + "   opngv_sentence\n" + "   INNER JOIN opngv_suggestion\n"
                + "    ON opngv_sentence.comment_id = opngv_suggestion.comment_id\n"
                + "     AND opngv_sentence.sentence_id = opngv_suggestion.sentence_id\n"
                + "   INNER JOIN opngv_trainset\n"
                + "    ON opngv_sentence.comment_id = opngv_trainset.comment_id\n"
                + "     AND opngv_sentence.sentence_id = opngv_trainset.sentence_id\n" + "ORDER BY\n"
                + "   opngv_trainset.Suggestion DESC\n" + "LIMIT 366";

        return q;
    }

    /**
     *
     * @param classifier
     * @param unclassified
     * @return
     */
    public Instances classify(Classifier classifier, Instances unclassified) {

        unclassified.setClassIndex(unclassified.numAttributes() - 1);

        //new set wich will contain classifies instances
        Instances classified = new Instances(unclassified);

        double clsLabel;
        try {
            for (int i = 0; i < unclassified.numInstances(); i++) {
                //for each unclassifies, classify
                clsLabel = classifier.classifyInstance(unclassified.instance(i));
                //append result to final set
                classified.instance(i).setClassValue(clsLabel);
            }

        } catch (Exception ex) {
            Logger.getLogger(AnalyzeArguments.class.getName()).log(Level.SEVERE, null, ex);
        }

        return classified;
    }

    /**
     *
     * @param classifier
     */
    public void crossValidationTrainSet(Classifier classifier) {

        Evaluation eval;
        try {
            //initialize cross validation
            eval = new Evaluation(this.trainset);
            //validate
            eval.crossValidateModel(classifier, this.trainset, 10, new Random(1));

            System.out.println(eval.toSummaryString());
            System.out.println(eval.toClassDetailsString());
            System.out.println(eval.toMatrixString());

        } catch (Exception ex) {
            Logger.getLogger(AnalyzeSuggestions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     *
     * @param classifier
     * @param testset
     */
    public void valuateSet(Classifier classifier, Instances testset) {

        Evaluation eval;
        try {
            eval = new Evaluation(this.trainset);
            eval.evaluateModel(classifier, testset);

            System.out.println(eval.toSummaryString());
            System.out.println(eval.toClassDetailsString());
            System.out.println(eval.toMatrixString());
        } catch (Exception ex) {
            Logger.getLogger(AnalyzeSuggestions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     *
     * @return
     */
    public String getClassifierOutput() {
        return this.classifierOutput;
    }
}