Best way to add new rules when using with Maven in a Java Application

Hi,

I want to use LanguageTools (great tool!) in a Java application (in a Maven Mojo) for an automatic check of our DocBook documentation.
What is the best way to add my own rules? With addTemporaryFile? Another question: In the standalone version, one gets the context of an error which is very helpful but RuleMatch has not method for this. So, how can I get the context?

Regards,
Michael

What is the best way to add my own rules? With addTemporaryFile?

addTemporaryFile is an internal method, I will improve its javadoc to
avoid confusion. This should work:

  PatternRuleLoader loader = new PatternRuleLoader();
  List<PatternRule> rules = loader.getRules(new File("/path/to/myGrammar.xml"));
  for (PatternRule rule : rules) {
    languageTool.addRule(rule);
  }

Another question: In the standalone version, one gets the context of
an error which is very helpful but RuleMatch has not method for this.
So, how can I get the context?

There are methods for that in StringTools, or in ContextTools (if
you’re using the development version).

Regards
Daniel

Wow, that was fast :wink:
The PatternRuleLoader worked fine, thanks!
How is it with words for the spellcheck? I’ve added some names etc. to the English ignore.txt file of hunspell. Is there a similar way to add them to the JLanguageTool class?

Regards,
Michael

How is it with words for the spellcheck? I’ve added some names etc.
to the English ignore.txt file of hunspell. Is there a similar way to
add them to the JLanguageTool class?

Yes, see
http://languagetool-user-forum.2306527.n4.nabble.com/Is-there-support-to-add-custom-words-to-the-existing-dictionary-td4641042.html

Regards
Daniel

Actually it’s even a bit easier using JLanguageTool.loadPatternRules(File), no need to use PatternRuleLoader directly.

Suppose i have written a class for a rule by extending the rule class.How do i use that newly built rule in my language tool.How do i add that class? How do i test my formed rule?

Simply call JLanguageTool.addRule(new YourNewRule()) if you’re using LanguageTool via API. If not, you will have to compile LT yourself and add your rule to English.getRelevantRules() (or whatever language your rule is for).

I am using LanguageTool API .The WordRepeatRule.java code is this languagetool/WordRepeatRule.java at master · languagetool-org/languagetool · GitHub and i have written a code in java like this 7S87Pi - Online Java Compiler & Debugging Tool - Ideone.com ,but this doesn’t work.It shows error. Kindly help

It looks like your project isn’t set up properly yet. There’s documentation at Java API - LanguageTool Wiki on how to add LanguageTool to your project.