Repository: redpen-cc/redpen
Pull request #559 (2016-03-04 09:47:45)

Better defaults for some validators


Patch:
diff --git a/redpen-core/src/main/java/cc/redpen/model/Paragraph.java b/redpen-core/src/main/java/cc/redpen/model/Paragraph.java
index 417c1af0..87a75257 100644
--- a/redpen-core/src/main/java/cc/redpen/model/Paragraph.java
+++ b/redpen-core/src/main/java/cc/redpen/model/Paragraph.java
@@ -59,8 +59,9 @@ public class Paragraph {
      * @param content sentence
      * @param lineNum line number of sentence
      */
-    public void appendSentence(String content, int lineNum) {
+    public Paragraph appendSentence(String content, int lineNum) {
         sentences.add(new Sentence(content, lineNum));
+        return this;
     }
 
     /**
@@ -68,8 +69,9 @@ public class Paragraph {
      *
      * @param sentence a sentence to be added to paragraph
      */
-    public void appendSentence(Sentence sentence) {
+    public Paragraph appendSentence(Sentence sentence) {
         sentences.add(sentence);
+        return this;
     }
 
     /**
diff --git a/redpen-core/src/main/java/cc/redpen/validator/Validator.java b/redpen-core/src/main/java/cc/redpen/validator/Validator.java
index ad1bc32a..798dfc6a 100644
--- a/redpen-core/src/main/java/cc/redpen/validator/Validator.java
+++ b/redpen-core/src/main/java/cc/redpen/validator/Validator.java
@@ -113,7 +113,7 @@ public abstract class Validator {
         return Collections.emptyList();
     }
 
-    final void preInit(ValidatorConfiguration config, Configuration globalConfig) throws RedPenException {
+    public final void preInit(ValidatorConfiguration config, Configuration globalConfig) throws RedPenException {
         this.config = config;
         this.globalConfig = globalConfig;
         init();
diff --git a/redpen-core/src/main/java/cc/redpen/validator/section/ParagraphStartWithValidator.java b/redpen-core/src/main/java/cc/redpen/validator/section/ParagraphStartWithValidator.java
index 8fd0cf65..6aca69b5 100644
--- a/redpen-core/src/main/java/cc/redpen/validator/section/ParagraphStartWithValidator.java
+++ b/redpen-core/src/main/java/cc/redpen/validator/section/ParagraphStartWithValidator.java
@@ -26,12 +26,12 @@ import cc.redpen.validator.Validator;
 /**
  * Validate whether paragraph start as specified.
  */
-final public class ParagraphStartWithValidator extends Validator {
+public final class ParagraphStartWithValidator extends Validator {
     /**
      * Default matter paragraph start with.
      */
     @SuppressWarnings("WeakerAccess")
-    public static final String DEFAULT_PARAGRAPH_START_WITH = " ";
+    public static final String DEFAULT_PARAGRAPH_START_WITH = "";
     private String beginningOfParagraph = DEFAULT_PARAGRAPH_START_WITH;
 
     @Override
diff --git a/redpen-core/src/main/java/cc/redpen/validator/sentence/SpaceBetweenAlphabeticalWordValidator.java b/redpen-core/src/main/java/cc/redpen/validator/sentence/SpaceBetweenAlphabeticalWordValidator.java
index af61e5a4..dfcb19b5 100644
--- a/redpen-core/src/main/java/cc/redpen/validator/sentence/SpaceBetweenAlphabeticalWordValidator.java
+++ b/redpen-core/src/main/java/cc/redpen/validator/sentence/SpaceBetweenAlphabeticalWordValidator.java
@@ -22,13 +22,21 @@ import cc.redpen.model.Sentence;
 import cc.redpen.util.StringUtils;
 import cc.redpen.validator.Validator;
 
+import java.util.List;
+import java.util.Locale;
+
 import static cc.redpen.config.SymbolType.*;
+import static java.util.Collections.singletonList;
 
 public class SpaceBetweenAlphabeticalWordValidator extends Validator {
     private char leftParenthesis = '(';
     private char rightParenthesis = ')';
     private char comma = ',';
 
+    @Override public List<String> getSupportedLanguages() {
+        return singletonList(Locale.JAPANESE.getLanguage());
+    }
+
     @Override
     public void validate(Sentence sentence) {
         char prevCharacter = ' ';
diff --git a/redpen-core/src/main/java/cc/redpen/validator/sentence/SuggestExpressionValidator.java b/redpen-core/src/main/java/cc/redpen/validator/sentence/SuggestExpressionValidator.java
index e3883e08..874fd534 100644
--- a/redpen-core/src/main/java/cc/redpen/validator/sentence/SuggestExpressionValidator.java
+++ b/redpen-core/src/main/java/cc/redpen/validator/sentence/SuggestExpressionValidator.java
@@ -32,38 +32,31 @@ import java.util.Optional;
  * returns the errors with corrected expressions.
  */
 final public class SuggestExpressionValidator extends Validator {
-
-    private static final Logger LOG =
-            LoggerFactory.getLogger(SuggestExpressionValidator.class);
+    private static final Logger LOG = LoggerFactory.getLogger(SuggestExpressionValidator.class);
     private Map<String, String> synonyms = new HashMap<>();
 
     @Override
     public void validate(Sentence sentence) {
         synonyms.keySet().stream().forEach(value -> {
-                    int startPosition = sentence.getContent().indexOf(value);
-                    if (startPosition != -1) {
-                        final String word = value;
-                        final String suggested = synonyms.get(value);
-                        addLocalizedErrorWithPosition(sentence,
-                                startPosition,
-                                startPosition + value.length(),
-                                word, suggested);
-                    }
-                }
-        );
+            int startPosition = sentence.getContent().indexOf(value);
+            if (startPosition != -1) {
+                String suggested = synonyms.get(value);
+                addLocalizedErrorWithPosition(sentence, startPosition, startPosition + value.length(), value, suggested);
+            }
+        });
     }
 
     @Override
     protected void init() throws RedPenException {
         //TODO: support default dictionary.
         Optional<String> confFile = getConfigAttribute("dict");
-        LOG.info("Dictionary file is " + confFile);
-        if (!confFile.isPresent()) {
-            LOG.error("Dictionary file is not specified");
-            throw new RedPenException("dictionary file is not specified");
-        } else {
+        if (confFile.isPresent()) {
+            LOG.info("Dictionary file is " + confFile);
             synonyms = KEY_VALUE.loadCachedFromFile(findFile(confFile.get()), "SuggestExpressionValidator dictionary");
         }
+        else {
+            LOG.warn("Dictionary file is not specified");
+        }
     }
 
     protected void setSynonyms(Map<String, String> synonymMap) {


Test patch:
diff --git a/redpen-core/src/test/java/cc/redpen/validator/section/ParagraphStartWithValidatorTest.java b/redpen-core/src/test/java/cc/redpen/validator/section/ParagraphStartWithValidatorTest.java
index 0a80a0f5..909da992 100644
--- a/redpen-core/src/test/java/cc/redpen/validator/section/ParagraphStartWithValidatorTest.java
+++ b/redpen-core/src/test/java/cc/redpen/validator/section/ParagraphStartWithValidatorTest.java
@@ -17,50 +17,49 @@
  */
 package cc.redpen.validator.section;
 
+import cc.redpen.config.Configuration;
+import cc.redpen.config.ValidatorConfiguration;
 import cc.redpen.model.Paragraph;
 import cc.redpen.model.Section;
 import cc.redpen.validator.ValidationError;
+import org.junit.Before;
 import org.junit.Test;
 
 import java.util.ArrayList;
 import java.util.List;
 
+import static java.util.Collections.singletonMap;
 import static org.junit.Assert.assertEquals;
 
 public class ParagraphStartWithValidatorTest {
+    ParagraphStartWithValidator validator = new ParagraphStartWithValidator();
+
+    @Before
+    public void setUp() throws Exception {
+        validator.preInit(new ValidatorConfiguration("ParagraphStartWith").addAttribute("start_from", " "), Configuration.builder().build());
+    }
 
     @Test
-    public void testStartWithoutSpace() {
-        ParagraphStartWithValidator validator = new ParagraphStartWithValidator();
-        Section section = new Section(0, "header");
-        Paragraph paragraph = new Paragraph();
-        paragraph.appendSentence("it like a piece of a cake.", 0);
-        section.appendParagraph(paragraph);
-        List<ValidationError> errors = new ArrayList<>();
-       validator.setErrorList(errors);         validator.validate(section);
-        assertEquals(1, errors.size());
+    public void startWithoutSpace() {
+        assertEquals(1, validateParagraph(new Paragraph().appendSentence("it like a piece of a cake.", 1)).size());
     }
 
     @Test
-    public void testStartWithSpace() {
-        ParagraphStartWithValidator validator = new ParagraphStartWithValidator();
-        Section section = new Section(0);
-        Paragraph paragraph = new Paragraph();
-        paragraph.appendSentence(" it like a piece of a cake.", 0);
-        section.appendParagraph(paragraph);
-        List<ValidationError> errors = new ArrayList<>();
-       validator.setErrorList(errors);         validator.validate(section);
-        assertEquals(0, errors.size());
+    public void startWithSpace() {
+        assertEquals(0, validateParagraph(new Paragraph().appendSentence(" it like a piece of a cake.", 1)).size());
     }
 
     @Test
-    public void testVoidParagraph() {
-        ParagraphStartWithValidator validator = new ParagraphStartWithValidator();
+    public void voidParagraph() {
+        assertEquals(0, validateParagraph(new Paragraph()).size());
+    }
+
+    private List<ValidationError> validateParagraph(Paragraph paragraph) {
         Section section = new Section(0);
-        Paragraph paragraph = new Paragraph();
         section.appendParagraph(paragraph);
         List<ValidationError> errors = new ArrayList<>();
-       validator.setErrorList(errors);         validator.validate(section);
-        assertEquals(0, errors.size());
+        validator.setErrorList(errors);
+        validator.validate(section);
+        return errors;
     }
 }
diff --git a/redpen-core/src/test/java/cc/redpen/validator/sentence/SuggestExpressionValidatorTest.java b/redpen-core/src/test/java/cc/redpen/validator/sentence/SuggestExpressionValidatorTest.java
index edf67eb5..8ed60c86 100644
--- a/redpen-core/src/test/java/cc/redpen/validator/sentence/SuggestExpressionValidatorTest.java
+++ b/redpen-core/src/test/java/cc/redpen/validator/sentence/SuggestExpressionValidatorTest.java
@@ -17,6 +17,8 @@
  */
 package cc.redpen.validator.sentence;
 
+import cc.redpen.config.Configuration;
+import cc.redpen.config.ValidatorConfiguration;
 import cc.redpen.model.Sentence;
 import cc.redpen.validator.ValidationError;
 import org.junit.Before;
@@ -89,5 +91,8 @@ public class SuggestExpressionValidatorTest {
         assertEquals("Found invalid word \"info\". Use the synonym \"information\" instead.", errors.get(0).getMessage());
     }
 
-
+    @Test
+    public void initDoesNotFailIfDictionaryIsNotSpecified() throws Exception {
+        suggestExpressionValidator.preInit(new ValidatorConfiguration("SuggestExpression"), Configuration.builder().build());
+    }
 }