Note: We recommend using the LanguageTool HTTP server instead of using the Java API directly.
It’s easy to use LanguageTool as a spell checker in your Java projects. While you can also use existing projects like HunspellJNA or HunspellBridJ (at Maven Central), LanguageTool has some advantages:
mvn dependency:tree
on your LanguageTool-based Maven project. If the dependencies contain
hunspell-native-libs
, LanguageTool also uses Hunspell internally for at least one of your languages
and thus also requires native libraries.See Java API for the Maven dependency you need to specify. Then, use code like this to find only spelling errors in a string:
JLanguageTool langTool = new JLanguageTool(new BritishEnglish());
for (Rule rule : langTool.getAllRules()) {
if (!rule.isDictionaryBasedSpellingRule()) {
langTool.disableRule(rule.getId());
}
}
List<RuleMatch> matches = langTool.check("A speling error");
for (RuleMatch match : matches) {
System.out.println("Potential typo at characters " +
match.getFromPos() + "-" + match.getToPos() + ": " +
match.getMessage());
System.out.println("Suggested correction(s): " +
match.getSuggestedReplacements());
}
If you want more than just spell checking, just remove the first for
loop to activate
all rules. See our development documentation for how
those rules work. Need help? Just ask in our forum.