<commit_before>/*
 * Copyright 2014 <NAME> and <NAME> and <NAME> and <NAME>
 * 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 it.uniroma2.sag.kelp.learningalgorithm.classification.multiclassification;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonTypeName;

import it.uniroma2.sag.kelp.data.dataset.Dataset;
import it.uniroma2.sag.kelp.data.label.Label;
import it.uniroma2.sag.kelp.learningalgorithm.LearningAlgorithm;
import it.uniroma2.sag.kelp.learningalgorithm.MetaLearningAlgorithm;
import it.uniroma2.sag.kelp.learningalgorithm.classification.ClassificationLearningAlgorithm;
import it.uniroma2.sag.kelp.predictionfunction.PredictionFunction;
import it.uniroma2.sag.kelp.predictionfunction.classifier.Classifier;
import it.uniroma2.sag.kelp.predictionfunction.classifier.multiclass.OneVsAllClassifier;


/**
 * It is a meta algorithm that operates applying a One-Vs-All strategy over the base 
 * learning algorithm which is intended to be a binary learner. 
 * The One-Vs-All strategy will learn N different classifiers, where N is the number
 * of classes involved in the dataset.
 * In this strategy each classifier is learned by considering in turn the examples of a single 
 * class as positives, while all the other examples are considered as negative.
 * <p>
 * 
 * NOTE: the base learning algorithm must provide a duplicate method which properly works
 * 
 * @author      <NAME>, <NAME>
 */
@JsonTypeName("oneVsAll")
public class OneVsAllLearning implements ClassificationLearningAlgorithm, MetaLearningAlgorithm{
	private Logger logger = LoggerFactory.getLogger(OneVsAllLearning.class);

	private LearningAlgorithm baseAlgorithm;
	
	@JsonIgnore
	private LearningAlgorithm[] algorithms;
	
	@JsonIgnore
	private OneVsAllClassifier classifier;
	
	private List<Label> labels;
	
	public OneVsAllLearning(){
		this.classifier = new OneVsAllClassifier();
	}
	
	/**
	 * Set the labels associated to this multi-classifier.
	 */
	@Override
	public void setLabels(List<Label> labels){
		this.labels=labels;
		this.classifier.setLabels(labels);
	}
		
	/**
	 * Returns the labels to be learned applying a one-vs-all strategy
	 * 
	 * @return the labels to be learned
	 */
	public List<Label> getLabels(){
		return labels;
	}
		
	public void initialize() {
		System.out.println("QUI!");
		algorithms = new LearningAlgorithm[labels.size()];		
		Classifier[] binaryClassifier = new Classifier[labels.size()];
		for(int i=0; i<labels.size(); i++){
			try {
				algorithms[i] = this.baseAlgorithm.duplicate();
				algorithms[i].setLabels(Arrays.asList(labels.get(i)));
				binaryClassifier[i] = (Classifier)algorithms[i].getPredictionFunction();
			} catch (Exception e) {
				logger.error(e.getMessage());
				e.printStackTrace();
				System.exit(0);
			}
		}
		classifier.setBinaryClassifiers(binaryClassifier);	
		
	}
	
	public void initializeWithPredictionFunction(OneVsAllClassifier oneVsAllClassifier) {
		algorithms = new LearningAlgorithm[labels.size()];		<commit_msg>removed redundant print<commit_after>/*
 * Copyright 2014 <NAME> and <NAME> and <NAME> and <NAME>
 * 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 it.uniroma2.sag.kelp.learningalgorithm.classification.multiclassification;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonTypeName;

import it.uniroma2.sag.kelp.data.dataset.Dataset;
import it.uniroma2.sag.kelp.data.label.Label;
import it.uniroma2.sag.kelp.learningalgorithm.LearningAlgorithm;
import it.uniroma2.sag.kelp.learningalgorithm.MetaLearningAlgorithm;
import it.uniroma2.sag.kelp.learningalgorithm.classification.ClassificationLearningAlgorithm;
import it.uniroma2.sag.kelp.predictionfunction.PredictionFunction;
import it.uniroma2.sag.kelp.predictionfunction.classifier.Classifier;
import it.uniroma2.sag.kelp.predictionfunction.classifier.multiclass.OneVsAllClassifier;


/**
 * It is a meta algorithm that operates applying a One-Vs-All strategy over the base 
 * learning algorithm which is intended to be a binary learner. 
 * The One-Vs-All strategy will learn N different classifiers, where N is the number
 * of classes involved in the dataset.
 * In this strategy each classifier is learned by considering in turn the examples of a single 
 * class as positives, while all the other examples are considered as negative.
 * <p>
 * 
 * NOTE: the base learning algorithm must provide a duplicate method which properly works
 * 
 * @author      <NAME>, <NAME>
 */
@JsonTypeName("oneVsAll")
public class OneVsAllLearning implements ClassificationLearningAlgorithm, MetaLearningAlgorithm{
	private Logger logger = LoggerFactory.getLogger(OneVsAllLearning.class);

	private LearningAlgorithm baseAlgorithm;
	
	@JsonIgnore
	private LearningAlgorithm[] algorithms;
	
	@JsonIgnore
	private OneVsAllClassifier classifier;
	
	private List<Label> labels;
	
	public OneVsAllLearning(){
		this.classifier = new OneVsAllClassifier();
	}
	
	/**
	 * Set the labels associated to this multi-classifier.
	 */
	@Override
	public void setLabels(List<Label> labels){
		this.labels=labels;
		this.classifier.setLabels(labels);
	}
		
	/**
	 * Returns the labels to be learned applying a one-vs-all strategy
	 * 
	 * @return the labels to be learned
	 */
	public List<Label> getLabels(){
		return labels;
	}
		
	public void initialize() {
		algorithms = new LearningAlgorithm[labels.size()];		
		Classifier[] binaryClassifier = new Classifier[labels.size()];
		for(int i=0; i<labels.size(); i++){
			try {
				algorithms[i] = this.baseAlgorithm.duplicate();
				algorithms[i].setLabels(Arrays.asList(labels.get(i)));
				binaryClassifier[i] = (Classifier)algorithms[i].getPredictionFunction();
			} catch (Exception e) {
				logger.error(e.getMessage());
				e.printStackTrace();
				System.exit(0);
			}
		}
		classifier.setBinaryClassifiers(binaryClassifier);	
		
	}
	
	public void initializeWithPredictionFunction(OneVsAllClassifier oneVsAllClassifier) {
		algorithms = new LearningAlgorithm[labels.size()];		
{'fraction_non_alphanumeric': 0.09455744914788346, 'fraction_numerical': 0.00577240241891149, 'mean_word_length': 4.711930926216641, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '6278282', 'n_tokens_mistral': 2291, 'n_tokens_neox': 2053, 'n_words': 613}