{"record":{"id":"05f84d8e1f5070a0","repo":"stanfordnlp/CoreNLP","slug":"r-must-have-size-min-input","errorCode":null,"errorMessage":"r must have size >= ${MIN_INPUT}!","messagePattern":"r must have size >= (.+?)!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/SimpleGoodTuring.java","lineNumber":50,"sourceCode":"  private double[] logR;\n  private double[] logZ;\n  private double[] rStar;\n  private double[] p;\n\n  /**\n   * Each instance of this class encapsulates the computation of the smoothing\n   * for one probability distribution.  The constructor takes two arguments\n   * which are two parallel arrays.  The first is an array of counts, which must\n   * be positive and in ascending order.  The second is an array of\n   * corresponding counts of counts; that is, for each i, n[i] represents the\n   * number of types which occurred with count r[i] in the underlying\n   * collection.  See the documentation for main() for a concrete example.\n   */\n  public SimpleGoodTuring(int[] r, int[] n) {\n    if (r == null) throw new IllegalArgumentException(\"r must not be null!\");\n    if (n == null) throw new IllegalArgumentException(\"n must not be null!\");\n    if (r.length != n.length) throw new IllegalArgumentException(\"r and n must have same size!\");\n    if (r.length < MIN_INPUT) throw new IllegalArgumentException(\"r must have size >= \" + MIN_INPUT + \"!\");\n    this.r = new int[r.length];\n    this.n = new int[n.length];\n    System.arraycopy(r, 0, this.r, 0, r.length); // defensive copy\n    System.arraycopy(n, 0, this.n, 0, n.length); // defensive copy\n    this.rows = r.length;\n    compute();\n    validate(TOLERANCE);\n  }\n\n  /**\n   * Returns the probability allocated to types not seen in the underlying\n   * collection.\n   */\n  public double getProbabilityForUnseen() {\n    return pZero;\n  }\n\n  /**","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/SimpleGoodTuring.java#L32-L68","documentation":"SimpleGoodTuring's constructor requires the frequency-of-frequency arrays r and n to contain at least MIN_INPUT rows for the smoothing algorithm to be meaningful. If r is shorter than MIN_INPUT, the constructor refuses to build the model and throws IllegalArgumentException before any computation. Simple Good Turing smoothing needs enough distinct frequency counts to fit a regression.","triggerScenarios":"Calling new SimpleGoodTuring(r, n) with an int[] r whose length is below MIN_INPUT (e.g., a tiny sample with only 1-4 distinct counts), while r and n are non-null and of equal length (earlier checks passed).","commonSituations":"Developers feeding very small corpora or toy frequency tables into the smoother, or building r/n arrays programmatically from a small dataset where few distinct token frequencies occur.","solutions":["Collect more data so you have at least MIN_INPUT distinct frequency-of-frequency pairs before constructing SimpleGoodTuring.","Check r.length >= SimpleGoodTuring.MIN_INPUT (read the constant from the class) before constructing, and handle the too-small case with a simpler smoothing method.","If you genuinely have fewer data points, use a different smoothing algorithm instead of SGT."],"exampleFix":"// before\nSimpleGoodTuring sgt = new SimpleGoodTuring(rCounts, nCounts);\n// after\nif (rCounts.length < 5) { // at least MIN_INPUT entries\n  throw new IllegalArgumentException(\"Need at least \" + 5 + \" frequency pairs for SGT, got \" + rCounts.length);\n}\nSimpleGoodTuring sgt = new SimpleGoodTuring(rCounts, nCounts);","handlingStrategy":"validation","validationCode":"// Java\nclass SimpleGoodTuring {\n  private static final int MIN_INPUT = 5; // check the class constant\n  public static boolean canBuildSGT(int[] r) {\n    return r != null && r.length >= MIN_INPUT;\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always verify r.length >= MIN_INPUT before constructing the smoother.","For small samples, plan a fallback smoother (e.g., add-one) in advance.","Compute frequency-of-frequency counts from the full corpus, not a subsample."],"tags":["nlp","smoothing","input-validation"],"backgroundTag":"argument-out-of-range","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}