@Test void testSingleSentence() throws RedPenException { ParenthesizedSentenceValidator validator = new ParenthesizedSentenceValidator(); validator.preInit(new ValidatorConfiguration("ParenthesizedSentence").addProperty("max_nesting_level", "2"), Configuration.builder().build()); List<Document> documents = new ArrayList<>(); documents.add( Document.builder(new WhiteSpaceTokenizer()) .addSection(1) .addParagraph() .addSentence(new Sentence("When it comes to the subject of cake (the sweet and delicious baked delicacy), one should" + " always remember (or at least consider)" + " this foodstuff's effect on one's ever-expanding waistline.", 1)) .addSentence(new Sentence(" I aimlessly wandered the streets near my abode (here, in the middle of (merry) England)" + " and tripped over a (discarded) matchstick.", 2)) .build()); Sentence st = documents.get(0).getLastSection().getParagraph(0).getSentence(0); List<ValidationError> errors = new ArrayList<>(); validator.setErrorList(errors); validator.validate(st); assertEquals(1, errors.size(), st.toString()); st = documents.get(0).getLastSection().getParagraph(0).getSentence(1); errors = new ArrayList<>(); validator.setErrorList(errors); validator.validate(st); assertEquals(2, errors.size(), st.toString()); }
@Override public void validate(Sentence sentence) { int nestingLevel = 0; int subsentenceLength = 0; int subsentenceCount = 0; for (TokenElement token : sentence.getTokens()) { if (token.getSurface().length() == 1) { if (OPEN_PARENS.indexOf(token.getSurface().charAt(0)) != -1) { nestingLevel++; if (nestingLevel > getInt("max_nesting_level")) { addLocalizedErrorWithPosition( "NestingLevelTooDeep", sentence, token.getOffset(), token.getOffset() + token.getSurface().length()); } } else if (CLOSE_PARENS.indexOf(token.getSurface().charAt(0)) != -1) { nestingLevel = Math.max(0, nestingLevel - 1); if (nestingLevel == 0) { subsentenceCount++; if (subsentenceLength > getInt("max_length")) { addLocalizedErrorWithPosition( "SubsentenceTooLong", sentence, token.getOffset(), token.getOffset() + token.getSurface().length()); } subsentenceLength = 0; } } } if (nestingLevel > 0) { subsentenceLength++; } } if (subsentenceCount > getInt("max_count")) { addLocalizedError("SubsentenceTooFrequent", sentence); } }
ParenthesizedSentenceValidator extends Validator { @Override public void validate(Sentence sentence) { int nestingLevel = 0; int subsentenceLength = 0; int subsentenceCount = 0; for (TokenElement token : sentence.getTokens()) { if (token.getSurface().length() == 1) { if (OPEN_PARENS.indexOf(token.getSurface().charAt(0)) != -1) { nestingLevel++; if (nestingLevel > getInt("max_nesting_level")) { addLocalizedErrorWithPosition( "NestingLevelTooDeep", sentence, token.getOffset(), token.getOffset() + token.getSurface().length()); } } else if (CLOSE_PARENS.indexOf(token.getSurface().charAt(0)) != -1) { nestingLevel = Math.max(0, nestingLevel - 1); if (nestingLevel == 0) { subsentenceCount++; if (subsentenceLength > getInt("max_length")) { addLocalizedErrorWithPosition( "SubsentenceTooLong", sentence, token.getOffset(), token.getOffset() + token.getSurface().length()); } subsentenceLength = 0; } } } if (nestingLevel > 0) { subsentenceLength++; } } if (subsentenceCount > getInt("max_count")) { addLocalizedError("SubsentenceTooFrequent", sentence); } } }
ParenthesizedSentenceValidator extends Validator { @Override public void validate(Sentence sentence) { int nestingLevel = 0; int subsentenceLength = 0; int subsentenceCount = 0; for (TokenElement token : sentence.getTokens()) { if (token.getSurface().length() == 1) { if (OPEN_PARENS.indexOf(token.getSurface().charAt(0)) != -1) { nestingLevel++; if (nestingLevel > getInt("max_nesting_level")) { addLocalizedErrorWithPosition( "NestingLevelTooDeep", sentence, token.getOffset(), token.getOffset() + token.getSurface().length()); } } else if (CLOSE_PARENS.indexOf(token.getSurface().charAt(0)) != -1) { nestingLevel = Math.max(0, nestingLevel - 1); if (nestingLevel == 0) { subsentenceCount++; if (subsentenceLength > getInt("max_length")) { addLocalizedErrorWithPosition( "SubsentenceTooLong", sentence, token.getOffset(), token.getOffset() + token.getSurface().length()); } subsentenceLength = 0; } } } if (nestingLevel > 0) { subsentenceLength++; } } if (subsentenceCount > getInt("max_count")) { addLocalizedError("SubsentenceTooFrequent", sentence); } } ParenthesizedSentenceValidator(); }
ParenthesizedSentenceValidator extends Validator { @Override public void validate(Sentence sentence) { int nestingLevel = 0; int subsentenceLength = 0; int subsentenceCount = 0; for (TokenElement token : sentence.getTokens()) { if (token.getSurface().length() == 1) { if (OPEN_PARENS.indexOf(token.getSurface().charAt(0)) != -1) { nestingLevel++; if (nestingLevel > getInt("max_nesting_level")) { addLocalizedErrorWithPosition( "NestingLevelTooDeep", sentence, token.getOffset(), token.getOffset() + token.getSurface().length()); } } else if (CLOSE_PARENS.indexOf(token.getSurface().charAt(0)) != -1) { nestingLevel = Math.max(0, nestingLevel - 1); if (nestingLevel == 0) { subsentenceCount++; if (subsentenceLength > getInt("max_length")) { addLocalizedErrorWithPosition( "SubsentenceTooLong", sentence, token.getOffset(), token.getOffset() + token.getSurface().length()); } subsentenceLength = 0; } } } if (nestingLevel > 0) { subsentenceLength++; } } if (subsentenceCount > getInt("max_count")) { addLocalizedError("SubsentenceTooFrequent", sentence); } } ParenthesizedSentenceValidator(); @Override void validate(Sentence sentence); }
ParenthesizedSentenceValidator extends Validator { @Override public void validate(Sentence sentence) { int nestingLevel = 0; int subsentenceLength = 0; int subsentenceCount = 0; for (TokenElement token : sentence.getTokens()) { if (token.getSurface().length() == 1) { if (OPEN_PARENS.indexOf(token.getSurface().charAt(0)) != -1) { nestingLevel++; if (nestingLevel > getInt("max_nesting_level")) { addLocalizedErrorWithPosition( "NestingLevelTooDeep", sentence, token.getOffset(), token.getOffset() + token.getSurface().length()); } } else if (CLOSE_PARENS.indexOf(token.getSurface().charAt(0)) != -1) { nestingLevel = Math.max(0, nestingLevel - 1); if (nestingLevel == 0) { subsentenceCount++; if (subsentenceLength > getInt("max_length")) { addLocalizedErrorWithPosition( "SubsentenceTooLong", sentence, token.getOffset(), token.getOffset() + token.getSurface().length()); } subsentenceLength = 0; } } } if (nestingLevel > 0) { subsentenceLength++; } } if (subsentenceCount > getInt("max_count")) { addLocalizedError("SubsentenceTooFrequent", sentence); } } ParenthesizedSentenceValidator(); @Override void validate(Sentence sentence);  }