Package org.languagetool.rules
Class Rule
- java.lang.Object
-
- org.languagetool.rules.Rule
-
- Direct Known Subclasses:
AbstractCompoundRule
,AbstractDashRule
,AbstractPatternRule
,AbstractPunctuationCheckRule
,AbstractSimpleReplaceRule
,AbstractSimpleReplaceRule2
,AbstractSpaceBeforeRule
,AbstractUnitConversionRule
,AdvancedWordRepeatRule
,BitextRule
,CommaWhitespaceRule
,ConfusionProbabilityRule
,DemoRule
,DoublePunctuationRule
,LongSentenceRule
,NeuralNetworkRule
,NgramProbabilityRule
,OpenNMTRule
,SpellingCheckRule
,TextLevelRule
,WhiteSpaceAtBeginOfParagraph
,WhitespaceBeforePunctuationRule
,WordRepeatRule
,WrongWordInContextRule
public abstract class Rule extends Object
Abstract rule class. A Rule describes a language error and can test whether a given pre-analyzed text contains that error using thematch(AnalyzedSentence)
method.Rules are created whenever a
JLanguageTool
or aMultiThreadedJLanguageTool
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.- Author:
- Daniel Naber
-
-
Field Summary
Fields Modifier and Type Field Description protected ResourceBundle
messages
-
Constructor Summary
Constructors Constructor Description Rule()
Rule(ResourceBundle messages)
Called by rules that require a translation of their messages.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method 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.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 yourmatch(AnalyzedSentence)
method needs to callgetSentenceWithImmunization(org.languagetool.AnalyzedSentence)
for this to be used and you need to checkAnalyzedTokenReadings.isImmunized()
Category
getCategory()
String
getConfigureText()
Overwrite this to define the Text in the option panel for the configurable valueList<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 panelabstract String
getDescription()
A short description of the error this rule can detect, usually in the language of the text that is checked.List<ErrorTriggeringExample>
getErrorTriggeringExamples()
Get the examples that are correct but still trigger the rule due to an issue with the rule.abstract String
getId()
A string used to identify the rule in e.g.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 valueint
getMinConfigurableValue()
Overwrite this to define the minimum of a configurable valueprotected AnalyzedSentence
getSentenceWithImmunization(AnalyzedSentence sentence)
To be called frommatch(AnalyzedSentence)
for rules that wantgetAntiPatterns()
to be considered.@Nullable 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 panelboolean
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
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.protected List<DisambiguationPatternRule>
makeAntiPatterns(List<List<PatternToken>> patternList, Language language)
Helper for implementinggetAntiPatterns()
.abstract RuleMatch[]
match(AnalyzedSentence sentence)
Check whether the given sentence matches this error rule, i.e.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
setErrorTriggeringExamples(List<ErrorTriggeringExample> examples)
Set the examples that are correct but still trigger the rule due to an issue with the rule.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
setOfficeDefaultOff()
Turns the rule off for Office Extension by default.void
setOfficeDefaultOn()
Turns the rule on for Office Extension by default.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.
-
-
-
Field Detail
-
messages
protected final ResourceBundle messages
-
-
Constructor Detail
-
Rule
public Rule()
-
Rule
public Rule(ResourceBundle messages)
Called by rules that require a translation of their messages.
-
-
Method Detail
-
getId
public abstract String getId()
A string used to identify the rule in e.g. configuration files. This string is supposed to be unique and to stay the same in all upcoming versions of LanguageTool. It's supposed to contain only the charactersA-Z
and the underscore.
-
getDescription
public abstract String getDescription()
A short description of the error this rule can detect, usually in the language of the text that is checked.
-
match
public abstract RuleMatch[] match(AnalyzedSentence sentence) throws IOException
Check whether the given sentence matches this error rule, i.e. whether it contains the error detected by this rule. Note that the order in which this method is called is not always guaranteed, i.e. the sentence order in the text may be different than the order in which you get the sentences (this may be the case when LanguageTool is used as a LibreOffice/OpenOffice add-on, for example).- Parameters:
sentence
- a pre-analyzed sentence- Returns:
- an array of
RuleMatch
objects - Throws:
IOException
-
estimateContextForSureMatch
@Experimental public 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. This is useful for check-as-you-type, where a match might occur and the word that gets typed next makes the match disappear (something one would obviously like to avoid). Note: this may over-estimate the real context size. Returns-1
when the sentence needs to end to be sure there's a match.- Since:
- 4.5
-
getAntiPatterns
public List<DisambiguationPatternRule> getAntiPatterns()
Overwrite this to avoid false alarms by ignoring these patterns - note that yourmatch(AnalyzedSentence)
method needs to callgetSentenceWithImmunization(org.languagetool.AnalyzedSentence)
for this to be used and you need to checkAnalyzedTokenReadings.isImmunized()
- Since:
- 3.1
-
hasConfigurableValue
public boolean hasConfigurableValue()
Overwrite this to return true, if a value may be configured by option panel- Since:
- 4.2
-
getDefaultValue
public int getDefaultValue()
Overwrite this to get a default Integer value by option panel- Since:
- 4.1
-
getMinConfigurableValue
public int getMinConfigurableValue()
Overwrite this to define the minimum of a configurable value- Since:
- 4.2
-
getMaxConfigurableValue
public int getMaxConfigurableValue()
Overwrite this to define the maximum of a configurable value- Since:
- 4.2
-
getConfigureText
public String getConfigureText()
Overwrite this to define the Text in the option panel for the configurable value- Since:
- 4.2
-
getSentenceWithImmunization
protected AnalyzedSentence getSentenceWithImmunization(AnalyzedSentence sentence)
To be called frommatch(AnalyzedSentence)
for rules that wantgetAntiPatterns()
to be considered.- Since:
- 3.1
-
makeAntiPatterns
protected List<DisambiguationPatternRule> makeAntiPatterns(List<List<PatternToken>> patternList, Language language)
Helper for implementinggetAntiPatterns()
.- Since:
- 3.1
-
supportsLanguage
public boolean supportsLanguage(Language language)
Whether this rule can be used for text in the given language. Since LanguageTool 2.6, this also worksPatternRule
s (before, it used to always returnfalse
for those).
-
isDictionaryBasedSpellingRule
public boolean isDictionaryBasedSpellingRule()
Whether this is a spelling rule that uses a dictionary. Rules that returntrue
here are basically rules that work like a simple hunspell-like spellchecker: they check words without considering the words' context.- Since:
- 2.5
-
useInOffice
public boolean useInOffice()
Whether this rule should be forced to be used in LO/OO extension. Rules that returntrue
will be enabled always in LO/OO extension regardless of other options like isDictionaryBasedSpellingRule().- Since:
- 2.6
-
setCorrectExamples
public final void setCorrectExamples(List<CorrectExample> correctExamples)
Set the examples that are correct and thus do not trigger the rule.
-
getCorrectExamples
public final List<CorrectExample> getCorrectExamples()
Get example sentences that are correct and thus will not match this rule.
-
setIncorrectExamples
public final void setIncorrectExamples(List<IncorrectExample> incorrectExamples)
Set the examples that are incorrect and thus do trigger the rule.
-
getIncorrectExamples
public final List<IncorrectExample> getIncorrectExamples()
Get example sentences that are incorrect and thus will match this rule.
-
setErrorTriggeringExamples
public final void setErrorTriggeringExamples(List<ErrorTriggeringExample> examples)
Set the examples that are correct but still trigger the rule due to an issue with the rule.- Since:
- 3.5
-
getErrorTriggeringExamples
public final List<ErrorTriggeringExample> getErrorTriggeringExamples()
Get the examples that are correct but still trigger the rule due to an issue with the rule.- Since:
- 3.5
-
getCategory
public final Category getCategory()
- Returns:
- a category (never null since LT 3.4)
-
setCategory
public final void setCategory(Category category)
-
isDefaultOff
public final boolean isDefaultOff()
Checks whether the rule has been turned off by default by the rule author.- Returns:
- True if the rule is turned off by default.
-
isDefaultTempOff
public final boolean isDefaultTempOff()
Checks whether the rule has been turned off using "default='temp_off'" by default by the rule author. This is a special case where the rule is off for users but active for nightly regression checks.
-
setDefaultOff
public final void setDefaultOff()
Turns the rule off by default.
-
setDefaultTempOff
public final void setDefaultTempOff()
Turns the pattern rule off by default, expect for internal regression tests.
-
setDefaultOn
public final void setDefaultOn()
Turns the rule on by default.
-
isOfficeDefaultOff
public final boolean isOfficeDefaultOff()
Checks whether the rule has been turned off by default for Office Extension by the rule author.- Returns:
- True if the rule is turned off. Overrides the default for LO/OO.
- Since:
- 4.0
-
isOfficeDefaultOn
public final boolean isOfficeDefaultOn()
Checks whether the rule has been turned on by default for Office Extension by the rule author.- Returns:
- True if the rule is turned on. Overrides the default for LO/OO.
- Since:
- 4.0
-
setOfficeDefaultOff
public final void setOfficeDefaultOff()
Turns the rule off for Office Extension by default.- Since:
- 4.0
-
setOfficeDefaultOn
public final void setOfficeDefaultOn()
Turns the rule on for Office Extension by default.- Since:
- 4.0
-
getUrl
@Nullable public @Nullable URL getUrl()
An optional URL describing the rule match in more detail. Typically points to a dictionary or grammar website with explanations and examples. Will returnnull
for rules that have no URL.- Since:
- 1.8
-
getLocQualityIssueType
public ITSIssueType getLocQualityIssueType()
Returns the Localization Quality Issue Type, as defined at http://www.w3.org/International/multilingualweb/lt/drafts/its20/its20.html#lqissue-typevalues.Note that not all languages nor all rules actually map yet to a type yet. In those cases, uncategorized is returned.
- Returns:
- the Localization Quality Issue Type - uncategorized if no type has been assigned
- Since:
- 2.5
-
setLocQualityIssueType
public void setLocQualityIssueType(ITSIssueType locQualityIssueType)
Set the Localization Quality Issue Type.- Since:
- 2.5
- See Also:
getLocQualityIssueType()
-
addExamplePair
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.- Since:
- 2.5
-
-