public abstract class Rule extends Object
match(AnalyzedSentence)
method.
Rules are created whenever a JLanguageTool
or
a MultiThreadedJLanguageTool
object is created.
As these objects are not thread-safe, this can happen often. Rules should thus
make sure that their initialization works fast. For example, if a rule needs
to load data from disk, it should store it in a static variable to make sure
the loading happens only once.
Rules also need to make sure their match()
code is stateless, i.e. that
its results are not influenced by previous calls to match()
(this is relevant
if pipeline caching is used).
Modifier and Type | Field and Description |
---|---|
protected ResourceBundle |
messages |
Constructor and Description |
---|
Rule() |
Rule(ResourceBundle messages)
Called by rules that require a translation of their messages.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addExamplePair(IncorrectExample incorrectSentence,
CorrectExample correctSentence)
Convenience method to add a pair of sentences: an incorrect sentence and the same sentence
with the error corrected.
|
void |
addTags(List<String> tags) |
void |
addToneTags(List<String> toneTags) |
protected static Supplier<List<DisambiguationPatternRule>> |
cacheAntiPatterns(Language language,
List<List<PatternToken>> antiPatterns) |
int |
estimateContextForSureMatch()
A number that estimates how many words there must be after a match before we
can be (relatively) sure the match is valid.
|
List<DisambiguationPatternRule> |
getAntiPatterns()
Overwrite this to avoid false alarms by ignoring these patterns -
note that your
match(AnalyzedSentence) method needs to
call getSentenceWithImmunization(org.languagetool.AnalyzedSentence) for this to be used
and you need to check AnalyzedTokenReadings.isImmunized() |
Category |
getCategory() |
String |
getConfigureText()
Overwrite this to define the Text in the option panel for the configurable value
|
List<CorrectExample> |
getCorrectExamples()
Get example sentences that are correct and thus will not match this rule.
|
int |
getDefaultValue()
Overwrite this to get a default Integer value by option panel
|
abstract String |
getDescription()
A short description of the error this rule can detect, usually in the language of the text
that is checked.
|
int |
getDistanceTokens() |
List<ErrorTriggeringExample> |
getErrorTriggeringExamples()
Get the examples that are correct but still trigger the rule due to an issue with the rule.
|
String |
getFullId()
Same as
getId() for Java rules. |
abstract String |
getId()
A string used to identify the rule in e.g. configuration files.
|
List<IncorrectExample> |
getIncorrectExamples()
Get example sentences that are incorrect and thus will match this rule.
|
ITSIssueType |
getLocQualityIssueType()
Returns the Localization Quality Issue Type, as defined
at http://www.w3.org/International/multilingualweb/lt/drafts/its20/its20.html#lqissue-typevalues.
|
int |
getMaxConfigurableValue()
Overwrite this to define the maximum of a configurable value
|
int |
getMinConfigurableValue()
Overwrite this to define the minimum of a configurable value
|
int |
getMinPrevMatches() |
protected AnalyzedSentence |
getSentenceWithImmunization(AnalyzedSentence sentence)
To be called from
match(AnalyzedSentence) for rules that want
getAntiPatterns() to be considered. |
String |
getSourceFile() |
String |
getSubId() |
List<Tag> |
getTags() |
List<ToneTag> |
getToneTags() |
URL |
getUrl()
An optional URL describing the rule match in more detail.
|
boolean |
hasConfigurableValue()
Overwrite this to return true, if a value may be configured by option panel
|
boolean |
hasTag(Tag tag) |
boolean |
hasToneTag(ToneTag toneTag) |
boolean |
isDefaultOff()
Checks whether the rule has been turned off by default by the rule author.
|
boolean |
isDefaultTempOff()
Checks whether the rule has been turned off using "default='temp_off'" by default by the rule author.
|
boolean |
isDictionaryBasedSpellingRule()
Whether this is a spelling rule that uses a dictionary.
|
boolean |
isGoalSpecific() |
boolean |
isOfficeDefaultOff()
Checks whether the rule has been turned off by default for Office Extension by the rule author.
|
boolean |
isOfficeDefaultOn()
Checks whether the rule has been turned on by default for Office Extension by the rule author.
|
boolean |
isPremium() |
protected static List<DisambiguationPatternRule> |
makeAntiPatterns(List<List<PatternToken>> patternList,
Language language)
Helper for implementing
getAntiPatterns() . |
abstract RuleMatch[] |
match(AnalyzedSentence sentence)
Check whether the given sentence matches this error rule, i.e. whether it
contains the error detected by this rule.
|
void |
setCategory(Category category) |
void |
setCorrectExamples(List<CorrectExample> correctExamples)
Set the examples that are correct and thus do not trigger the rule.
|
void |
setDefaultOff()
Turns the rule off by default.
|
void |
setDefaultOn()
Turns the rule on by default.
|
void |
setDefaultTempOff()
Turns the pattern rule off by default, expect for internal regression tests.
|
void |
setDistanceTokens(int i) |
void |
setErrorTriggeringExamples(List<ErrorTriggeringExample> examples)
Set the examples that are correct but still trigger the rule due to an issue with the rule.
|
protected void |
setExamplePair(IncorrectExample incorrectSentence,
CorrectExample correctSentence)
Convenience method to set a pair of sentences: an incorrect sentence and the same sentence
with the error corrected.
|
void |
setGoalSpecific(boolean goalSpecific) |
void |
setIncorrectExamples(List<IncorrectExample> incorrectExamples)
Set the examples that are incorrect and thus do trigger the rule.
|
void |
setLocQualityIssueType(ITSIssueType locQualityIssueType)
Set the Localization Quality Issue Type.
|
void |
setMinPrevMatches(int i) |
void |
setOfficeDefaultOff()
Turns the rule off for Office Extension by default.
|
void |
setOfficeDefaultOn()
Turns the rule on for Office Extension by default.
|
void |
setPremium(boolean premium) |
void |
setTags(List<Tag> tags) |
void |
setToneTags(List<ToneTag> toneTags) |
void |
setUrl(URL url) |
boolean |
supportsLanguage(Language language)
Whether this rule can be used for text in the given language.
|
protected RuleMatch[] |
toRuleMatchArray(List<RuleMatch> ruleMatches) |
boolean |
useInOffice()
Whether this rule should be forced to be used in LO/OO extension.
|
protected final ResourceBundle messages
public Rule()
public Rule(ResourceBundle messages)
public abstract String getId()
A-Z
and the underscore.public String getFullId()
getId()
for Java rules. For XML rules, this can contain a number
that identifies the subrule of a rule group.@Nullable public String getSubId()
@Nullable public String getSourceFile()
public abstract String getDescription()
public abstract RuleMatch[] match(AnalyzedSentence sentence) throws IOException
sentence
- a pre-analyzed sentenceRuleMatch
objectsIOException
public int estimateContextForSureMatch()
-1
when the sentence needs to end to be sure there's a match.public List<DisambiguationPatternRule> getAntiPatterns()
match(AnalyzedSentence)
method needs to
call getSentenceWithImmunization(org.languagetool.AnalyzedSentence)
for this to be used
and you need to check AnalyzedTokenReadings.isImmunized()
public boolean hasConfigurableValue()
public int getDefaultValue()
public int getMinConfigurableValue()
public int getMaxConfigurableValue()
public String getConfigureText()
protected AnalyzedSentence getSentenceWithImmunization(AnalyzedSentence sentence)
match(AnalyzedSentence)
for rules that want
getAntiPatterns()
to be considered.protected static List<DisambiguationPatternRule> makeAntiPatterns(List<List<PatternToken>> patternList, Language language)
getAntiPatterns()
. The result of this method should better be cached, please see
cacheAntiPatterns(org.languagetool.Language, java.util.List<java.util.List<org.languagetool.rules.patterns.PatternToken>>)
which does that.protected static Supplier<List<DisambiguationPatternRule>> cacheAntiPatterns(Language language, List<List<PatternToken>> antiPatterns)
makeAntiPatterns(java.util.List<java.util.List<org.languagetool.rules.patterns.PatternToken>>, org.languagetool.Language)
. It makes sense
to store the returned value, e.g. in a field.public boolean supportsLanguage(Language language)
PatternRule
s
(before, it used to always return false
for those).public boolean isDictionaryBasedSpellingRule()
true
here are basically rules that work like
a simple hunspell-like spellchecker: they check words without considering
the words' context.public boolean useInOffice()
true
will be enabled always in LO/OO extension
regardless of other options like isDictionaryBasedSpellingRule().public final void setCorrectExamples(List<CorrectExample> correctExamples)
public final List<CorrectExample> getCorrectExamples()
public final void setIncorrectExamples(List<IncorrectExample> incorrectExamples)
public final List<IncorrectExample> getIncorrectExamples()
public final void setErrorTriggeringExamples(List<ErrorTriggeringExample> examples)
public final List<ErrorTriggeringExample> getErrorTriggeringExamples()
@NotNull public Category getCategory()
public final void setCategory(Category category)
public final boolean isDefaultOff()
public final boolean isDefaultTempOff()
public final void setDefaultOff()
public final void setDefaultTempOff()
public final void setDefaultOn()
public final boolean isOfficeDefaultOff()
public final boolean isOfficeDefaultOn()
public final void setOfficeDefaultOff()
public final void setOfficeDefaultOn()
@Nullable public URL getUrl()
null
for rules that have no URL.public ITSIssueType getLocQualityIssueType()
Note that not all languages nor all rules actually map yet to a type yet. In those cases, uncategorized is returned.
public void setLocQualityIssueType(ITSIssueType locQualityIssueType)
getLocQualityIssueType()
protected void addExamplePair(IncorrectExample incorrectSentence, CorrectExample correctSentence)
protected void setExamplePair(IncorrectExample incorrectSentence, CorrectExample correctSentence)
public boolean hasTag(Tag tag)
public boolean hasToneTag(ToneTag toneTag)
public boolean isPremium()
public void setPremium(boolean premium)
public void setMinPrevMatches(int i)
public int getMinPrevMatches()
public void setDistanceTokens(int i)
public int getDistanceTokens()
public boolean isGoalSpecific()
public void setGoalSpecific(boolean goalSpecific)