Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, String>> loader = KEY_VALUE;
private String dictionaryPrefix;
private Map<String, String> dictionary = emptyMap();

public KeyValueDictionaryValidator() {
super("map", new HashMap<>(), "dict", "");
Expand All @@ -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<String, String> dictionary = loader.loadCachedFromResource(defaultDictionaryFile, getClass().getSimpleName() + " default dictionary");
getMap("map").putAll(dictionary);
}
String confFile = getString("dict");
if (isNotEmpty(confFile)) {
Expand All @@ -58,15 +57,12 @@ protected void init() throws RedPenException {
}

protected boolean inDictionary(String word) {
Map<String, String> customDictionary = getMap("map");
return dictionary.containsKey(word) || customDictionary != null && customDictionary.containsKey(word);
return getMap("map").containsKey(word);
}

protected String getValue(String word) {
Map<String, String> customDictionary = getMap("map");
if (customDictionary != null && customDictionary.containsKey(word)) {
return customDictionary.get(word);
} else if (this.dictionary.containsKey(word)) {
Map<String, String> dictionary = getMap("map");
if (dictionary != null && dictionary.containsKey(word)) {
return dictionary.get(word);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@

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;

import java.util.HashMap;

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() {
Expand All @@ -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();
}

}