Class AbstractPatternRule

java.lang.Object
org.languagetool.rules.Rule
org.languagetool.rules.patterns.AbstractPatternRule
Direct Known Subclasses:
AbstractTokenBasedRule, RegexPatternRule

public abstract class AbstractPatternRule extends Rule
An Abstract Pattern Rule that describes a pattern of words or part-of-speech tags used for PatternRule and DisambiguationPatternRule. Introduced to minimize code duplication between those classes.
  • Field Details

    • language

      protected final Language language
    • patternTokens

      protected final List<PatternToken> patternTokens
    • testUnification

      protected final boolean testUnification
    • sentStart

      protected final boolean sentStart
    • suggestionMatches

      protected List<Match> suggestionMatches
    • adjustSuggestionCase

      private boolean adjustSuggestionCase
    • suggestionMatchesOutMsg

      protected List<Match> suggestionMatchesOutMsg
    • antiPatterns

      protected List<DisambiguationPatternRule> antiPatterns
    • subId

      protected String subId
    • startPositionCorrection

      protected int startPositionCorrection
    • endPositionCorrection

      protected int endPositionCorrection
    • suggestionsOutMsg

      protected String suggestionsOutMsg
    • filter

      protected RuleFilter filter
    • filterArgs

      protected String filterArgs
    • message

      protected String message
    • sourceFile

      protected String sourceFile
    • type

      protected RuleMatch.Type type
    • lineNumber

      private int lineNumber
    • id

      private final String id
    • description

      private final String description
    • getUnified

      private final boolean getUnified
    • groupsOrUnification

      private final boolean groupsOrUnification
  • Constructor Details

  • Method Details

    • supportsLanguage

      public boolean supportsLanguage(Language language)
      Description copied from class: Rule
      Whether this rule can be used for text in the given language. Since LanguageTool 2.6, this also works PatternRules (before, it used to always return false for those).
      Overrides:
      supportsLanguage in class Rule
    • initUnifier

      private boolean initUnifier()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getDescription

      public String getDescription()
      Description copied from class: Rule
      A short description of the error this rule can detect, usually in the language of the text that is checked.
      Specified by:
      getDescription in class Rule
    • getSourceFile

      @Nullable public String getSourceFile()
      Overrides:
      getSourceFile in class Rule
    • setSourceFile

      void setSourceFile(String sourceFile)
    • getId

      public String getId()
      Description copied from class: Rule
      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 characters A-Z and the underscore.
      Specified by:
      getId in class Rule
      See Also:
    • match

      public RuleMatch[] match(AnalyzedSentence sentence) throws IOException
      Description copied from class: Rule
      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.
      Specified by:
      match in class Rule
      Parameters:
      sentence - a pre-analyzed sentence
      Returns:
      an array of RuleMatch objects
      Throws:
      IOException
    • getLanguage

      public final Language getLanguage()
      Since:
      2.3
    • setStartPositionCorrection

      public final void setStartPositionCorrection(int startPositionCorrection)
    • getStartPositionCorrection

      public final int getStartPositionCorrection()
    • setEndPositionCorrection

      public final void setEndPositionCorrection(int endPositionCorrection)
    • getEndPositionCorrection

      public final int getEndPositionCorrection()
    • getFullId

      public String getFullId()
      The rule id and its sub id, if any. The format is like RULE_ID[SUB_ID], e.g. WANT_TO[2].
      Overrides:
      getFullId in class Rule
      Since:
      3.2
      See Also:
    • getPatternRuleId

      public PatternRuleId getPatternRuleId()
      The rule id and its sub id, if any.
      Since:
      3.2
    • getSubId

      public final String getSubId()
      Overrides:
      getSubId in class Rule
    • setSubId

      public final void setSubId(String subId)
    • isGroupsOrUnification

      public boolean isGroupsOrUnification()
      Since:
      2.3
    • isGetUnified

      public boolean isGetUnified()
      Since:
      2.3
    • isSentStart

      public boolean isSentStart()
      Since:
      2.3
    • isTestUnification

      public boolean isTestUnification()
      Since:
      2.3
    • getPatternTokens

      public List<PatternToken> getPatternTokens()
      Since:
      2.3
    • addSuggestionMatch

      public final void addSuggestionMatch(Match m)
      Add formatted suggestion elements.
    • addSuggestionMatchOutMsg

      public final void addSuggestionMatchOutMsg(Match m)
      Add formatted suggestion elements outside message.
    • getSuggestionMatches

      public List<Match> getSuggestionMatches()
    • getSuggestionMatchesOutMsg

      List<Match> getSuggestionMatchesOutMsg()
    • getSuggestionsOutMsg

      @NotNull public final String getSuggestionsOutMsg()
    • getMessage

      public final String getMessage()
      Get the message shown to the user if this rule matches.
    • setMessage

      public final void setMessage(String message)
      Set the message shown to the user if this rule matches.
    • setFilter

      public void setFilter(RuleFilter filter)
      Since:
      2.7 (public since 3.2)
    • getFilter

      @Nullable public RuleFilter getFilter()
      Since:
      2.7 (public since 3.2)
    • setFilterArguments

      public void setFilterArguments(String filterArgs)
      Since:
      2.7 (public since 3.2)
    • getFilterArguments

      @Nullable public String getFilterArguments()
      Since:
      2.7 (public since 3.2)
    • setAntiPatterns

      public void setAntiPatterns(List<DisambiguationPatternRule> antiPatterns)
      Set up the list of antipatterns used to immunize tokens, i.e., make them non-matchable by the current rule. Useful for multi-word complex exceptions, such as multi-word idiomatic expressions.
      Parameters:
      antiPatterns - A list of antiPatterns, implemented as DisambiguationPatternRule.
      Since:
      2.5
    • getAntiPatterns

      public final List<DisambiguationPatternRule> getAntiPatterns()
      Description copied from class: Rule
      Overwrite this to avoid false alarms by ignoring these patterns - note that your Rule.match(AnalyzedSentence) method needs to call Rule.getSentenceWithImmunization(org.languagetool.AnalyzedSentence) for this to be used and you need to check AnalyzedTokenReadings.isImmunized()
      Overrides:
      getAntiPatterns in class Rule
      Since:
      3.1
    • getShortMessage

      String getShortMessage()
      Returns:
      String of short message as specified in <short>...</short>
      Since:
      4.4
    • getType

      @Nullable @Experimental public RuleMatch.Type getType()
      Determines the match type, based on the type variable if set (to allow overriding) or Rule.getLocQualityIssueType()
      Returns:
      The match type for the matches created by this rule
      Since:
      5.7
    • setType

      @Experimental public void setType(RuleMatch.Type type)
      Allows overriding the match type, otherwise determined by Rule.getLocQualityIssueType()
      Parameters:
      type - the desired match type
      Since:
      5.7
    • isAdjustSuggestionCase

      @Experimental public boolean isAdjustSuggestionCase()
      Allows adjusting the behavior of uppercasing suggestions when the matched text started with an upper-case letter
    • setAdjustSuggestionCase

      @Experimental public void setAdjustSuggestionCase(boolean adjustSuggestionCase)
      Allows adjusting the behavior of uppercasing suggestions when the matched text started with an upper-case letter
    • setXmlLineNumber

      public void setXmlLineNumber(int lineNumber)
    • getXmlLineNumber

      public int getXmlLineNumber()
      Returns the line in the XML file where this rule is located. Will return -1 if the line isn't known.