<commit_before>package univ.ml.sparse.algorithm;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.math3.linear.RealVector;

import univ.ml.sparse.SparseCentroidCluster;
import univ.ml.sparse.SparseWeightableVector;

public class SparseKmeansCoresetAlgorithm implements SparseCoresetAlgorithm {

    private static final long serialVersionUID = 6378586987151382195L;

    private int sampleSize;

    public SparseKmeansCoresetAlgorithm() {
        this(0);
    }

    public SparseKmeansCoresetAlgorithm(int sampleSize) {
        this.sampleSize = sampleSize;
    }

    @Override
    public List<SparseWeightableVector> takeSample(final List<SparseWeightableVector> pointset) {
        if (pointset.size() <= sampleSize)
            return pointset;

        final SparseWeightedKMeansPlusPlus kmeans = new SparseWeightedKMeansPlusPlus(sampleSize);
        final List<SparseCentroidCluster> clusters = kmeans.cluster(pointset);

        final List<SparseWeightableVector> results = new ArrayList<>();
        for (final SparseCentroidCluster cluster : clusters) {
            final RealVector center = cluster.getCenter().getVector();
            final int weight = cluster.getPoints().size();
            results.add(new SparseWeightableVector(center, weight));
        }

        return results;
    }
}
<commit_msg>Count total cluster weight rather than cluster size<commit_after>package univ.ml.sparse.algorithm;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.math3.linear.RealVector;

import univ.ml.sparse.SparseCentroidCluster;
import univ.ml.sparse.SparseWeightableVector;

public class SparseKmeansCoresetAlgorithm implements SparseCoresetAlgorithm {

    private static final long serialVersionUID = 6378586987151382195L;

    private int sampleSize;

    public SparseKmeansCoresetAlgorithm() {
        this(0);
    }

    public SparseKmeansCoresetAlgorithm(int sampleSize) {
        this.sampleSize = sampleSize;
    }

    @Override
    public List<SparseWeightableVector> takeSample(final List<SparseWeightableVector> pointset) {
        if (pointset.size() <= sampleSize)
            return pointset;

        final SparseWeightedKMeansPlusPlus kmeans = new SparseWeightedKMeansPlusPlus(sampleSize);
        final List<SparseCentroidCluster> clusters = kmeans.cluster(pointset);

        final List<SparseWeightableVector> results = new ArrayList<>();
        for (final SparseCentroidCluster cluster : clusters) {
            final RealVector center = cluster.getCenter().getVector();
            double weight = 0d;
            // Get cluster total weight
            for (SparseWeightableVector point : cluster.getPoints()) {
                weight += point.getWeight();
            }

            results.add(new SparseWeightableVector(center, weight));
        }

        return results;
    }
}

{'fraction_non_alphanumeric': 0.08055555555555556, 'fraction_numerical': 0.014930555555555556, 'mean_word_length': 3.385083713850837, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, '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': '7061259', 'n_tokens_mistral': 842, 'n_tokens_neox': 790, 'n_words': 170}