Class AgreementRule


  • public class AgreementRule
    extends org.languagetool.rules.Rule
    Simple agreement checker for German noun phrases. Checks agreement in:
    • DET/PRO NOUN: e.g. "mein Auto", "der Mann", "die Frau" (correct), "die Haus" (incorrect)
    • DET/PRO ADJ NOUN: e.g. "der riesige Tisch" (correct), "die riesigen Tisch" (incorrect)
    Note that this rule only checks agreement inside the noun phrase, not whether e.g. the correct case is used. For example, "Es ist das Haus dem Mann" is not detected as incorrect.

    TODO: the implementation could use a re-write that first detects the relevant noun phrases and then checks agreement

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.List<java.util.List<org.languagetool.rules.patterns.PatternToken>> ANTI_PATTERNS  
      private static org.languagetool.AnalyzedToken[] INS_REPLACEMENT  
      private German language  
      private org.languagetool.JLanguageTool lt  
      private static java.util.Set<java.lang.String> MODIFIERS  
      private static java.util.Set<java.lang.String> NOUNS_TO_BE_IGNORED  
      private static java.util.Set<java.lang.String> PRONOUNS_TO_BE_IGNORED  
      private static java.lang.String[] REL_PRONOUN_LEMMAS  
      private static java.util.Set<java.lang.String> VIELE_WENIGE_LOWERCASE  
      private static org.languagetool.AnalyzedToken[] ZUR_REPLACEMENT  
      • Fields inherited from class org.languagetool.rules.Rule

        messages
    • Constructor Summary

      Constructors 
      Constructor Description
      AgreementRule​(java.util.ResourceBundle messages, German language)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private boolean agreementWithCategoryRelaxation​(org.languagetool.AnalyzedTokenReadings token1, org.languagetool.AnalyzedTokenReadings token2, AgreementRule.GrammarCategory categoryToRelax)  
      private org.languagetool.rules.RuleMatch checkDetAdjNounAgreement​(org.languagetool.AnalyzedTokenReadings token1, org.languagetool.AnalyzedTokenReadings token2, org.languagetool.AnalyzedTokenReadings token3, org.languagetool.AnalyzedSentence sentence, int tokenPos)  
      private @Nullable org.languagetool.rules.RuleMatch checkDetNounAgreement​(org.languagetool.AnalyzedTokenReadings token1, org.languagetool.AnalyzedTokenReadings token2, org.languagetool.AnalyzedSentence sentence, int tokenPos)  
      private boolean couldBeRelativeOrDependentClause​(org.languagetool.AnalyzedTokenReadings[] tokens, int pos)  
      int estimateContextForSureMatch()  
      private java.util.Set<java.lang.String> getAgreementCategories​(org.languagetool.AnalyzedTokenReadings aToken)  
      java.util.List<org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule> getAntiPatterns()  
      private java.util.List<java.lang.String> getCategoriesCausingError​(org.languagetool.AnalyzedTokenReadings token1, org.languagetool.AnalyzedTokenReadings token2)  
      private @Nullable org.languagetool.rules.RuleMatch getCompoundError​(org.languagetool.AnalyzedTokenReadings token1, org.languagetool.AnalyzedTokenReadings token2, int tokenPos, org.languagetool.AnalyzedSentence sentence)  
      private @Nullable org.languagetool.rules.RuleMatch getCompoundError​(org.languagetool.AnalyzedTokenReadings token1, org.languagetool.AnalyzedTokenReadings token2, org.languagetool.AnalyzedTokenReadings token3, int tokenPos, org.languagetool.AnalyzedSentence sentence)  
      java.lang.String getDescription()  
      java.lang.String getId()  
      private int getPosAfterModifier​(int startAt, org.languagetool.AnalyzedTokenReadings[] tokens)
      Search for modifiers (such as "sehr", "1,4 Meter") which can expand a determiner - adjective - noun group ("ein hohes Haus" -> "ein sehr hohes Haus", "ein 500 Meter hohes Haus") and return the index of the first non-modifier token ("Haus")
      private @Nullable org.languagetool.rules.RuleMatch getRuleMatch​(org.languagetool.AnalyzedTokenReadings token1, org.languagetool.AnalyzedSentence sentence, org.languagetool.AnalyzedTokenReadings nextToken, java.lang.String testPhrase, java.lang.String hyphenTestPhrase)  
      private void initLt()  
      private boolean isException​(org.languagetool.AnalyzedTokenReadings token1, org.languagetool.AnalyzedTokenReadings token2)  
      private boolean isNonPredicativeAdjective​(org.languagetool.AnalyzedTokenReadings tokensReadings)  
      private boolean isParticiple​(org.languagetool.AnalyzedTokenReadings tokensReadings)  
      private boolean isRelevantPronoun​(org.languagetool.AnalyzedTokenReadings[] tokens, int pos)  
      org.languagetool.rules.RuleMatch[] match​(org.languagetool.AnalyzedSentence sentence)  
      private void replacePrepositionsByArticle​(org.languagetool.AnalyzedTokenReadings[] tokens)  
      private @NotNull java.util.Set<java.lang.String> retainCommonCategories​(org.languagetool.AnalyzedTokenReadings token1, org.languagetool.AnalyzedTokenReadings token2, org.languagetool.AnalyzedTokenReadings token3)  
      • Methods inherited from class org.languagetool.rules.Rule

        addExamplePair, getCategory, getConfigureText, getCorrectExamples, getDefaultValue, getErrorTriggeringExamples, getIncorrectExamples, getLocQualityIssueType, getMaxConfigurableValue, getMinConfigurableValue, getSentenceWithImmunization, getUrl, hasConfigurableValue, isDefaultOff, isDefaultTempOff, isDictionaryBasedSpellingRule, isOfficeDefaultOff, isOfficeDefaultOn, makeAntiPatterns, setCategory, setCorrectExamples, setDefaultOff, setDefaultOn, setDefaultTempOff, setErrorTriggeringExamples, setIncorrectExamples, setLocQualityIssueType, setOfficeDefaultOff, setOfficeDefaultOn, setUrl, supportsLanguage, toRuleMatchArray, useInOffice
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • language

        private final German language
      • lt

        private org.languagetool.JLanguageTool lt
      • INS_REPLACEMENT

        private static final org.languagetool.AnalyzedToken[] INS_REPLACEMENT
      • ZUR_REPLACEMENT

        private static final org.languagetool.AnalyzedToken[] ZUR_REPLACEMENT
      • ANTI_PATTERNS

        private static final java.util.List<java.util.List<org.languagetool.rules.patterns.PatternToken>> ANTI_PATTERNS
      • MODIFIERS

        private static final java.util.Set<java.lang.String> MODIFIERS
      • VIELE_WENIGE_LOWERCASE

        private static final java.util.Set<java.lang.String> VIELE_WENIGE_LOWERCASE
      • REL_PRONOUN_LEMMAS

        private static final java.lang.String[] REL_PRONOUN_LEMMAS
      • PRONOUNS_TO_BE_IGNORED

        private static final java.util.Set<java.lang.String> PRONOUNS_TO_BE_IGNORED
      • NOUNS_TO_BE_IGNORED

        private static final java.util.Set<java.lang.String> NOUNS_TO_BE_IGNORED
    • Constructor Detail

      • AgreementRule

        public AgreementRule​(java.util.ResourceBundle messages,
                             German language)
    • Method Detail

      • getId

        public java.lang.String getId()
        Specified by:
        getId in class org.languagetool.rules.Rule
      • estimateContextForSureMatch

        public int estimateContextForSureMatch()
        Overrides:
        estimateContextForSureMatch in class org.languagetool.rules.Rule
      • getDescription

        public java.lang.String getDescription()
        Specified by:
        getDescription in class org.languagetool.rules.Rule
      • replacePrepositionsByArticle

        private void replacePrepositionsByArticle​(org.languagetool.AnalyzedTokenReadings[] tokens)
      • match

        public org.languagetool.rules.RuleMatch[] match​(org.languagetool.AnalyzedSentence sentence)
        Specified by:
        match in class org.languagetool.rules.Rule
      • getPosAfterModifier

        private int getPosAfterModifier​(int startAt,
                                        org.languagetool.AnalyzedTokenReadings[] tokens)
        Search for modifiers (such as "sehr", "1,4 Meter") which can expand a determiner - adjective - noun group ("ein hohes Haus" -> "ein sehr hohes Haus", "ein 500 Meter hohes Haus") and return the index of the first non-modifier token ("Haus")
        Parameters:
        startAt - index of array where to start searching for modifier
        Returns:
        index of first non-modifier token
      • getAntiPatterns

        public java.util.List<org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule> getAntiPatterns()
        Overrides:
        getAntiPatterns in class org.languagetool.rules.Rule
      • isNonPredicativeAdjective

        private boolean isNonPredicativeAdjective​(org.languagetool.AnalyzedTokenReadings tokensReadings)
      • isParticiple

        private boolean isParticiple​(org.languagetool.AnalyzedTokenReadings tokensReadings)
      • isRelevantPronoun

        private boolean isRelevantPronoun​(org.languagetool.AnalyzedTokenReadings[] tokens,
                                          int pos)
      • couldBeRelativeOrDependentClause

        private boolean couldBeRelativeOrDependentClause​(org.languagetool.AnalyzedTokenReadings[] tokens,
                                                         int pos)
      • checkDetNounAgreement

        @Nullable
        private @Nullable org.languagetool.rules.RuleMatch checkDetNounAgreement​(org.languagetool.AnalyzedTokenReadings token1,
                                                                                 org.languagetool.AnalyzedTokenReadings token2,
                                                                                 org.languagetool.AnalyzedSentence sentence,
                                                                                 int tokenPos)
      • getCompoundError

        @Nullable
        private @Nullable org.languagetool.rules.RuleMatch getCompoundError​(org.languagetool.AnalyzedTokenReadings token1,
                                                                            org.languagetool.AnalyzedTokenReadings token2,
                                                                            int tokenPos,
                                                                            org.languagetool.AnalyzedSentence sentence)
      • getCompoundError

        @Nullable
        private @Nullable org.languagetool.rules.RuleMatch getCompoundError​(org.languagetool.AnalyzedTokenReadings token1,
                                                                            org.languagetool.AnalyzedTokenReadings token2,
                                                                            org.languagetool.AnalyzedTokenReadings token3,
                                                                            int tokenPos,
                                                                            org.languagetool.AnalyzedSentence sentence)
      • getRuleMatch

        @Nullable
        private @Nullable org.languagetool.rules.RuleMatch getRuleMatch​(org.languagetool.AnalyzedTokenReadings token1,
                                                                        org.languagetool.AnalyzedSentence sentence,
                                                                        org.languagetool.AnalyzedTokenReadings nextToken,
                                                                        java.lang.String testPhrase,
                                                                        java.lang.String hyphenTestPhrase)
      • initLt

        private void initLt()
      • isException

        private boolean isException​(org.languagetool.AnalyzedTokenReadings token1,
                                    org.languagetool.AnalyzedTokenReadings token2)
      • getCategoriesCausingError

        private java.util.List<java.lang.String> getCategoriesCausingError​(org.languagetool.AnalyzedTokenReadings token1,
                                                                           org.languagetool.AnalyzedTokenReadings token2)
      • checkDetAdjNounAgreement

        private org.languagetool.rules.RuleMatch checkDetAdjNounAgreement​(org.languagetool.AnalyzedTokenReadings token1,
                                                                          org.languagetool.AnalyzedTokenReadings token2,
                                                                          org.languagetool.AnalyzedTokenReadings token3,
                                                                          org.languagetool.AnalyzedSentence sentence,
                                                                          int tokenPos)
      • agreementWithCategoryRelaxation

        private boolean agreementWithCategoryRelaxation​(org.languagetool.AnalyzedTokenReadings token1,
                                                        org.languagetool.AnalyzedTokenReadings token2,
                                                        AgreementRule.GrammarCategory categoryToRelax)
      • retainCommonCategories

        @NotNull
        private @NotNull java.util.Set<java.lang.String> retainCommonCategories​(org.languagetool.AnalyzedTokenReadings token1,
                                                                                org.languagetool.AnalyzedTokenReadings token2,
                                                                                org.languagetool.AnalyzedTokenReadings token3)
      • getAgreementCategories

        private java.util.Set<java.lang.String> getAgreementCategories​(org.languagetool.AnalyzedTokenReadings aToken)