private void findClusters(List<FastIDSet> newClusters) throws TasteException {
if (clusteringByThreshold) {
Pair<FastIDSet,FastIDSet> nearestPair = findNearestClusters(newClusters);
if (nearestPair != null) {
FastIDSet cluster1 = nearestPair.getFirst();
FastIDSet cluster2 = nearestPair.getSecond();
while (clusterSimilarity.getSimilarity(cluster1, cluster2) >= clusteringThreshold) {
newClusters.remove(cluster1);
newClusters.remove(cluster2);
FastIDSet merged = new FastIDSet(cluster1.size() + cluster2.size());
merged.addAll(cluster1);
merged.addAll(cluster2);
newClusters.add(merged);
nearestPair = findNearestClusters(newClusters);
if (nearestPair == null) {
break;
}
cluster1 = nearestPair.getFirst();
cluster2 = nearestPair.getSecond();
}
}
} else {
while (newClusters.size() > numClusters) {
Pair<FastIDSet,FastIDSet> nearestPair = findNearestClusters(newClusters);
if (nearestPair == null) {
break;
}
FastIDSet cluster1 = nearestPair.getFirst();
FastIDSet cluster2 = nearestPair.getSecond();
newClusters.remove(cluster1);
newClusters.remove(cluster2);
FastIDSet merged = new FastIDSet(cluster1.size() + cluster2.size());
merged.addAll(cluster1);
merged.addAll(cluster2);
newClusters.add(merged);
}
}
}