Forráskód Böngészése

Move nested class cluster out of ClusteringResult

Simon Ding 4 éve
szülő
commit
d9ae5c7069

+ 103 - 0
jplag/src/main/java/de/jplag/clustering/Cluster.java

@@ -0,0 +1,103 @@
+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 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;
+    }
+}

+ 5 - 5
jplag/src/main/java/de/jplag/clustering/ClusteringAdapter.java

@@ -24,8 +24,8 @@ public class ClusteringAdapter {
     private IntegerMapping<Submission> mapping;
 
     /**
-     * Creates the clustering adapter. Only submissions that appear in those similarities might also appear in {@link ClusteringResult}s obtained from this adapter.
-     *  
+     * Creates the clustering adapter. Only submissions that appear in those similarities might also appear in
+     * {@link ClusteringResult}s obtained from this adapter.
      * @param comparisons that should be included in the process of clustering
      * @param metric function that assigns a similarity to each comparison
      */
@@ -48,8 +48,8 @@ public class ClusteringAdapter {
     }
 
     /**
-     * Use a generic clustering algorithm to cluster the submissions, that were included in this {@link ClusteringAdapter}'s comparison.
-     * 
+     * Use a generic clustering algorithm to cluster the submissions, that were included in this {@link ClusteringAdapter}'s
+     * comparison.
      * @param algorithm that is used for clustering
      * @return the clustered submissions
      */
@@ -68,7 +68,7 @@ public class ClusteringAdapter {
         public MappedClusteringResult(ClusteringResult<M> unmapped, Function<M, T> mapping) {
             communityStrength = unmapped.getCommunityStrength();
             clusters = unmapped.getClusters().stream().map(unmappedCluster -> {
-                return new ClusteringResult.Cluster<T>(unmappedCluster.getMembers().stream().map(mapping).collect(Collectors.toList()),
+                return new Cluster<T>(unmappedCluster.getMembers().stream().map(mapping).collect(Collectors.toList()),
                         unmappedCluster.getCommunityStrength(), this);
             }).collect(Collectors.toList());
             size = unmapped.size();

+ 0 - 99
jplag/src/main/java/de/jplag/clustering/ClusteringResult.java

@@ -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;
-        }
-    }
-
 }

+ 0 - 1
jplag/src/test/java/de/jplag/clustering/ClusteringTest.java

@@ -28,7 +28,6 @@ import org.mockito.invocation.InvocationOnMock;
 
 import de.jplag.JPlagComparison;
 import de.jplag.Submission;
-import de.jplag.clustering.ClusteringResult.Cluster;
 import de.jplag.clustering.algorithm.GenericClusteringAlgorithm;
 import de.jplag.clustering.algorithm.SpectralClustering;
 import de.jplag.clustering.preprocessors.CdfPreprocessor;