Back to LanguageTool Homepage - Privacy - Imprint

Unable to check first word and grammar and misspell word together of sentence using Java API

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();

}
}

You have to re-check your text after accepting suggestions.

Hi @dnaber ,
First of all thank you for your quick response.
Yes after rechecking it, it works fine.
Code correction -

String content =“strt my car.plese check.”;
List matches = jLanguageTool.check(content);
String recheck = correctTextFromMatches(content,matches);
List rematches = jLanguageTool.check(recheck);
System.out.println("Output "+ correctTextFromMatches(recheck,rematches));

Query - Do we have any method which can resolve this instead to checking this twice ?

No, there’s no way to do that in one step.