{"record":{"id":"bf115547b9d9aba7","repo":"languagetool-org/languagetool","slug":"probability-must-be-0","errorCode":null,"errorMessage":"Probability must be >= 0: ","messagePattern":"Probability must be >= 0: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"languagetool-core/src/main/java/org/languagetool/rules/ngrams/Probability.java","lineNumber":33,"sourceCode":" * License along with this library; if not, write to the Free Software\n * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301\n * USA\n */\npackage org.languagetool.rules.ngrams;\n\n/**\n * Probability, but doesn't check for a range of 0 to 1.\n * @since 3.2\n */\npublic class Probability {\n\n  private final double prob;\n  private final float coverage;\n  private final long occurrences;\n\n  public Probability(double prob, float coverage, long occurrences) {\n    if (prob < 0.0) {\n      throw new RuntimeException(\"Probability must be >= 0: \" + prob);\n    }\n    //if (prob > 1.0) {\n    //  throw new RuntimeException(\"Probability must be 0.0 to 1.0: \" + prob);\n    //}\n    this.prob = prob;\n    this.coverage = coverage;\n    this.occurrences = occurrences;\n  }\n\n  public Probability(double prob, float coverage) {\n    this(prob, coverage, -1);\n  }\n\n  /**\n   * A probability-like value, but can be filled with any float &gt;= 0.\n   */\n  public double getProb() {\n    return prob;","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/languagetool-org/languagetool/blob/2e990059ce67d5e2a0f7f7ca5d31160c6709df4b/languagetool-core/src/main/java/org/languagetool/rules/ngrams/Probability.java#L15-L51","documentation":"Probability is the value object wrapping an n-gram match probability. Its constructor validates that prob is non-negative and throws if a negative probability is passed, since probabilities below 0 are mathematically invalid and indicate an upstream computation error.","triggerScenarios":"Constructing new Probability(...) with a negative prob value — typically a double returned by LanguageModelUtils n-gram lookups (e.g. from log-space conversions or a misbehaving language model) that came back negative.","commonSituations":"Custom language model implementations returning negative values, interpolation/smoothing bugs producing negative interpolated probabilities, or unit tests feeding invalid data.","solutions":["Clamp the computed probability with Math.max(0.0, prob) before constructing Probability","Inspect the language-model lookup that produced the negative value (log-to-linear conversion, interpolation weights)","Update to a fixed LanguageModelUtils/lucene-index version if the negative value comes from library code","Fix test data if the negative probability originates from a mock or fixture"],"exampleFix":"// before\nreturn new Probability(prob, coverage, occurrences);\n// after\nreturn new Probability(Math.max(0.0, prob), coverage, occurrences);","handlingStrategy":"try-catch","validationCode":"if (prob < 0.0) {\n  throw new IllegalArgumentException(\"prob must be >= 0\");\n}","typeGuard":"boolean isValidProbability(double p) {\n  return !Double.isNaN(p) && p >= 0.0;\n}","tryCatchPattern":"try {\n  return new Probability(prob, coverage, occurrences);\n} catch (RuntimeException e) {\n  if (e.getMessage().startsWith(\"Probability must be >= 0\")) {\n    return new Probability(0.0, coverage, occurrences);\n  }\n  throw e;\n}","preventionTips":["Clamp model outputs with Math.max(0.0, p) before constructing Probability","Check log-to-probability conversions for sign errors","Validate custom LanguageModel implementations return non-negative values"],"tags":["probability","validation","ngram","invariant"],"backgroundTag":"invalid-argument-value","analyzedSha":"2e990059ce67d5e2a0f7f7ca5d31160c6709df4b","analyzedAt":"2026-09-06T09:20:17.015Z","contentChangedAt":"2026-09-06T09:20:17.015Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}