Skip to content

Commit

Permalink
Merge pull request #570 from codeborne/fix-space-after
Browse files Browse the repository at this point in the history
Do not require after space if it is at the end of sentence
  • Loading branch information
takahi-i committed Mar 11, 2016
2 parents aeae70b + ad3d8f5 commit 91a1f77
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

import java.util.Set;

import static java.lang.Character.isLetterOrDigit;
import static java.lang.Character.isWhitespace;

/**
* Validate symbol has before and after symbols. Needed spaces is depend on
* the symbol and defined in DVCharacterTable.
Expand All @@ -51,14 +54,12 @@ private ValidationError validateSymbol(Sentence sentence, SymbolType symbolType)
if (position != -1) {
String key = "";

if (position > 0 && symbol.isNeedBeforeSpace()
&& !Character.isWhitespace(sentenceStr.charAt(position - 1))) {
if (position > 0 && symbol.isNeedBeforeSpace() && !isWhitespace(sentenceStr.charAt(position - 1))) {
key = "Before";
}

if (position < sentenceStr.length() - 1
&& symbol.isNeedAfterSpace()
&& !Character.isWhitespace(sentenceStr.charAt(position + 1))) {
if (position < sentenceStr.length() - 1 && symbol.isNeedAfterSpace()
&& !isWhitespace(sentenceStr.charAt(position + 1)) && isLetterOrDigit(sentenceStr.charAt(position + 1))) {
key += "After";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ public void testNeedAfterSpace() throws RedPenException {
assertEquals(10, errors.get(document).get(0).getStartPosition().get().offset);
}

@Test
public void testDoNotNeedAfterSpaceAtTheEndOfSentence() throws RedPenException {
Document document = prepareSimpleDocument("Hello (world).");

config = Configuration.builder()
.addValidatorConfig(new ValidatorConfiguration("SymbolWithSpace"))
.addSymbol(new Symbol(RIGHT_PARENTHESIS, ')', "", false, true))
.build();

RedPen redPen = new RedPen(config);
Map<Document, List<ValidationError>> errors = redPen.validate(singletonList(document));
assertEquals(0, errors.get(document).size());
}

@Test
public void testNeedBeforeSpace() throws RedPenException {
Document document = prepareSimpleDocument("I like her(Nancy) very much.");
Expand Down

0 comments on commit 91a1f77

Please sign in to comment.