From caab13de808559d3c347ca21072d0a74e0b06922 Mon Sep 17 00:00:00 2001 From: takahi-i Date: Fri, 27 Oct 2017 00:07:55 +0900 Subject: [PATCH] Make SuggestExpressionValidator inherits KeyValueDictionaryValidator --- .../validator/KeyValueDictionaryValidator.java | 14 +++++--------- .../sentence/SuggestExpressionValidator.java | 16 +++------------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/redpen-core/src/main/java/cc/redpen/validator/KeyValueDictionaryValidator.java b/redpen-core/src/main/java/cc/redpen/validator/KeyValueDictionaryValidator.java index 773699171..374585e74 100644 --- a/redpen-core/src/main/java/cc/redpen/validator/KeyValueDictionaryValidator.java +++ b/redpen-core/src/main/java/cc/redpen/validator/KeyValueDictionaryValidator.java @@ -23,13 +23,11 @@ import java.util.HashMap; import java.util.Map; -import static java.util.Collections.emptyMap; import static org.apache.commons.lang3.StringUtils.isNotEmpty; public class KeyValueDictionaryValidator extends Validator { protected DictionaryLoader> loader = KEY_VALUE; private String dictionaryPrefix; - private Map dictionary = emptyMap(); public KeyValueDictionaryValidator() { super("map", new HashMap<>(), "dict", ""); @@ -49,7 +47,8 @@ public KeyValueDictionaryValidator(String dictionaryPrefix) { protected void init() throws RedPenException { if (dictionaryPrefix != null) { String defaultDictionaryFile = "default-resources/" + dictionaryPrefix + "-" + getSymbolTable().getLang() + ".dat"; - dictionary = loader.loadCachedFromResource(defaultDictionaryFile, getClass().getSimpleName() + " default dictionary"); + Map dictionary = loader.loadCachedFromResource(defaultDictionaryFile, getClass().getSimpleName() + " default dictionary"); + getMap("map").putAll(dictionary); } String confFile = getString("dict"); if (isNotEmpty(confFile)) { @@ -58,15 +57,12 @@ protected void init() throws RedPenException { } protected boolean inDictionary(String word) { - Map customDictionary = getMap("map"); - return dictionary.containsKey(word) || customDictionary != null && customDictionary.containsKey(word); + return getMap("map").containsKey(word); } protected String getValue(String word) { - Map customDictionary = getMap("map"); - if (customDictionary != null && customDictionary.containsKey(word)) { - return customDictionary.get(word); - } else if (this.dictionary.containsKey(word)) { + Map dictionary = getMap("map"); + if (dictionary != null && dictionary.containsKey(word)) { return dictionary.get(word); } return null; 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 ccc2df15d..af29239ca 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 @@ -19,7 +19,7 @@ import cc.redpen.RedPenException; import cc.redpen.model.Sentence; -import cc.redpen.validator.Validator; +import cc.redpen.validator.KeyValueDictionaryValidator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,13 +27,12 @@ import static cc.redpen.util.StringUtils.isProbablyJapanese; import static java.lang.Character.isLetter; -import static org.apache.commons.lang3.StringUtils.isNotEmpty; /** * If input sentences contain invalid expressions, this validator * returns the errors with corrected expressions. */ -public final class SuggestExpressionValidator extends Validator { +public final class SuggestExpressionValidator extends KeyValueDictionaryValidator { private static final Logger LOG = LoggerFactory.getLogger(SuggestExpressionValidator.class); public SuggestExpressionValidator() { @@ -56,16 +55,7 @@ public void validate(Sentence sentence) { @Override protected void init() throws RedPenException { - //TODO: support default dictionary. - String confFile = getString("dict"); - if (isNotEmpty(confFile)) { - LOG.info("Dictionary file is " + confFile); - getMap("map").putAll(KEY_VALUE.loadCachedFromFile(findFile(confFile), "SuggestExpressionValidator " + - "dictionary")); - } - else { - LOG.warn("Dictionary file is not specified"); - } + super.init(); } }