Package org.languagetool.rules
Class Rule
java.lang.Object
org.languagetool.rules.Rule
- Direct Known Subclasses:
AbstractCompoundRule,AbstractDashRule,AbstractPatternRule,AbstractPunctuationCheckRule,AbstractSimpleReplaceRule,AbstractSimpleReplaceRule2,AbstractSpaceBeforeRule,AbstractSpecificCaseRule,AbstractUnitConversionRule,AdvancedWordRepeatRule,AgreementRule,AgreementRule2,AvsAnRule,BitextRule,CaseRule,CommaWhitespaceRule,CompoundInfinitivRule,ConfusionProbabilityRule,DashRule,DemoRule,DhaNoBeirtRule,DoublePunctuationRule,FakeRule,GrammalecteRule,GrammalecteRule.GrammalecteInternalRule,GRPCRule.GRPCSubRule,HiddenCharacterRule,IrishSpecificCaseRule,KhmerWordRepeatRule,MissingCommaRelativeClauseRule,MissingHyphenRule,MissingVerbRule,MixedAlphabetsRule,NgramProbabilityRule,NumeralStressRule,OldSpellingRule,PortugueseAccentuationCheckRule,PreferredWordRule,ProhibitedCompoundRule,PronomFebleDuplicateRule,QuestionWhitespaceRule,RedundantModalOrAuxiliaryVerb,RemoteRule,RemoteRuleFilters.ExpectedRule,ResultExtender.HiddenRule,RuleData,RuleFilter.FakeRule,RussianSpecificCaseRule,RussianVerbConjugationRule,SimpleReplaceRenamedRule,SimpleReplaceSpelling2019Rule,SpaceInCompoundRule,SpecificIdRule,SpellingCheckRule,SubjectVerbAgreementRule,TextLevelRule,TokenAgreementAdjNounRule,TokenAgreementNounVerbRule,TokenAgreementNumrNounRule,TokenAgreementPrepNounRule,TokenAgreementVerbNounRule,TopoReplaceRule,TypographyRule,UpperCaseNgramRule,UpperCaseNgramRule,WhiteSpaceAtBeginOfParagraph,WhitespaceBeforePunctuationRule,WiederVsWiderRule,WordRepeatRule,WrongWordInContextRule
Abstract rule class. A Rule describes a language error and can test whether a
given pre-analyzed text contains that error using the
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).
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Categoryprivate List<CorrectExample> private booleanprivate booleanprivate intprivate List<ErrorTriggeringExample> private booleanprivate List<IncorrectExample> private booleanprivate booleanprivate ITSIssueTypeprotected final ResourceBundleprivate intprivate static final Categoryprivate booleanprivate booleanprivate intprivate URL -
Constructor Summary
ConstructorsConstructorDescriptionRule()Rule(ResourceBundle messages) Called by rules that require a translation of their messages. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddExamplePair(IncorrectExample incorrectSentence, CorrectExample correctSentence) Convenience method to add a pair of sentences: an incorrect sentence and the same sentence with the error corrected.voidvoidaddToneTags(List<String> toneTags) protected static Supplier<List<DisambiguationPatternRule>> cacheAntiPatterns(Language language, List<List<PatternToken>> antiPatterns) intA number that estimates how many words there must be after a match before we can be (relatively) sure the match is valid.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()final List<CorrectExample> Get example sentences that are correct and thus will not match this rule.abstract StringA short description of the error this rule can detect, usually in the language of the text that is checked.intfinal List<ErrorTriggeringExample> Get the examples that are correct but still trigger the rule due to an issue with the rule.Same asgetId()for Java rules.abstract StringgetId()A string used to identify the rule in e.g.final List<IncorrectExample> Get example sentences that are incorrect and thus will match this rule.Returns the Localization Quality Issue Type, as defined at http://www.w3.org/International/multilingualweb/lt/drafts/its20/its20.html#lqissue-typevalues.intintOverwrite this to return configurable options for option panelprotected AnalyzedSentencegetSentenceWithImmunization(AnalyzedSentence sentence) To be called frommatch(AnalyzedSentence)for rules that wantgetAntiPatterns()to be considered.getSubId()getTags()getUrl()An optional URL describing the rule match in more detail.booleanbooleanhasToneTag(ToneTag toneTag) final booleanChecks whether the rule has been turned off by default by the rule author.final booleanChecks whether the rule has been turned off using "default='temp_off'" by default by the rule author.booleanWhether this is a spelling rule that uses a dictionary.booleanbooleanfinal booleanChecks whether the rule has been turned off by default for Office Extension by the rule author.final booleanChecks whether the rule has been turned on by default for Office Extension by the rule author.booleanprotected static 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.final voidsetCategory(Category category) final voidsetCorrectExamples(List<CorrectExample> correctExamples) Set the examples that are correct and thus do not trigger the rule.final voidTurns the rule off by default.final voidTurns the rule on by default.final voidTurns the pattern rule off by default, expect for internal regression tests.voidsetDistanceTokens(int i) final voidsetErrorTriggeringExamples(List<ErrorTriggeringExample> examples) Set the examples that are correct but still trigger the rule due to an issue with the rule.protected voidsetExamplePair(IncorrectExample incorrectSentence, CorrectExample correctSentence) Convenience method to set a pair of sentences: an incorrect sentence and the same sentence with the error corrected.voidsetGoalSpecific(boolean goalSpecific) voidsetIncludedInHiddenMatches(boolean includedInHiddenMatches) final voidsetIncorrectExamples(List<IncorrectExample> incorrectExamples) Set the examples that are incorrect and thus do trigger the rule.voidsetLocQualityIssueType(ITSIssueType locQualityIssueType) Set the Localization Quality Issue Type.voidsetMinPrevMatches(int i) final voidTurns the rule off for Office Extension by default.final voidTurns the rule on for Office Extension by default.voidsetPremium(boolean premium) voidsetPriority(int priority) voidvoidsetToneTags(List<ToneTag> toneTags) voidbooleansupportsLanguage(Language language) Whether this rule can be used for text in the given language.protected final RuleMatch[]toRuleMatchArray(List<RuleMatch> ruleMatches) booleanWhether this rule should be forced to be used in LO/OO extension.
-
Field Details
-
MISC
-
messages
-
tags
-
toneTags
-
isGoalSpecific
private boolean isGoalSpecific -
correctExamples
-
incorrectExamples
-
errorTriggeringExamples
-
locQualityIssueType
-
category
-
url
-
isPremium
private boolean isPremium -
defaultOff
private boolean defaultOff -
defaultTempOff
private boolean defaultTempOff -
officeDefaultOn
private boolean officeDefaultOn -
officeDefaultOff
private boolean officeDefaultOff -
includedInHiddenMatches
private boolean includedInHiddenMatches -
minPrevMatches
private int minPrevMatches -
distanceTokens
private int distanceTokens -
priority
private int priority
-
-
Constructor Details
-
Rule
public Rule() -
Rule
Called by rules that require a translation of their messages.
-
-
Method Details
-
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-Zand the underscore. -
getFullId
Same asgetId()for Java rules. For XML rules, this can contain a number that identifies the subrule of a rule group.- Since:
- 4.9
-
getSubId
-
getSourceFile
-
getDescription
A short description of the error this rule can detect, usually in the language of the text that is checked. -
match
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 from 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). In other words, implementations must be stateless, so that a previous call to this method has no influence on later calls.- Parameters:
sentence- a pre-analyzed sentence- Returns:
- an array of
RuleMatchobjects - Throws:
IOException
-
estimateContextForSureMatch
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-1when the sentence needs to end to be sure there's a match.- Since:
- 4.5
-
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
-
getRuleOptions
Overwrite this to return configurable options for option panel- Since:
- 6.5
-
getSentenceWithImmunization
To be called frommatch(AnalyzedSentence)for rules that wantgetAntiPatterns()to be considered.- Since:
- 3.1
-
makeAntiPatterns
protected static List<DisambiguationPatternRule> makeAntiPatterns(List<List<PatternToken>> patternList, Language language) Helper for implementinggetAntiPatterns(). The result of this method should better be cached, please seecacheAntiPatterns(org.languagetool.Language, java.util.List<java.util.List<org.languagetool.rules.patterns.PatternToken>>)which does that.- Since:
- 3.1
-
cacheAntiPatterns
protected static Supplier<List<DisambiguationPatternRule>> cacheAntiPatterns(Language language, List<List<PatternToken>> antiPatterns) - Returns:
- a memoizing supplier that caches the result of
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. - Since:
- 5.2
-
supportsLanguage
Whether this rule can be used for text in the given language. Since LanguageTool 2.6, this also worksPatternRules (before, it used to always returnfalsefor those). -
isDictionaryBasedSpellingRule
public boolean isDictionaryBasedSpellingRule()Whether this is a spelling rule that uses a dictionary. Rules that returntruehere 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 returntruewill be enabled always in LO/OO extension regardless of other options like isDictionaryBasedSpellingRule().- Since:
- 2.6
-
setCorrectExamples
Set the examples that are correct and thus do not trigger the rule. -
getCorrectExamples
Get example sentences that are correct and thus will not match this rule. -
setIncorrectExamples
Set the examples that are incorrect and thus do trigger the rule. -
getIncorrectExamples
Get example sentences that are incorrect and thus will match this rule. -
setErrorTriggeringExamples
Set the examples that are correct but still trigger the rule due to an issue with the rule.- Since:
- 3.5
-
getErrorTriggeringExamples
Get the examples that are correct but still trigger the rule due to an issue with the rule.- Since:
- 3.5
-
getCategory
- Returns:
- a category (never null since LT 3.4)
-
setCategory
-
toRuleMatchArray
-
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
An optional URL describing the rule match in more detail. Typically points to a dictionary or grammar website with explanations and examples. Will returnnullfor rules that have no URL.- Since:
- 1.8
-
setUrl
- Since:
- 1.8
- See Also:
-
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,
uncategorizedis returned.- Returns:
- the Localization Quality Issue Type -
uncategorizedif no type has been assigned - Since:
- 2.5
-
setLocQualityIssueType
Set the Localization Quality Issue Type.- Since:
- 2.5
- See Also:
-
addExamplePair
Convenience method to add a pair of sentences: an incorrect sentence and the same sentence with the error corrected.- Since:
- 2.5
-
setExamplePair
Convenience method to set a pair of sentences: an incorrect sentence and the same sentence with the error corrected.- Since:
- 4.9
-
addTags
- Since:
- 5.1
-
setTags
- Since:
- 5.1
-
getTags
- Since:
- 5.1
-
hasTag
- Since:
- 5.1
-
addToneTags
- Since:
- 6.2
-
setToneTags
- Since:
- 6.2
-
getToneTags
- Since:
- 6.2
-
hasToneTag
- Since:
- 6.2
-
isPremium
public boolean isPremium() -
setPremium
public void setPremium(boolean premium) -
setMinPrevMatches
public void setMinPrevMatches(int i) -
getMinPrevMatches
public int getMinPrevMatches() -
setDistanceTokens
public void setDistanceTokens(int i) -
getDistanceTokens
public int getDistanceTokens() -
isGoalSpecific
public boolean isGoalSpecific() -
setGoalSpecific
public void setGoalSpecific(boolean goalSpecific) -
getPriority
public int getPriority() -
setPriority
public void setPriority(int priority) -
isIncludedInHiddenMatches
public boolean isIncludedInHiddenMatches()- Returns:
- whether this rule should be run when hidden rules are enabled when Rule.isPremium is true and QueryParams.premium is false, this rule will only be run when both Rule.isIncludedInHiddenMatches and QueryParams.enableHiddenRules are true No effect otherwise
- Since:
- 6.5
-
setIncludedInHiddenMatches
public void setIncludedInHiddenMatches(boolean includedInHiddenMatches) - Parameters:
includedInHiddenMatches- whether this rule should be run when hidden rules are enabled (if it's a Premium rule)- Since:
- 6.5
-