<commit_before>package com.github.TKnudsen.DMandML.model.supervised.classifier;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.github.TKnudsen.ComplexDataObject.data.probability.ProbabilityDistribution;
import com.github.TKnudsen.ComplexDataObject.model.tools.MathFunctions;
import com.github.TKnudsen.DMandML.data.classification.IProbabilisticClassificationResult;
import com.github.TKnudsen.DMandML.data.classification.LabelDistribution;
import com.github.TKnudsen.DMandML.data.classification.LabelDistributionTools;
import com.github.TKnudsen.DMandML.data.classification.ProbabilisticClassificationResult;

public class EnsembleClassifier<FV> implements IProbabilisticClassifier<FV> {

	private final List<IProbabilisticClassifier<FV>> classifiers;
	private final String classAttribute;

	public EnsembleClassifier(Collection<? extends IProbabilisticClassifier<FV>> classifiers) {
		this.classAttribute = extractClassAttribute(classifiers);
		this.classifiers = new ArrayList<IProbabilisticClassifier<FV>>(classifiers);
	}

	private static String extractClassAttribute(Iterable<? extends IClassifier<?>> classifiers) {
		Set<String> classAttributes = new HashSet<String>();
		for (IClassifier<?> classifier : classifiers) {
			classAttributes.add(classifier.getClassAttribute());
		}
		if (classAttributes.size() != 1) {
			throw new IllegalArgumentException("The classifiers have different class attributes: " + classAttributes);
		}
		return classAttributes.iterator().next();
	}

	@Override
	public String getClassAttribute() {
		return classAttribute;
	}

	@Override
	public void train(List<FV> featureVectors) {

		// TODO Implementations do this. Consider it as part of the contract.
		if (featureVectors.size() < 2) {
			throw new IllegalArgumentException("at least two training instances required");
		}

		for (IProbabilisticClassifier<FV> classifier : classifiers) {
			try {
				classifier.train(featureVectors);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public List<String> test(List<FV> featureVectors) {
		List<String> labels = new ArrayList<String>();
		for (FV fv : featureVectors) {
			Map<String, Double> labelDistribution = getLabelDistribution(fv);
			Entry<String, Double> entryWithHighestProbability = Collections.max(labelDistribution.entrySet(),
					Map.Entry.comparingByValue());
			String label = entryWithHighestProbability.getKey();
			labels.add(label);
		}
		return labels;
	}

	@Override
	public Map<String, Double> getLabelDistribution(FV featureVector) {

		Map<String, List<Double>> labelDistributions = new HashMap<>();

		for (IProbabilisticClassifier<FV> classifier : classifiers) {
			Map<String, Double> labelDistribution = classifier.getLabelDistribution(featureVector);

			if (labelDistribution == null) {
				System.err.println(<commit_msg>ensemble classifier<commit_after>package com.github.TKnudsen.DMandML.model.supervised.classifier;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.github.TKnudsen.ComplexDataObject.data.probability.ProbabilityDistribution;
import com.github.TKnudsen.ComplexDataObject.model.tools.MathFunctions;
import com.github.TKnudsen.DMandML.data.classification.IProbabilisticClassificationResult;
import com.github.TKnudsen.DMandML.data.classification.LabelDistribution;
import com.github.TKnudsen.DMandML.data.classification.LabelDistributionTools;
import com.github.TKnudsen.DMandML.data.classification.ProbabilisticClassificationResult;

public class EnsembleClassifier<FV> implements IProbabilisticClassifier<FV> {

	private final List<IProbabilisticClassifier<FV>> classifiers;
	private final String classAttribute;

	public EnsembleClassifier(Collection<? extends IProbabilisticClassifier<FV>> classifiers) {
		this.classAttribute = extractClassAttribute(classifiers);
		this.classifiers = new ArrayList<IProbabilisticClassifier<FV>>(classifiers);
	}

	private static String extractClassAttribute(Iterable<? extends IClassifier<?>> classifiers) {
		Set<String> classAttributes = new HashSet<String>();
		for (IClassifier<?> classifier : classifiers) {
			classAttributes.add(classifier.getClassAttribute());
		}
		if (classAttributes.size() != 1) {
			throw new IllegalArgumentException("The classifiers have different class attributes: " + classAttributes);
		}
		return classAttributes.iterator().next();
	}

	@Override
	public String getClassAttribute() {
		return classAttribute;
	}

	@Override
	public void train(List<FV> featureVectors) {

		// TODO Implementations do this. Consider it as part of the contract.
		if (featureVectors.size() < 2) {
			throw new IllegalArgumentException("at least two training instances required");
		}

		for (IProbabilisticClassifier<FV> classifier : classifiers) {
			try {
				classifier.train(featureVectors);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public List<String> test(List<FV> featureVectors) {
		List<String> labels = new ArrayList<String>();
		for (FV fv : featureVectors) {
			Map<String, Double> labelDistribution = getLabelDistribution(fv);
			if (labelDistribution != null && labelDistribution.size() == 0)
				labels.add(null);
			else {
				Entry<String, Double> entryWithHighestProbability = Collections.max(labelDistribution.entrySet(),
						Map.Entry.comparingByValue());
				String label = entryWithHighestProbability.getKey();
				labels.add(label);
			}
		}
		return labels;
	}

	@Override
	public Map<String, Double> getLabelDistribution(FV featureVector) {

		Map<String, List<Double>> labelDistributions = new HashMap<>();

		for (IProbabilisticClassifier<FV> classifier : classifiers) {
			Map<String, Double> labelDistribution = classifier.getLabelDistribution(featureVector);

			if (labelDistribution == null) {
				System.err.println(
{'fraction_non_alphanumeric': 0.1051543111676742, 'fraction_numerical': 0.0007954183900731785, 'mean_word_length': 5.856052344601963, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6894148', 'n_tokens_mistral': 2055, 'n_tokens_neox': 1790, 'n_words': 373}