sid684
sid684

Reputation: 21

Java application outside IDE is not using Weka classes

I am facing some issues when trying to run my application outside the Netbeans IDE. I have a GUI which is calling calling a few of my own classes and methods, which in turn, is calling some Weka classes. The Weka class is giving an output,which is getting displayed on the GUI. Now when I do this on the IDE, the output gets displayed correctly. However, when I try to create a jar of my application and try to run the jar from outside the IDE, the output from the Weka class is not displayed. My classes, which I am using to do some parsing and processing of the data are working fine, though. Initially I thought it was a problem of external jar not getting included properly, but I checked the *PROJECT_HOME*/dist/lib folder and the weka.jar file seemed to be there. Can someone please help me out on this?

Thanks in advance!

Upvotes: 2

Views: 1084

Answers (1)

Kaustubh Hiwarekar
Kaustubh Hiwarekar

Reputation: 11

One thing I can tell you I have developed one application outside IDE. You just need to get WEKA package folder in your application directory and import it as package in your java class application and directly call the WEKA classifiers or any other work you wish. for example I am using PART Classifier following code works and speaks for itself.

import java.io.*;
import java.util.Random;
import weka.core.Instances;
import weka.classifiers.Classifier;
import weka.classifiers.rules.PART;
import weka.classifiers.Evaluation;


public class Prediction 
 {

     public double Create(String Rf1,String Rf2,String Rf3) throws Exception
    {
        input p = new input();
    double res=p.classify(Rf1,Rf2,Rf3);
    return res;
    }
 }


  class input
    {
        public double classify(String file1,String file2,String file3) throws Exception
        {
     // ------------> 1. Reading from an ARFF file

            FileReader fr = new FileReader(file1);
            BufferedReader br = new BufferedReader(fr);

                Instances data = new Instances(br);
            br.close();

            // setting class attribute
            data.setClassIndex(data.numAttributes() - 1);

            // -------------> 2. Building a Classifier

            String[] options = new String[1];
            options[0] = "M 2 -C 0.25 -Q 1"; // confidenceFactor = 0.25, minNumObject = 2
            PART tree = new PART(); // new instance of tree
            tree.setOptions(options); // set the options
            tree.buildClassifier(data); // build classifier
            // -------------> 3. Cross-validation

            Evaluation eval = new Evaluation(data);
            eval.crossValidateModel(tree, data, 10, new Random(1));

            // check --------------> 4. Train

            Instances train = new Instances(data);
            Instances test = new Instances(train);

            // train classifier
            Classifier cls = new PART();
            cls.buildClassifier(train);

            // evaluate classifier and print some statistics
            Evaluation eval1 = new Evaluation(train);
            eval1.evaluateModel(cls, test);

            //System.out.println(eval1.toSummaryString("\nResults\n======\n", false));

            // ----------------> 5. Statistics

            String[] options1 = new String[2];
            options1[0] = "-t";
            options1[1] = file1;

            // ----------------> 6. Classifying instances

            // load unlabeled data
            FileReader fr2 = new FileReader(file2);
            BufferedReader br2 = new BufferedReader(fr2);

            Instances unlabeled = new Instances(br2);

            // set class attribute
            unlabeled.setClassIndex(unlabeled.numAttributes() - 1);

            // create copy
            Instances labeled = new Instances(unlabeled);  
            double clsLabel[];
            clsLabel = new double[100];
            int count = 0;

            // label instances
            for (int i = 0; i < unlabeled.numInstances(); i++)
            {
                clsLabel[i] = tree.classifyInstance(unlabeled.instance(i));
                labeled.instance(i).setClassValue(clsLabel[i]);
                count++;
            }

            // save labeled data
            FileWriter fw = new FileWriter(file3);
            BufferedWriter bw = new BufferedWriter(fw);

            bw.write(labeled.toString());
            bw.newLine();
            bw.flush();
            bw.close();
            fw.close();

                for(int i=0;i<count;i++)
                System.out.println(clsLabel[i]);
            return clsLabel[0];
        } // main
    } // class

Upvotes: 1

Related Questions