Case 1:
Unable check the first letter of sentence, Its only suggesting first letter as capital.
Input : strt my car. plese check.
Output : Strt my car. Plese check. (strt converts to Strt and plese convert to Plese but no suggestion for misspell)
Case 2:
If Grammar and misspell words come together only checking for grammar.
Input : strt my car,plese check.
Output : Strt my car, plese check.(there is space after comma (,) but no suggestion for misspell plese)
Code :
public class Test {
public static void main(String args) throws IOException {
String content =“strt my car,plese check.”;
List matches = jLanguageTool.check(content);
System.out.println("correctTextFromMatches :: "+ correctTextFromMatches(content,matches));
}
public static String correctTextFromMatches(
String contents, List matches) {
StringBuilder sb = new StringBuilder(contents);
List<String> errors = new ArrayList<>();
for (RuleMatch rm : matches) {
List<String> replacements = rm.getSuggestedReplacements();
if (!replacements.isEmpty()) {
errors.add(sb.substring(rm.getFromPos(), rm.getToPos()));
}
}
int offset = 0;
int counter = 0;
for (RuleMatch rm : matches) {
List<String> replacements = rm.getSuggestedReplacements();
System.out.println("replacements ::"+ replacements);
if (!replacements.isEmpty()) {
if (rm.getFromPos()-offset >= 0 &&
rm.getToPos()-offset >= rm.getFromPos()-offset &&
errors.get(counter).equals(sb.substring(rm.getFromPos() - offset, rm.getToPos() - offset))) {
sb.replace(rm.getFromPos() - offset, rm.getToPos() - offset, replacements.get(0));
offset += rm.getToPos() - rm.getFromPos() - replacements.get(0).length();
}
counter++;
}
}
return sb.toString();
}
}