<commit_before>// Copyright 2015 Georg-August-Universität Göttingen, Germany
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

package de.ugoe.cs.cpdp.wekaclassifier;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import de.ugoe.cs.util.console.Console;
import weka.classifiers.AbstractClassifier;
import weka.classifiers.Classifier;
import weka.classifiers.bayes.BayesNet;
import weka.classifiers.functions.Logistic;
import weka.classifiers.functions.MultilayerPerceptron;
import weka.classifiers.functions.RBFNetwork;
import weka.classifiers.rules.DecisionTable;
import weka.classifiers.trees.ADTree;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;

/**
 * <p>
 * Implements CODEP proposed by Panichella et al. (2014).
 * </p>
 * 
 * @author <NAME>
 */
public abstract class AbstractCODEP extends AbstractClassifier {

    /**
     * Default serialization ID.
     */
    private static final long serialVersionUID = 1L;

    /**
     * List of classifiers that is internally used.
     */
    private List<Classifier> internalClassifiers = null;

    /**
     * List of attributes that is internally used.
     */
    private ArrayList<Attribute> internalAttributes = null;

    /**
     * Trained CODEP classifier.
     */
    private Classifier codepClassifier = null;

    /*
     * (non-Javadoc)
     * 
     * @see weka.classifiers.AbstractClassifier#classifyInstance(weka.core.Instance)
     */
    @Override
    public double classifyInstance(Instance instance) throws Exception {
        if (codepClassifier == null) {
            throw new RuntimeException("classifier must be trained first, call to buildClassifier missing");
        }
        Instances tmp = new Instances(instance.dataset());
        tmp.clear();
        tmp.add(createInternalInstance(instance));
        return codepClassifier.classifyInstance(tmp.firstInstance());
    }

    /*
     * (non-Javadoc)
     * 
     * @see weka.classifiers.Classifier#buildClassifier(weka.core.Instances)
     */
    @Override
    public void buildClassifier(Instances traindata) throws Exception {
        setupInternalClassifiers();
        setupInternalAttributes();

        for (Classifier classifier : internalClassifiers) {
            Console.traceln(Level.FINE, "internally training " + classifier.getClass().getName());
            classifier.buildClassifier(traindata);
        }

        Instances internalTraindata =
            new Instances("internal instances", internalAttributes, traindata.size());
        internalTraindata.setClass(internalAttributes.get(internalAttributes.size() - 1));<commit_msg>* second (and now working) attempt to fix the bug in AbstractCODEP<commit_after>// Copyright 2015 Georg-August-Universität Göttingen, Germany
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

package de.ugoe.cs.cpdp.wekaclassifier;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import de.ugoe.cs.util.console.Console;
import weka.classifiers.AbstractClassifier;
import weka.classifiers.Classifier;
import weka.classifiers.bayes.BayesNet;
import weka.classifiers.functions.Logistic;
import weka.classifiers.functions.MultilayerPerceptron;
import weka.classifiers.functions.RBFNetwork;
import weka.classifiers.rules.DecisionTable;
import weka.classifiers.trees.ADTree;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;

/**
 * <p>
 * Implements CODEP proposed by Panichella et al. (2014).
 * </p>
 * 
 * @author <NAME>
 */
public abstract class AbstractCODEP extends AbstractClassifier {

    /**
     * Default serialization ID.
     */
    private static final long serialVersionUID = 1L;

    /**
     * List of classifiers that is internally used.
     */
    private List<Classifier> internalClassifiers = null;

    /**
     * List of attributes that is internally used.
     */
    private ArrayList<Attribute> internalAttributes = null;

    /**
     * Trained CODEP classifier.
     */
    private Classifier codepClassifier = null;

    /*
     * (non-Javadoc)
     * 
     * @see weka.classifiers.AbstractClassifier#classifyInstance(weka.core.Instance)
     */
    @Override
    public double classifyInstance(Instance instance) throws Exception {
        if (codepClassifier == null) {
            throw new RuntimeException("classifier must be trained first, call to buildClassifier missing");
        }
        Instances tmp = new Instances("tmp", internalAttributes, 1);
        tmp.setClass(internalAttributes.get(internalAttributes.size() - 1));
        tmp.add(createInternalInstance(instance));
        return codepClassifier.classifyInstance(tmp.firstInstance());
    }

    /*
     * (non-Javadoc)
     * 
     * @see weka.classifiers.Classifier#buildClassifier(weka.core.Instances)
     */
    @Override
    public void buildClassifier(Instances traindata) throws Exception {
        setupInternalClassifiers();
        setupInternalAttributes();

        for (Classifier classifier : internalClassifiers) {
            Console.traceln(Level.FINE, "internally training " + classifier.getClass().getName());
            classifier.buildClassifier(traindata);
        }

        Instances internalTraindata =
            new Instances("internal instances", internalAttributes, traindata.size());
        internalTraindata.setClass(internalAttributes.get(internalAttributes.size() - 1));
{'fraction_non_alphanumeric': 0.09784765684628301, 'fraction_numerical': 0.004579453518546787, 'mean_word_length': 3.293577981651376, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3018907', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1786, 'n_words': 485}