Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance SpaceBetweenAlphabeticalWordValidator #740

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
6 changes: 5 additions & 1 deletion redpen-core/src/main/java/cc/redpen/validator/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ protected float getFloat(String name) {
}

protected String getString(String name) {
return config.getProperties().getOrDefault(name, (String) defaultProps.get(name));
if(config != null){
return config.getProperties().getOrDefault(name, (String) defaultProps.get(name));
}else{
return (String) defaultProps.get(name);
}
}

protected boolean getBoolean(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public class SpaceBetweenAlphabeticalWordValidator extends Validator {
private final Pattern pat = Pattern.compile(shard + "\\s+(" + word + ")\\s+" + shard);

public SpaceBetweenAlphabeticalWordValidator() {
super("forbidden", false); // Spaces are enforced (false) or forbidden (true)
super("forbidden", false, // Spaces are enforced (false) or forbidden (true)
"skip_before", "",
"skip_after", "");
}

@Override public List<String> getSupportedLanguages() {
Expand Down Expand Up @@ -76,15 +78,17 @@ public void validate(Sentence sentence) {
// TODO: need refactoring...
private boolean notHasWhiteSpaceBeforeLeftParenthesis(char prevCharacter, char character) {
return !StringUtils.isBasicLatin(prevCharacter)
&& getString("skip_before").indexOf(prevCharacter) == -1
&& prevCharacter != leftParenthesis
&& prevCharacter != comma
&& (prevCharacter != rightParenthesis && rightParenthesis != '') // For handling multi-byte Parenthesis
&& (prevCharacter != rightParenthesis && rightParenthesis != '') // For handling multi-byte Parenthesis
&& StringUtils.isBasicLatin(character)
&& Character.isLetter(character);
}

private boolean notHasWhiteSpaceAfterRightParenthesis(char prevCharacter, char character) {
return !StringUtils.isBasicLatin(character)
&& getString("skip_after").indexOf(character) == -1
&& character != rightParenthesis
&& (character != leftParenthesis && leftParenthesis != '(') // For handling multi-byte Parenthesis
&& character != comma
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,36 @@ public void testNeedNoBeforeAndAfterSpace() throws RedPenException {
assertEquals(1, errors.size());
}

@Test
public void testSkipBefore() throws RedPenException {
SpaceBetweenAlphabeticalWordValidator validator = new SpaceBetweenAlphabeticalWordValidator();
validator.preInit(new ValidatorConfiguration("SpaceBetweenAlphabeticalWord").addProperty("skip_before", "「"), Configuration.builder().build());
List<ValidationError> errors = new ArrayList<>();
validator.setErrorList(errors);
validator.validate(new Sentence("きょうは「Coke 」を飲みたい。", 0));
assertEquals(0, errors.size());
}

@Test
public void testSkipAfter() throws RedPenException {
SpaceBetweenAlphabeticalWordValidator validator = new SpaceBetweenAlphabeticalWordValidator();
validator.preInit(new ValidatorConfiguration("SpaceBetweenAlphabeticalWord").addProperty("skip_after", "」"), Configuration.builder().build());
List<ValidationError> errors = new ArrayList<>();
validator.setErrorList(errors);
validator.validate(new Sentence("きょうは「 Coke」を飲みたい。", 0));
assertEquals(0, errors.size());
}

@Test
public void testSkipBeforeAndAfter() throws RedPenException {
SpaceBetweenAlphabeticalWordValidator validator = new SpaceBetweenAlphabeticalWordValidator();
validator.preInit(new ValidatorConfiguration("SpaceBetweenAlphabeticalWord").addProperty("skip_before", "・「").addProperty("skip_after", "・」"), Configuration.builder().build());
List<ValidationError> errors = new ArrayList<>();
validator.setErrorList(errors);
validator.validate(new Sentence("きょうは「Coke・Pepsi」を飲みたい。", 0));
assertEquals(0, errors.size());
}


@Test
public void testSupportedLanguages() {
Expand Down