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

Move PreprocessorHelper out of ClusteringPreprocessor

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

+ 0 - 33
jplag/src/main/java/de/jplag/clustering/ClusteringPreprocessor.java

@@ -1,11 +1,7 @@
 package de.jplag.clustering;
 
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.List;
-import java.util.stream.Collectors;
-import java.util.stream.DoubleStream;
 
 import org.apache.commons.math3.linear.Array2DRowRealMatrix;
 import org.apache.commons.math3.linear.RealMatrix;
@@ -23,35 +19,6 @@ public interface ClusteringPreprocessor {
 
     Collection<Collection<Integer>> postProcessResult(Collection<Collection<Integer>> result);
 
-    /**
-     * Implements removal of zero rows / columns. And calculation of original indices.
-     */
-    public static class PreprocessorHelper {
-        private IntegerMapping<Integer> mapping;
-
-        public double[][] removeDisconnectedEntries(double[][] connections) {
-            List<Integer> rowList = new ArrayList<>();
-            mapping = new IntegerMapping<>(connections.length);
-            RealMatrix similarity = new Array2DRowRealMatrix(connections, true);
-            for (int i = 0; i < similarity.getRowDimension(); i++) {
-                if (DoubleStream.of(similarity.getRow(i)).filter(x -> x > 0).findAny().isPresent()) {
-                    rowList.add(i);
-                    mapping.map(i);
-                }
-            }
-            int[] preservedRows = rowList.stream().mapToInt(Integer::intValue).toArray();
-            if (preservedRows.length == 0) {
-                return new double[0][];
-            }
-            similarity = similarity.getSubMatrix(preservedRows, preservedRows);
-            return similarity.getData();
-        }
-
-        public Collection<Collection<Integer>> postProcessResult(Collection<Collection<Integer>> result) {
-            return result.stream().map(cluster -> cluster.stream().map(mapping::unmap).collect(Collectors.toList())).collect(Collectors.toList());
-        }
-    }
-
     /**
      * Adapter class to put a preprocessor before any clustering algorithm.
      */

+ 50 - 0
jplag/src/main/java/de/jplag/clustering/PreprocessorHelper.java

@@ -0,0 +1,50 @@
+package de.jplag.clustering;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.DoubleStream;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.RealMatrix;
+
+/**
+ * Implements removal of zero rows / columns and calculation of original indices for classes implementing
+ * {@link ClusteringPreprocessor}.
+ */
+public class PreprocessorHelper {
+    private IntegerMapping<Integer> mapping;
+
+    /**
+     * Removes disconnected edges from the input matrix.
+     * @param connections similarity matrix
+     * @return similarity matrix without zero rows / cols
+     */
+    public double[][] removeDisconnectedEntries(double[][] connections) {
+        List<Integer> rowList = new ArrayList<>();
+        mapping = new IntegerMapping<>(connections.length);
+        RealMatrix similarity = new Array2DRowRealMatrix(connections, true);
+        for (int i = 0; i < similarity.getRowDimension(); i++) {
+            if (DoubleStream.of(similarity.getRow(i)).filter(x -> x > 0).findAny().isPresent()) {
+                rowList.add(i);
+                mapping.map(i);
+            }
+        }
+        int[] preservedRows = rowList.stream().mapToInt(Integer::intValue).toArray();
+        if (preservedRows.length == 0) {
+            return new double[0][];
+        }
+        similarity = similarity.getSubMatrix(preservedRows, preservedRows);
+        return similarity.getData();
+    }
+
+    /**
+     * Implements the logic for {@link ClusteringPreprocessor#postProcessResult(Collection)} 
+     * @param result clustering result gained from preprocessed similarities
+     * @return post processed clustering result
+     */
+    public Collection<Collection<Integer>> postProcessResult(Collection<Collection<Integer>> result) {
+        return result.stream().map(cluster -> cluster.stream().map(mapping::unmap).collect(Collectors.toList())).collect(Collectors.toList());
+    }
+}

+ 1 - 0
jplag/src/main/java/de/jplag/clustering/preprocessors/CumulativeDistributionFunctionPreprocessor.java

@@ -9,6 +9,7 @@ import org.apache.commons.math3.linear.RealMatrix;
 import org.apache.commons.math3.random.EmpiricalDistribution;
 
 import de.jplag.clustering.ClusteringPreprocessor;
+import de.jplag.clustering.PreprocessorHelper;
 
 /**
  * Multiplies entries of the similarity matrix by their cumulative probability. - Weights that are very low will be

+ 1 - 0
jplag/src/main/java/de/jplag/clustering/preprocessors/ThresholdPreprocessor.java

@@ -7,6 +7,7 @@ import org.apache.commons.math3.linear.DefaultRealMatrixChangingVisitor;
 import org.apache.commons.math3.linear.RealMatrix;
 
 import de.jplag.clustering.ClusteringPreprocessor;
+import de.jplag.clustering.PreprocessorHelper;
 
 /**
  * Suppresses all similarities below a given threshold.