{"record":{"id":"28671335eef6c5c4","repo":"languagetool-org/languagetool","slug":"grams-must-be-between-1-and-5","errorCode":null,"errorMessage":"grams must be between 1 and 5: ","messagePattern":"grams must be between 1 and 5: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"languagetool-core/src/main/java/org/languagetool/rules/ngrams/ConfusionProbabilityRule.java","lineNumber":109,"sourceCode":"   * @since 4.7\n   */\n  public ConfusionProbabilityRule(ResourceBundle messages, LanguageModel languageModel, Language language, int grams, List<String> exceptions) {\n    this(messages, languageModel, language, grams, exceptions, Collections.emptyList());\n  }\n\n  public ConfusionProbabilityRule(ResourceBundle messages, LanguageModel languageModel, Language language, int grams,\n                                  List<String> exceptions, List<List<PatternToken>> antiPatterns) {\n    super(messages);\n    setCategory(Categories.TYPOS.getCategory(messages));\n    setLocQualityIssueType(ITSIssueType.NonConformance);\n    for (String filename : getFilenames()) {\n      String path = \"/\" + language.getShortCode() + \"/\" + filename;\n      this.wordToPairs.putAll(confSetCache.getUnchecked(new PathAndLanguage(path, language)));\n    }\n    this.lm = Objects.requireNonNull(languageModel);\n    this.language = Objects.requireNonNull(language);\n    if (grams < 1 || grams > 5) {\n      throw new IllegalArgumentException(\"grams must be between 1 and 5: \" + grams);\n    }\n    this.grams = grams;\n    this.exceptions = exceptions;\n    this.antiPatterns = makeAntiPatterns(antiPatterns, language);\n  }\n\n  @NotNull\n  protected List<String> getFilenames() {\n    return Arrays.asList(\"confusion_sets.txt\");\n  }\n\n  @Override\n  public String getId() {\n    return RULE_ID;\n  }\n\n  @Override\n  public int estimateContextForSureMatch() {","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/languagetool-org/languagetool/blob/2e990059ce67d5e2a0f7f7ca5d31160c6709df4b/languagetool-core/src/main/java/org/languagetool/rules/ngrams/ConfusionProbabilityRule.java#L91-L127","documentation":"ConfusionProbabilityRule's constructor validates the n-gram order parameter 'grams' and throws this IllegalArgumentException when it is outside the supported 1..5 range, because the underlying language model lookup logic only supports up to 5-grams.","triggerScenarios":"Constructing ConfusionProbabilityRule with grams = 0, negative, or > 5.","commonSituations":"Tuning n-gram order in custom confusion-rule setups for languages without a 5-gram model; passing a config value or CLI flag unvalidated; confusing n-gram order with window size.","solutions":["Pass a grams value between 1 and 5 (typically 3 for trigram models).","Clamp or validate the value before constructing the rule, e.g. Math.max(1, Math.min(5, grams)).","Check where grams comes from (config file/flag) and fix the source value."],"exampleFix":"// before\nnew ConfusionProbabilityRule(messages, lm, language, grams, exceptions, antiPatterns);\n// after\nint safeGrams = Math.max(1, Math.min(5, grams));\nnew ConfusionProbabilityRule(messages, lm, language, safeGrams, exceptions, antiPatterns);","handlingStrategy":"validation","validationCode":"// clamp grams before constructing the rule\nif (grams < 1 || grams > 5)\n    throw new IllegalArgumentException(\"grams must be 1..5, got \" + grams);\nint safeGrams = Math.max(1, Math.min(5, grams));","typeGuard":null,"tryCatchPattern":"try { new ConfusionProbabilityRule(msgs, lm, lang, grams, exc, ap); } catch (IllegalArgumentException e) { log.error(\"Config error: \" + e.getMessage()); }","preventionTips":["Centralize grams as a validated constant","Validate external config/flags before rule construction","Use 3 for typical trigram-based models"],"tags":["java","ngrams","argument-validation","languagetool"],"backgroundTag":"value-out-of-range","analyzedSha":"2e990059ce67d5e2a0f7f7ca5d31160c6709df4b","analyzedAt":"2026-09-06T09:20:17.015Z","contentChangedAt":"2026-09-06T09:20:17.015Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}