{
// Prepare a centroid. If dimensionality reduction was used,
// the centroid from k-means will not be based on real terms,
// so we need to calculate the centroid here once again based
// on the cluster's documents.
final DoubleMatrix1D centroid = new DenseDoubleMatrix1D(termDocumentMatrix.rows());
for (IntCursor d : documents)
{
centroid.assign(termDocumentMatrix.viewColumn(d.value), Functions.PLUS);
}
final List<String> labels = Lists.newArrayListWithCapacity(labelCount);
final int [] order = IndirectSort.mergesort(0, centroid.size(),
new IndirectComparator()
{
@Override
public int compare(int a, int b)
{
final double valueA = centroid.get(a);
final double valueB = centroid.get(b);
return valueA < valueB ? -1 : valueA > valueB ? 1 : 0;
}
});
final double minValueForLabel = centroid.get(order[order.length
- Math.min(labelCount, order.length)]);
for (int i = 0; i < centroid.size(); i++)
{
if (centroid.getQuick(i) >= minValueForLabel)
{
labels.add(LabelFormatter.format(new char [] []
{
wordImage[mostFrequentOriginalWordIndex[rowToStemIndex.get(i)]]
}, new boolean []