Back to LanguageTool Homepage - Privacy - Imprint

Adding additional ignore words

Hello,

Following on from from a previous post, how to add ignored words, I want to add new words after adding the new rule:

langTool.disableRule("MORFOLOGIK_RULE_EN_GB");
MorfologikSpellerRule spellingRule = new MorfologikSpellerRule(
		JLanguageTool.getMessageBundle(), language) {
	@Override
	public String getFileName() {
		return "/en/hunspell/en_GB.dict";
	}
	@Override
	public String getId() {
		return "NEW_SPELLING_RULE";
	}
};
// list of words to be ignored
spellingRule.addIgnoreTokens(ignored);
langTool.addRule(spellingRule);

It seems the only way I can add more ignored words is to get the “NEW_SPELLING_RULE” by looping all the rules:

for (Rule rule : langTool.getAllRules()) {
	if (rule.getId().equals("NEW_SPELLING_RULE")) {
		((MorfologikSpellerRule) rule).addIgnoreTokens(Arrays.asList(moreIgnoredWords));
		break;
	}
}

Is there a correct way to do this?

Cheers Greg

If you have a reference to the rule, calling "spellingRule.addIgnoreTokens(ignored); " should work, no matter if you have added the rule already. If that doesn’t work, could you maybe post a small self-contained example that shows the problem?

Hello,

Thanks for the reply,

I load the ignored words from a database and each time I create the spell checker instance I set the ignored words.

(Use SpellChecker.getInstance().check(“my text”) etc)

When I find new words I add them to the database than add them to the current rule.

see writeIgnored(String ignored){…}

So I have to stash a reference to the rule, rather than traverse the existing rules?

a method

JLanguageTool.getRule(“NEW_SPELLING_RULE”); would be useful.

public class SpellChecker {
static {
instance = new SpellChecker();
}

/**
 * Instantiates a new Spell Checker. 
 */
private SpellChecker() {
	langTool.disableRule("MORFOLOGIK_RULE_EN_GB");	
	........
	//load ignored words from database into variable ignored
	spellingRule.addIgnoreTokens(ignored);
	langTool.addRule(spellingRule);
}

public List<RuleMatch> check(String text) {
....
}

public void writeIgnored(String ignored) {
	// add rules to database
	// add to existing rule
	for (Rule rule : langTool.getAllRules()) {
		if (rule.getId().equals("NEW_SPELLING_RULE")) {
			((MorfologikSpellerRule) rule).addIgnoreTokens(Arrays.asList(ignored));
			break;
		}
	}
}

}

Cheers Greg

If you have the reference anyway, it probably makes sense to save it and use it. Otherwise, you’ll need to iterate over the rules.

Hello,

Not sure on having lots of stashed variables, but checking getAllActiveRules() only has 1 rule, which is the one added (for spell checking only).

langTool.getAllRules().size() == 15
langTool.getAllActiveRules().size() == 1

for (Rule rule : langTool.getAllActiveRules()) {
if (rule.getId().equals(“NEW_SPELLING_RULE”)) {
((MorfologikSpellerRule) rule).addIgnoreTokens(Arrays.asList(ignored));
break;
}
}

Cheers Greg.

To add the rules that make LT useful beyond simple spell checking, you’d need to call “langTool.activateDefaultPatternRules()”.

Hello,

OK, thanks, but was really only interested in spelling here, so from the docs i use:

langTool = new JLanguageTool(new BritishEnglish());

// Just spelling mistakes
for (Rule rule : langTool.getAllRules()) {
if (!rule.isDictionaryBasedSpellingRule()) {
langTool.disableRule(rule.getId());
}
}

Seems to work great!

Cheers Greg