Repository: redpen-cc/redpen
Pull request #793 (2017-09-03 15:01:08)

Add threshold parameter to RedPen cli
Ref https://github.com/redpen-cc/redpen/issues/792

coveralls: 
[![Coverage Status](https://coveralls.io/builds/13106181/badge)](https://coveralls.io/builds/13106181)

Coverage increased (+0.02%) to 91.008% when pulling **1ba06d8189a9da1ea0e1af1d986cff6dbec8a307 on takahi-i:add-threshold-parameter** into **344edb72ae97e64eed530eccc74179d63dad2bb2 on redpen-cc:master**.

takahi-i: TODO: add test cases 

Patch:
diff --git a/redpen-cli/src/main/java/cc/redpen/Main.java b/redpen-cli/src/main/java/cc/redpen/Main.java
index b07f7f72..97694605 100644
--- a/redpen-cli/src/main/java/cc/redpen/Main.java
+++ b/redpen-cli/src/main/java/cc/redpen/Main.java
@@ -101,6 +101,12 @@ public final class Main {
                 .withArgName("LANGUAGE")
                 .create("L"));
 
+        options.addOption(OptionBuilder.withLongOpt("threshold")
+                .withDescription("Threshold of error level (info, warn, error)")
+                .hasArg()
+                .withArgName("THRESHOLD")
+                .create("t"));
+
         options.addOption(OptionBuilder.withLongOpt("version")
                 .withDescription("Displays version information and exits")
                 .create("v"));
@@ -120,6 +126,8 @@ public final class Main {
         String resultFormat = "plain";
         String inputSentence = null;
         String language = "en";
+        String threshold = "error";
+
         int limit = DEFAULT_LIMIT;
 
         if (commandLine.hasOption("h")) {
@@ -148,6 +156,9 @@ public final class Main {
         if (commandLine.hasOption("s")) {
             inputSentence = commandLine.getOptionValue("s");
         }
+        if (commandLine.hasOption("t")) {
+            threshold= commandLine.getOptionValue("t");
+        }
 
         // set language
         if (language.equals("ja")) {
@@ -183,7 +194,7 @@ public final class Main {
         }
 
         List<Document> documents = getDocuments(inputFormat, inputSentence, inputFileNames, redPen);
-        Map<Document, List<ValidationError>> documentListMap = redPen.validate(documents);
+        Map<Document, List<ValidationError>> documentListMap = redPen.validate(documents, threshold);
 
         Formatter formatter = FormatterUtils.getFormatterByName(resultFormat);
         if (formatter == null) {
diff --git a/redpen-core/src/main/java/cc/redpen/RedPen.java b/redpen-core/src/main/java/cc/redpen/RedPen.java
index bbf4edcd..6e4d1a1c 100644
--- a/redpen-core/src/main/java/cc/redpen/RedPen.java
+++ b/redpen-core/src/main/java/cc/redpen/RedPen.java
@@ -78,17 +78,21 @@ public class RedPen {
         this.validators = new ArrayList<>();
     }
 
-    private void initializeValidators() {
+    private void initializeValidators(String thresholdStr) {
+        ValidatorConfiguration.LEVEL threshold = ValidatorConfiguration.LEVEL.valueOf(thresholdStr.toUpperCase());
         validators.clear();
         for (ValidatorConfiguration config : configuration.getValidatorConfigs()) {
             try {
-                validators.add(ValidatorFactory.getInstance(config, configuration));
+                if (config.getLevel().isWorseThan(threshold)) {
+                    validators.add(ValidatorFactory.getInstance(config, configuration));
+                }
             } catch (RedPenException e) {
                 throw new IllegalStateException("Failed to initialize validators.", e);
             }
         }
     }
 
+
     /**
      * parse given input stream.
      *
@@ -136,9 +140,20 @@ public class RedPen {
      * @return list of validation errors
      */
     public Map<Document, List<ValidationError>> validate(List<Document> documents) {
+        return validate(documents, "error");
+    }
+
+    /**
+     * validate the input document collection. Note that this method call is NOT thread safe. RedPen instances need to be crated for each thread.
+     *
+     * @param documents input document collection generated by Parser
+     * @param threshold threshold of error level
+     * @return list of validation errors
+     */
+    public Map<Document, List<ValidationError>> validate(List<Document> documents, String threshold) {
         Map<Document, List<ValidationError>> docErrorsMap = new HashMap<>();
         documents.forEach(e -> docErrorsMap.put(e, new ArrayList<>()));
-        initializeValidators();
+        initializeValidators(threshold);
         runDocumentValidators(documents, docErrorsMap);
         runSectionValidators(documents, docErrorsMap);
         runSentenceValidators(documents, docErrorsMap);
diff --git a/redpen-core/src/main/java/cc/redpen/config/ValidatorConfiguration.java b/redpen-core/src/main/java/cc/redpen/config/ValidatorConfiguration.java
index 61a01efd..2e644cb4 100644
--- a/redpen-core/src/main/java/cc/redpen/config/ValidatorConfiguration.java
+++ b/redpen-core/src/main/java/cc/redpen/config/ValidatorConfiguration.java
@@ -30,7 +30,9 @@ public class ValidatorConfiguration implements Serializable, Cloneable {
      * Define how severe the validation errors are.
      */
     public enum LEVEL {
-        INFO, WARN, ERROR;
+        INFO(0),
+        WARN(1),
+        ERROR(2);
 
         Map<String, String> mapping = new HashMap<String, String>() {
             {
@@ -40,6 +42,16 @@ public class ValidatorConfiguration implements Serializable, Cloneable {
             }
         };
 
+        public boolean isWorseThan(LEVEL other) {
+            return this.severity >= other.severity;
+        }
+
+        private Integer severity;
+
+        LEVEL(int severity) {
+            this.severity = severity;
+        }
+
         @Override
         public String toString() {
             return mapping.get(name());


Test patch:
diff --git a/redpen-core/src/test/java/cc/redpen/config/ConfigurationLoaderTest.java b/redpen-core/src/test/java/cc/redpen/config/ConfigurationLoaderTest.java
index 53b4161c..e9895d6d 100644
--- a/redpen-core/src/test/java/cc/redpen/config/ConfigurationLoaderTest.java
+++ b/redpen-core/src/test/java/cc/redpen/config/ConfigurationLoaderTest.java
@@ -492,7 +492,7 @@ public class ConfigurationLoaderTest {
                         .build());
 
         RedPen redPen = new RedPen(config);
-        Map<Document, List<ValidationError>> errors = redPen.validate(documents);
+        Map<Document, List<ValidationError>> errors = redPen.validate(documents, "info");
         assertEquals(1, errors.get(documents.get(0)).size());
         assertEquals(ValidatorConfiguration.LEVEL.INFO, errors.get(documents.get(0)).get(0).getLevel());
     }
diff --git a/redpen-core/src/test/java/cc/redpen/formatter/JSONBySentenceFormatterTest.java b/redpen-core/src/test/java/cc/redpen/formatter/JSONBySentenceFormatterTest.java
index 7a415409..72fae716 100644
--- a/redpen-core/src/test/java/cc/redpen/formatter/JSONBySentenceFormatterTest.java
+++ b/redpen-core/src/test/java/cc/redpen/formatter/JSONBySentenceFormatterTest.java
@@ -99,7 +99,7 @@ public class JSONBySentenceFormatterTest extends Validator {
         documents.add(parser.parse(sampleText,
                 new SentenceExtractor(conf.getSymbolTable()), conf.getTokenizer()));
         RedPen redPen = new RedPen(configuration);
-        List<ValidationError> errors = redPen.validate(documents).get(documents.get(0));
+        List<ValidationError> errors = redPen.validate(documents, "info").get(documents.get(0));
 
         JSONFormatter formatter = new JSONBySentenceFormatter();
         String resultString = formatter.format(new cc.redpen.model.Document.DocumentBuilder(
diff --git a/redpen-core/src/test/java/cc/redpen/formatter/JSONFormatterTest.java b/redpen-core/src/test/java/cc/redpen/formatter/JSONFormatterTest.java
index 1f6fc5a7..ec62c3e2 100644
--- a/redpen-core/src/test/java/cc/redpen/formatter/JSONFormatterTest.java
+++ b/redpen-core/src/test/java/cc/redpen/formatter/JSONFormatterTest.java
@@ -131,7 +131,7 @@ public class JSONFormatterTest extends Validator {
         documents.add(parser.parse(sampleText,
                 new SentenceExtractor(conf.getSymbolTable()), conf.getTokenizer()));
         RedPen redPen = new RedPen(configuration);
-        List<ValidationError> errors = redPen.validate(documents).get(documents.get(0));
+        List<ValidationError> errors = redPen.validate(documents, "info").get(documents.get(0));
 
         JSONFormatter formatter = new JSONFormatter();
         String resultString = formatter.format(new cc.redpen.model.Document.DocumentBuilder(
diff --git a/redpen-core/src/test/java/cc/redpen/formatter/XMLFormatterTest.java b/redpen-core/src/test/java/cc/redpen/formatter/XMLFormatterTest.java
index 282f5964..64e9f2a6 100644
--- a/redpen-core/src/test/java/cc/redpen/formatter/XMLFormatterTest.java
+++ b/redpen-core/src/test/java/cc/redpen/formatter/XMLFormatterTest.java
@@ -154,7 +154,7 @@ public class XMLFormatterTest extends Validator {
         documents.add(parser.parse(sampleText,
                 new SentenceExtractor(conf.getSymbolTable()), conf.getTokenizer()));
         RedPen redPen = new RedPen(configuration);
-        List<ValidationError> errors = redPen.validate(documents).get(documents.get(0));
+        List<ValidationError> errors = redPen.validate(documents, "info").get(documents.get(0));
 
         XMLFormatter formatter = new XMLFormatter();
         String resultString = formatter.format(new cc.redpen.model.Document.DocumentBuilder(