Class WrongWordInContextRule

java.lang.Object
org.languagetool.rules.Rule
org.languagetool.rules.WrongWordInContextRule

public abstract class WrongWordInContextRule extends Rule
Check if there is a confusion of two words (which might have a similar spelling) depending on the context. This rule loads the words and their respective context from an external file with the following format (tab-separated):
word1 word2 match1 match2 context1 context2 explanation1 explanation2
  • word1 and word2 are regular expressions of the words that can easily be confused
  • match1 is the substring of word1 that will be replaced with match2 when word1 occurs in a context where it is probably wrong (and vice versa)
  • context1 is the context in which word1 typically occurs (but which is wrong for word2), given as a regular expression
  • context2 is the context in which word2 typically occurs (but which is wrong for word1), given as a regular expression
  • explanation1 is a short description of word1 (optional)
  • explanation2 is a short description of word2 (optional)
  • Field Details

  • Constructor Details

    • WrongWordInContextRule

      public WrongWordInContextRule(ResourceBundle messages)
  • Method Details

    • getFilename

      protected abstract String getFilename()
    • getCategoryString

      protected String getCategoryString()
    • 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
    • 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
    • setMatchLemmmas

      public void setMatchLemmmas()
    • match

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

      protected abstract String getMessageString()
      Returns:
      a string like "Possible confusion of words: Did you mean <suggestion>$SUGGESTION</suggestion> instead of '$WRONGWORD'?"
    • getShortMessageString

      protected abstract String getShortMessageString()
      Returns:
      a string like "Possible confusion of words"
    • getLongMessageString

      protected abstract String getLongMessageString()
      Returns:
      a string like "Possible confusion of words: Did you mean <suggestion>$SUGGESTION</suggestion> (= $EXPLANATION_SUGGESTION) instead of '$WRONGWORD' (= $EXPLANATION_WRONGWORD)?"
    • getMessage

      private String getMessage(String wrongWord, String suggestion, String explanationSuggestion, String explanationWrongWord)
    • loadContextWords

      private static List<WrongWordInContextRule.ContextWords> loadContextWords(String path)
      Load words, contexts, and explanations.