Very bad performance when integrating into Java application

We are evaluating LanguageTool to integrate it into our own (Spring based) Java application, but we only need a basic spell check (plus user specific words to ignore)

So I followed Embedding LanguageTool in Java applications | dev.languagetool.org and created a simple main class:

Language de = Languages.getLanguageForShortCode("de-DE");
JLanguageTool lt = new JLanguageTool(de);

String toCheck = "Dieses Vorgehen scheint zielführend";
List<String> wordsToIgnore = Arrays.asList("foobar");

for (Rule rule : lt.getAllRules()) {
    if (rule instanceof SpellingCheckRule scr) {
        scr.addIgnoreTokens(wordsToIgnore);
    } else {
        lt.disableRule(rule.getId());
    }
}

List<RuleMatch> matches = lt.check(toCheck);

When I run the above code, the call lt.check() takes about 4-5 seconds which is unexpected (especially because only a single rule is active).

Is this because I am calling this only once from within the main() method and LanguageTool needs some “warming up” time?

If this is expected, is there a way to speed this up? We don’t expect to have strings that are longer than approx. 1000 characters to check.

Disabling of the rules also takes 1-2 seconds. But as JLanguageTool is not thread safe, we would need to do this for each “check request”, or is there a way to avoid that?

Yes, there’s a lot of warm-up time. The easiest fix/workaround is to initialize LT and check some fake text before you start sending real texts.

Thanks for the answer. But the documentation says, JLanguageTool isn’t thread safe and I need to create a new instance each time I want to check a text.

Please try creating a new JLanguageTool each time, but using the same language object. That should be fast.