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

Close #165. Close #167. Adds -n CLI flag to limit the number of saved matches.

* Close #165 and touches #167. Adds -n CLI flag to limit the maximum number of displayed matches.

* Close #165 and touches #167. Adds -n CLI flag to limit the maximum number of displayed matches.

* Replace default value.
Sebastian Hahner 5 лет назад
Родитель
Сommit
d2b707e2cd

+ 2 - 0
README.md

@@ -70,6 +70,8 @@ named arguments:
                          <n> increases the sensitivity
   -m M                   Match similarity  Threshold  [0-100]:  All matches
                          above this threshold will be saved (Standard: 0.0)
+  -n N                   Maximum number of matches that will be saved. If 
+                         set to -1 all matches will be saved (Standard: 30)
   -r R                   Name of directory in which  the  web pages will be
                          stored (default: result)
 ```

+ 13 - 2
jplag/src/main/java/jplag/CLI.java

@@ -69,7 +69,8 @@ public class CLI {
         parser.addArgument("-p").help("comma-separated list of all filename suffixes that are included");
         parser.addArgument("-x").help("All files named in this file will be ignored in the comparison (line-separated list)");
         parser.addArgument("-t").help("Tune the sensitivity of the comparison. A smaller <n> increases the sensitivity");
-        parser.addArgument("-m").setDefault(0f).help("Match similarity Threshold [0-100]: All matches above this threshold will be saved");
+        parser.addArgument("-m").setDefault(0f).help("Match similarity threshold [0-100]: All matches above this threshold will be saved");
+        parser.addArgument("-n").setDefault(30).help("Maximum number of matches that will be saved. If set to -1 all matches will be saved");
         parser.addArgument("-r").setDefault("result").help("Name of directory in which the comparison results will be stored");
     }
 
@@ -127,7 +128,17 @@ public class CLI {
                 options.setSimilarityThreshold(Float.parseFloat(similarityThreshold));
             } catch (NumberFormatException e) {
                 System.out.println("Illegal similarity threshold. Taking 0 as default value.");
-                options.setSimilarityThreshold(0);
+                options.setSimilarityThreshold(0); // TODO SH: Remove code duplication
+            }
+        }
+
+        String maxNumberOfMatches = namespace.getString("n");
+        if(maxNumberOfMatches != null) {
+            try {
+                options.setMaxNumberOfMatches(Integer.parseInt(maxNumberOfMatches));
+            } catch (NumberFormatException e) {
+                System.out.println("Illegal maximum number of matches. Taking 30 as default value.");
+                options.setMaxNumberOfMatches(30); // TODO SH: Remove code duplication
             }
         }
 

+ 32 - 1
jplag/src/main/java/jplag/JPlagResult.java

@@ -68,11 +68,42 @@ public class JPlagResult {
         return similarityDistribution;
     }
 
+    /**
+     * @return a list of all comparisons sorted by percentage (descending).
+     */
+    public List<JPlagComparison> getAllComparisons() {
+        return comparisons;
+    }
+
     /**
      * @return a list of comparisons sorted by percentage (descending).
+     * the maximum number of comparisons is limited by the settings set in JPlagOptions
      */
     public List<JPlagComparison> getComparisons() {
-        return comparisons;
+        if(this.options.getMaxNumberOfMatches() == -1) {
+            return this.getAllComparisons();
+        } else {
+            return this.getComparisons(this.options.getMaxNumberOfMatches());
+        }
+    }
+
+    /**
+     * Returns the first n comparisons (sorted by percentage, descending), limited by the specified parameter.
+     * @param maxCount the maximum number of yield comparisons
+     * @return a list of comparisons with a size of maxCount or less
+     */
+    public List<JPlagComparison> getComparisons(int maxCount) {
+        return comparisons.subList(0, Math.min(maxCount, comparisons.size()));
+    }
+
+    /**
+     * Drops elements from the comparison list to free memory. Note, that this affects the similarity distribution
+     * and is only meant to be used if you don't need the information about comparisons with lower match percentage
+     * anymore.
+     * @param limit the number of comparisons to keep in the list
+     */
+    public void dropComparisons(int limit) {
+        this.comparisons = this.getComparisons(limit);
     }
 
     public long getDuration() {

+ 18 - 35
jplag/src/main/java/jplag/options/JPlagOptions.java

@@ -12,21 +12,6 @@ public class JPlagOptions {
      */
     private Language language;
 
-    /**
-     * Deprecated - use similarityThreshold instead! Maximum number of comparisons to store per run.
-     */
-    @Deprecated
-    private int storeMatches = 30;
-
-    /**
-     * Deprecated - use similarityThreshold instead!
-     * <p>
-     * True, if `storeMatches` should be interpreted as a percentage threshold for the similarity of a comparison; false
-     * otherwise;
-     */
-    @Deprecated
-    private boolean storePercent = false;
-
     /**
      * TODO PB: Decide what to do with this.
      * <p>
@@ -59,6 +44,12 @@ public class JPlagOptions {
      */
     private float similarityThreshold = 0;
 
+    /**
+     * The maximum number of matches that will be saved. This does affect the generated report as well as the internally
+     * saved comparisons. If set to -1 all matches will be saved.
+     */
+    private int maxNumberOfMatches = 30;
+
     /**
      * TODO PB: Not happy with the name yet.
      */
@@ -151,16 +142,6 @@ public class JPlagOptions {
         return fileSuffixes;
     }
 
-    @Deprecated
-    public int getStoreMatches() {
-        return storeMatches;
-    }
-
-    @Deprecated
-    public boolean isStorePercent() {
-        return storePercent;
-    }
-
     public Language getLanguage() {
         return language;
     }
@@ -197,6 +178,10 @@ public class JPlagOptions {
         return similarityThreshold;
     }
 
+    public int getMaxNumberOfMatches() {
+        return this.maxNumberOfMatches;
+    }
+
     public SimilarityMetric getSimilarityMetric() {
         return similarityMetric;
     }
@@ -221,16 +206,6 @@ public class JPlagOptions {
         this.fileSuffixes = fileSuffixes;
     }
 
-    @Deprecated
-    public void setStoreMatches(int storeMatches) {
-        this.storeMatches = storeMatches;
-    }
-
-    @Deprecated
-    public void setStorePercent(boolean storePercent) {
-        this.storePercent = storePercent;
-    }
-
     public void setMinTokenMatch(Integer minTokenMatch) {
         this.minTokenMatch = minTokenMatch;
     }
@@ -269,6 +244,14 @@ public class JPlagOptions {
         }
     }
 
+    public void setMaxNumberOfMatches(int maxNumberOfMatches) {
+        if(maxNumberOfMatches < -1) {
+            this.maxNumberOfMatches = -1;
+        } else {
+            this.maxNumberOfMatches = maxNumberOfMatches;
+        }
+    }
+
     public void setSimilarityMetric(SimilarityMetric similarityMetric) {
         this.similarityMetric = similarityMetric;
     }

+ 2 - 1
jplag/src/main/java/jplag/reporting/Report.java

@@ -445,7 +445,8 @@ public class Report {
         if (result.getComparisons().size() > 0) {
             htmlFile.println("<TR BGCOLOR=#aaaaff VALIGN=top><TD>" + msg.getString("Report.Matches_displayed") + ":</TD>" + "<TD>");
 
-            htmlFile.println(result.getComparisons().size() + " (" + msg.getString("Report.Treshold") + ": "
+            htmlFile.println(result.getComparisons().size() + " of " + result.getAllComparisons().size()
+                    + " (" + msg.getString("Report.Treshold") + ": "
                     + result.getOptions().getSimilarityThreshold() + "%)<br>");
 
             htmlFile.println("</TD></TR>");