|
|
@@ -1,10 +1,7 @@
|
|
|
package de.jplag.clustering;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
import java.util.Collection;
|
|
|
-import java.util.List;
|
|
|
import java.util.function.BiFunction;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
public interface ClusteringResult<T> {
|
|
|
|
|
|
@@ -27,100 +24,4 @@ public interface ClusteringResult<T> {
|
|
|
.getAsDouble();
|
|
|
}
|
|
|
|
|
|
- public static class Cluster<T> {
|
|
|
-
|
|
|
- private float communityStrength;
|
|
|
- private Collection<T> members;
|
|
|
- private ClusteringResult<T> clusteringResult;
|
|
|
-
|
|
|
- public Cluster(Collection<T> members, float communityStrength, ClusteringResult<T> clustering) {
|
|
|
- this.members = new ArrayList<>(members);
|
|
|
- this.communityStrength = communityStrength;
|
|
|
- this.clusteringResult = clustering;
|
|
|
- }
|
|
|
-
|
|
|
- public Collection<T> getMembers() {
|
|
|
- return members;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * See {@link ClusteringResult#getCommunityStrength}
|
|
|
- * @return community strength of the cluster
|
|
|
- */
|
|
|
- public float getCommunityStrength() {
|
|
|
- return communityStrength;
|
|
|
- }
|
|
|
-
|
|
|
- public ClusteringResult<T> getClusteringResult() {
|
|
|
- return clusteringResult;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @return How much each member of this cluster contributes to the {@link ClusteringResult#getCommunityStrength}
|
|
|
- */
|
|
|
- public float getCommunityStrengthPerConnection() {
|
|
|
- int size = getMembers().size();
|
|
|
- if (size < 2)
|
|
|
- return 0;
|
|
|
- return getCommunityStrength() / connections();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Computes a normalized community strength per connection. Can be used as measure for strength of evidence in
|
|
|
- * comparison to other clusters in the same clustering. Guaranteed to be smaller than 1. Negative values indicate
|
|
|
- * non-clusters.
|
|
|
- * @return normalized community strength per connection
|
|
|
- */
|
|
|
- public float getNormalizedCommunityStrengthPerConnection() {
|
|
|
- List<Cluster<T>> goodClusters = getClusteringResult().getClusters().stream().filter(cluster -> cluster.getCommunityStrength() > 0)
|
|
|
- .collect(Collectors.toList());
|
|
|
- float posCommunityStrengthSum = (float) goodClusters.stream().mapToDouble(Cluster::getCommunityStrengthPerConnection).sum();
|
|
|
-
|
|
|
- int size = getClusteringResult().size();
|
|
|
- if (size < 2)
|
|
|
- return getCommunityStrengthPerConnection();
|
|
|
- return getCommunityStrengthPerConnection() / posCommunityStrengthSum;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * How much this cluster is worth during optimization.
|
|
|
- */
|
|
|
- public double getWorth(BiFunction<T, T, Float> similarity) {
|
|
|
- double ncs = getCommunityStrength();
|
|
|
- if (getMembers().size() > 1) {
|
|
|
- ncs /= connections();
|
|
|
- }
|
|
|
- double avgSim = avgSimilarity(similarity);
|
|
|
- return ncs * avgSim;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Computes the average similarity inside the cluster.
|
|
|
- * @param similarity function that supplies the similarity of two cluster members.
|
|
|
- * @return average similarity
|
|
|
- */
|
|
|
- public float avgSimilarity(BiFunction<T, T, Float> similarity) {
|
|
|
- List<T> members = new ArrayList<>(getMembers());
|
|
|
- if (members.size() < 2) {
|
|
|
- return 1;
|
|
|
- }
|
|
|
- float similaritySum = 0;
|
|
|
- for (int i = 0; i < members.size(); i++) {
|
|
|
- for (int j = i + 1; j < members.size(); j++) {
|
|
|
- similaritySum += similarity.apply(members.get(i), members.get(j));
|
|
|
- }
|
|
|
- }
|
|
|
- return similaritySum / connections();
|
|
|
- }
|
|
|
-
|
|
|
- public int connections() {
|
|
|
- int size = getMembers().size();
|
|
|
- return ((size - 1) * size) / 2;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean isBadCluster() {
|
|
|
- return getMembers().size() < 2 || getCommunityStrength() < 0;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
}
|