Back to LanguageTool Homepage - Privacy - Imprint

Callback function for pattern rules loaded from grammar.xml


(Stefan Falk) #1

I have an abstract ExtendedEnglishRule extends EnglishRule class that serves as a base class to all Extended-English rules.

This abstract class calls "onRuleMatch()" of a given ExtendedEnglishRuleListener that gets registered at startup:

@Override
public RuleMatch[] match(AnalyzedSentence sentence) throws IOException {		
	DatasheetRuleMatch[] extendedRuleMatches = doMatch(sentence);		
	for(DatasheetRuleMatch extendedRuleMatch : extendedRuleMatches) {
		this.extendedEnglishRuleListener.onRuleMatch(extendedRuleMatch);
	}		
	return extendedRuleMatches;
}

These rules are getting added by the language e.g.:

	rules.add(new ContainsNumbersMainTextRule(this.getExtendedEnglishRuleListener()));
	rules.add(new DeterminativeRule(this.getExtendedEnglishRuleListener()));

So there is no problem in having such a callback at this point.

My question is if there is also a way to have a callback-function for rules that come from grammar.xml files? What I need is some kind of notification as one of the given rules from a grammar.xml file has been matched. Is that possible?

Please let me know if what I am doing is horrible because I am not sure if I am supposed to "use" LanguageTool like the way I am using it here.

The reason for my listener up there is to be able to have context-based decisions and the possibility to enable/disable rules on each sentence and based on proprietary POS-tags.

PS: One thing I was thinking of was extending the class PatternRule but the function "match()" for example is final therefore I cannot override "match()" and do the same as for EnglishRule like above.


(Daniel Naber) #2

We don't use the concept of callbacks anywhere in LT (except the GUI code), so to add this for PatternRule you'd probably have to modify the code. But I still didn't quite understand your use case: if you have different rules for different parts of your text, why don't you just split the text and feed in into LT in several steps, with different rules activated?