Repository: redpen-cc/redpen
Pull request #751 (2017-02-25 06:50:02)

Rename voidsection
Ref #747 


Patch:
diff --git a/redpen-cli/sample/conf/redpen-conf-en.xml b/redpen-cli/sample/conf/redpen-conf-en.xml
index 04ea8aa3..e604d77c 100644
--- a/redpen-cli/sample/conf/redpen-conf-en.xml
+++ b/redpen-cli/sample/conf/redpen-conf-en.xml
@@ -22,7 +22,7 @@
         <validator name="ParagraphNumber"/>
         <validator name="SpaceBeginningOfSentence" />
         <validator name="ParenthesizedSentence"/>
-        <validator name="VoidSection" />
+        <validator name="EmptySection" />
         <validator name="GappedSection" />
         <validator name="SectionLevel" />
 
diff --git a/redpen-cli/sample/conf/redpen-conf-ja.xml b/redpen-cli/sample/conf/redpen-conf-ja.xml
index e4192c9e..5fad9398 100644
--- a/redpen-cli/sample/conf/redpen-conf-ja.xml
+++ b/redpen-cli/sample/conf/redpen-conf-ja.xml
@@ -33,7 +33,7 @@
         <validator name="SectionLength">
             <property name="max_num" value="1500"/>
         </validator>
-        <validator name="VoidSection" />
+        <validator name="EmptySection" />
         <validator name="GappedSection" />
         <validator name="SectionLevel" />
         <validator name="ParagraphNumber"/>
diff --git a/redpen-core/src/main/java/cc/redpen/validator/section/EmptySectionValidator.java b/redpen-core/src/main/java/cc/redpen/validator/section/EmptySectionValidator.java
new file mode 100644
index 00000000..07e3427a
--- /dev/null
+++ b/redpen-core/src/main/java/cc/redpen/validator/section/EmptySectionValidator.java
@@ -0,0 +1,60 @@
+package cc.redpen.validator.section;
+
+import cc.redpen.RedPenException;
+import cc.redpen.model.Paragraph;
+import cc.redpen.model.Section;
+import cc.redpen.validator.Validator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Optional;
+/**
+ * redpen: a text inspection tool
+ * Copyright (c) 2014-2015 Recruit Technologies Co., Ltd. and contributors
+ * (see CONTRIBUTORS.md)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class EmptySectionValidator extends Validator {
+    private static final Logger LOG = LoggerFactory.getLogger(EmptySectionValidator.class);
+    private int sectionLevelLimit = 5;
+
+    @Override
+    public void validate(Section section) {
+        if (section.getLevel() >= sectionLevelLimit) {
+            return;
+        }
+
+        if (section.getLevel() == 0) {
+            return; // hot fix for auto generated level 0 sections.
+        }
+        if (section.getNumberOfParagraphs() == 0) {
+            addLocalizedError(section.getJoinedHeaderContents());
+        } else {
+            for (Paragraph p : section.getParagraphs()) {
+                if (p.getNumberOfSentences() == 0) {
+                    addLocalizedError(section.getJoinedHeaderContents());
+                    break;
+                }
+            }
+        }
+    }
+
+    @Override
+    protected void init() throws RedPenException {
+        Optional<String> limit = getConfigAttribute("limit");
+        if (limit.isPresent()) {
+            sectionLevelLimit = Integer.parseInt(limit.get());
+        }
+    }
+}
diff --git a/redpen-core/src/main/java/cc/redpen/validator/section/VoidSectionValidator.java b/redpen-core/src/main/java/cc/redpen/validator/section/VoidSectionValidator.java
index 4227405d..e9dcf170 100644
--- a/redpen-core/src/main/java/cc/redpen/validator/section/VoidSectionValidator.java
+++ b/redpen-core/src/main/java/cc/redpen/validator/section/VoidSectionValidator.java
@@ -29,6 +29,7 @@ import java.util.Optional;
 /**
  * VoidSectionValidator detects sections with no content..
  */
+@Deprecated
 final public class VoidSectionValidator extends Validator {
     private static final Logger LOG = LoggerFactory.getLogger(VoidSectionValidator.class);
     private int sectionLevelLimit = 5;
diff --git a/redpen-core/src/main/resources/cc/redpen/validator/section/error-messages.properties b/redpen-core/src/main/resources/cc/redpen/validator/section/error-messages.properties
index d8c3eb09..8271e886 100644
--- a/redpen-core/src/main/resources/cc/redpen/validator/section/error-messages.properties
+++ b/redpen-core/src/main/resources/cc/redpen/validator/section/error-messages.properties
@@ -7,4 +7,5 @@ FrequentSentenceStartValidator.SentenceStartTooFrequent={0}% of sentences start
 WordFrequencyValidator.WordUsedTooFrequently=The word "{0}" comprises {1}% of all words. Expected around {2}%.
 SectionLevelValidator=The section "{0}" is too deep.
 VoidSectionValidator=The section is void.
+EmptySectionValidator=The section "{0}" is empty.
 GappedSectionValidator=The section "{0}" (level {1}) is misplaced. It should have level of {2}.
diff --git a/redpen-core/src/main/resources/cc/redpen/validator/section/error-messages_ja.properties b/redpen-core/src/main/resources/cc/redpen/validator/section/error-messages_ja.properties
index a459f5cf..afc10edc 100644
--- a/redpen-core/src/main/resources/cc/redpen/validator/section/error-messages_ja.properties
+++ b/redpen-core/src/main/resources/cc/redpen/validator/section/error-messages_ja.properties
@@ -4,4 +4,5 @@ ParagraphStartWithValidator=\u30D1\u30E9\u30B0\u30E9\u30D5\u306E\u958B\u59CB\u90
 DuplicatedSectionValidator=\u985E\u4F3C\u3059\u308B\u7BC0\uFF08"{0}"\u884C\u76EE\u304B\u3089\u958B\u59CB\uFF09\u304C\u898B\u3064\u304B\u308A\u307E\u3057\u305F
 SectionLevelValidator=\u7BC0"{0}"\u306E\u30EC\u30D9\u30EB\u304C\u6DF1\u3059\u304E\u307E\u3059
 VoidSectionValidator=\u7BC0\u306E\u5185\u5BB9\u304C\u7A7A\u3067\u3059\u3002
+EmptySectionValidator=\u7BC0 "{0}" \u306B\u4E2D\u8EAB\u304C\u3042\u308A\u307E\u305B\u3093\u3002
 GappedSectionValidator=\u7BC0 "{0}" \uFF08\u30EC\u30D9\u30EB {1}\uFF09\u306E\u914D\u7F6E\u304C\u6B63\u3057\u304F\u3042\u308A\u307E\u305B\u3093\u3002\u30EC\u30D9\u30EB {2} \u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059\u3002


Test patch:
diff --git a/redpen-core/src/test/java/cc/redpen/validator/section/EmptySectionValidatorTest.java b/redpen-core/src/test/java/cc/redpen/validator/section/EmptySectionValidatorTest.java
new file mode 100644
index 00000000..be6465df
--- /dev/null
+++ b/redpen-core/src/test/java/cc/redpen/validator/section/EmptySectionValidatorTest.java
@@ -0,0 +1,119 @@
+/**
+ * redpen: a text inspection tool
+ * Copyright (c) 2014-2015 Recruit Technologies Co., Ltd. and contributors
+ * (see CONTRIBUTORS.md)
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package cc.redpen.validator.section;
+
+import cc.redpen.RedPenException;
+import cc.redpen.config.Configuration;
+import cc.redpen.config.ValidatorConfiguration;
+import cc.redpen.model.Document;
+import cc.redpen.model.Sentence;
+import cc.redpen.validator.ValidationError;
+import cc.redpen.validator.Validator;
+import cc.redpen.validator.ValidatorFactory;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+public class EmptySectionValidatorTest {
+    private EmptySectionValidator validator = new EmptySectionValidator();
+
+    @Test
+    public void testInvalid() {
+        Document document =
+                Document.builder()
+                        .addSection(4)
+                        .addSectionHeader("Abstract")
+                        .build();
+
+        List<ValidationError> errors = new ArrayList<>();
+        validator.setErrorList(errors);
+        validator.validate(document.getSection(0));
+
+        assertEquals(1, errors.size());
+    }
+
+    @Test
+    public void testInvalidWithEmptySentence() {
+        Document document =
+                Document.builder()
+                        .addSection(1)
+                        .addSectionHeader("Abstract")
+                        .addParagraph()
+                        .build();
+
+        List<ValidationError> errors = new ArrayList<>();
+        validator.setErrorList(errors);
+        validator.validate(document.getSection(0));
+
+        assertEquals(1, errors.size());
+    }
+
+    @Test
+    public void testValid() {
+        Document document =
+                Document.builder()
+                        .addSection(1)
+                        .addSectionHeader("Abstract")
+                        .addParagraph()
+                        .addSentence(new Sentence("he is a super man.", 1))
+                        .build();
+
+        List<ValidationError> errors = new ArrayList<>();
+        validator.setErrorList(errors);
+        validator.validate(document.getSection(0));
+
+        assertEquals(0, errors.size());
+    }
+
+    @Test
+    public void testInvalidButSmallSection() {
+        Document document =
+                Document.builder()
+                        .addSection(7)
+                        .addSectionHeader("Abstract")
+                        .addParagraph()
+                        .build();
+
+        List<ValidationError> errors = new ArrayList<>();
+        validator.setErrorList(errors);
+        validator.validate(document.getSection(0));
+        assertEquals(0, errors.size());
+    }
+
+    @Test
+    public void testChangeLimit() throws RedPenException {
+        Document document =
+                Document.builder()
+                        .addSection(4)
+                        .addSectionHeader("Abstract")
+                        .addParagraph()
+                        .build();
+
+        Configuration config = Configuration.builder()
+                .addValidatorConfig(new ValidatorConfiguration("EmptySection").addProperty("limit", "3"))
+                .build();
+        Validator validator = ValidatorFactory.getInstance(config.getValidatorConfigs().get(0), config);
+        List<ValidationError> errors = new ArrayList<>();
+        validator.setErrorList(errors);
+        validator.validate(document.getSection(0));
+        assertEquals(0, errors.size());
+    }
+}