Просмотр исходного кода

Moved PreprocessedClusteringAlgorithm out of ClusteringPreprocessor

Simon Ding 4 лет назад
Родитель
Сommit
85aaaa6a4d

+ 1 - 1
jplag/src/main/java/de/jplag/clustering/ClusteringFactory.java

@@ -49,7 +49,7 @@ public class ClusteringFactory {
         }
         if (preprocessor != null) {
             // Package preprocessor into a clustering algorithm
-            clusteringAlgorithm = new ClusteringPreprocessor.PreprocessedClusteringAlgorithm(clusteringAlgorithm, preprocessor);
+            clusteringAlgorithm = new PreprocessedClusteringAlgorithm(clusteringAlgorithm, preprocessor);
         }
 
         // init adapter

+ 10 - 31
jplag/src/main/java/de/jplag/clustering/ClusteringPreprocessor.java

@@ -1,12 +1,6 @@
 package de.jplag.clustering;
 
 import java.util.Collection;
-import java.util.Collections;
-
-import org.apache.commons.math3.linear.Array2DRowRealMatrix;
-import org.apache.commons.math3.linear.RealMatrix;
-
-import de.jplag.clustering.algorithm.GenericClusteringAlgorithm;
 
 /**
  * Interface for classes that process similarity matrices before any clustering. Classes implementing this interface
@@ -15,32 +9,17 @@ import de.jplag.clustering.algorithm.GenericClusteringAlgorithm;
  * {@link ClusteringPreprocessor#postProcessResult} method.
  */
 public interface ClusteringPreprocessor {
+    /**
+     * Applies some preprocessing defined by the implementing class.
+     * @param similarityMatrix original similarities
+     * @return preprocessed similarities
+     */
     double[][] preprocessSimilarities(double[][] similarityMatrix);
 
-    Collection<Collection<Integer>> postProcessResult(Collection<Collection<Integer>> result);
-
     /**
-     * Adapter class to put a preprocessor before any clustering algorithm.
+     * Change the indices contained in the clustering result to the indices used before preprocessing.
+     * @param result from clustering with preprocessed similarities
+     * @return the same result but with the indices from the original similarity matrix
      */
-    public static class PreprocessedClusteringAlgorithm implements GenericClusteringAlgorithm {
-
-        private final GenericClusteringAlgorithm base;
-        private final ClusteringPreprocessor preprocessor;
-
-        public PreprocessedClusteringAlgorithm(GenericClusteringAlgorithm base, ClusteringPreprocessor preprocessor) {
-            this.base = base;
-            this.preprocessor = preprocessor;
-        }
-
-        @Override
-        public Collection<Collection<Integer>> cluster(RealMatrix similarityMatrix) {
-            double[][] data = preprocessor.preprocessSimilarities(similarityMatrix.getData());
-            if (data.length > 2) {
-                Collection<Collection<Integer>> preliminaryResult = base.cluster(new Array2DRowRealMatrix(data, false));
-                return preprocessor.postProcessResult(preliminaryResult);
-            }
-            return Collections.emptyList();
-        }
-
-    }
-}
+    Collection<Collection<Integer>> postProcessResult(Collection<Collection<Integer>> result);
+}

+ 34 - 0
jplag/src/main/java/de/jplag/clustering/PreprocessedClusteringAlgorithm.java

@@ -0,0 +1,34 @@
+package de.jplag.clustering;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.RealMatrix;
+
+import de.jplag.clustering.algorithm.GenericClusteringAlgorithm;
+
+/**
+ * Adapter class to put a preprocessor before any clustering algorithm.
+ */
+public class PreprocessedClusteringAlgorithm implements GenericClusteringAlgorithm {
+
+    private final GenericClusteringAlgorithm base;
+    private final ClusteringPreprocessor preprocessor;
+
+    public PreprocessedClusteringAlgorithm(GenericClusteringAlgorithm base, ClusteringPreprocessor preprocessor) {
+        this.base = base;
+        this.preprocessor = preprocessor;
+    }
+
+    @Override
+    public Collection<Collection<Integer>> cluster(RealMatrix similarityMatrix) {
+        double[][] data = preprocessor.preprocessSimilarities(similarityMatrix.getData());
+        if (data.length > 2) {
+            Collection<Collection<Integer>> preliminaryResult = base.cluster(new Array2DRowRealMatrix(data, false));
+            return preprocessor.postProcessResult(preliminaryResult);
+        }
+        return Collections.emptyList();
+    }
+
+}

+ 1 - 1
jplag/src/main/java/de/jplag/clustering/PreprocessorHelper.java

@@ -40,7 +40,7 @@ public class PreprocessorHelper {
     }
 
     /**
-     * Implements the logic for {@link ClusteringPreprocessor#postProcessResult(Collection)} 
+     * Implements the logic for {@link ClusteringPreprocessor#postProcessResult(Collection)}
      * @param result clustering result gained from preprocessed similarities
      * @return post processed clustering result
      */