lector.Analizador.java Source code

Java tutorial

Introduction

Here is the source code for lector.Analizador.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 lector;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;
import weka.core.Instances;

public class Analizador {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        clasificador();
    }

    public static void clasificador() {

        BufferedReader reader1;
        BufferedReader reader2;
        try {
            reader1 = new BufferedReader(new FileReader("/Users/danieltapia/Google Drive/EPN/MAESTRIA/MSW128 BI/"
                    + "proyecto/compartida/DataSetAnalisisSentimientos.arff"));

            reader2 = new BufferedReader(new FileReader("/Users/danieltapia/Google Drive/EPN/MAESTRIA/MSW128 BI/"
                    + "proyecto/compartida/DataSetAnalisisSentimientos_inc.arff"));
            Instances train = new Instances(reader1);
            train.setClassIndex(train.numAttributes() - 1);
            System.out.println(train.classIndex() + " " + train.numAttributes());

            Instances test = new Instances(reader2);
            test.setClassIndex(train.numAttributes() - 1);
            System.out.println(test.classIndex() + " " + test.numAttributes());

            NaiveBayes model = new NaiveBayes();
            model.buildClassifier(train);

            //classify
            Instances labeled = new Instances(test);

            for (int i = 0; i < test.numInstances(); i++) {
                double clsLabel = model.classifyInstance(test.instance(i));
                labeled.instance(i).setClassValue(clsLabel);
            }

            // https://youtu.be/JY_x5zKTfyo?list=PLJbE6j2EG1pZnBhOg3_Rb63WLCprtyJag
            Evaluation eval_train = new Evaluation(test);
            eval_train.evaluateModel(model, test);

            reader1.close();
            reader2.close();

            //System.out.println(eval_train.toSummaryString("\nResults\n======\n", false));
            String[] options = new String[4];
            options[0] = "-t"; //name of training file
            options[1] = "/Users/danieltapia/Google Drive/EPN/MAESTRIA/MSW128 BI/proyecto/"
                    + "compartida/DataSetAnalisisSentimientos.arff";
            options[2] = "-T";
            options[3] = "/Users/danieltapia/Google Drive/EPN/MAESTRIA/MSW128 BI/proyecto/"
                    + "compartida/DataSetAnalisisSentimientos_inc.arff";
            System.out.println(Evaluation.evaluateModel(model, options));

            try ( // print classification results to file
                    BufferedWriter writer = new BufferedWriter(
                            new FileWriter("/Users/danieltapia/Google Drive/EPN/MAESTRIA/MSW128 BI/"
                                    + "proyecto/compartida/DataSetAnalisisSentimientos_labeled.arff"))) {
                writer.write(labeled.toString());
            }

        } catch (Exception e) {
        }
    }
}