stanfordnlp/CoreNLP · error · IllegalArgumentException

r must have size >= !

Error message

r must have size >= ${MIN_INPUT}!

What it means

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.

Solutions

  1. Collect more data so you have at least MIN_INPUT distinct frequency-of-frequency pairs before constructing SimpleGoodTuring.
  2. 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.
  3. If you genuinely have fewer data points, use a different smoothing algorithm instead of SGT.

Example fix

// before
SimpleGoodTuring sgt = new SimpleGoodTuring(rCounts, nCounts);
// after
if (rCounts.length < 5) { // at least MIN_INPUT entries
  throw new IllegalArgumentException("Need at least " + 5 + " frequency pairs for SGT, got " + rCounts.length);
}
SimpleGoodTuring sgt = new SimpleGoodTuring(rCounts, nCounts);
Defensive patterns

Strategy: validation

Validate before calling

// Java
class SimpleGoodTuring {
  private static final int MIN_INPUT = 5; // check the class constant
  public static boolean canBuildSGT(int[] r) {
    return r != null && r.length >= MIN_INPUT;
  }
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/05f84d8e1f5070a0. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/stats/SimpleGoodTuring.java:50

  private double[] logR;
  private double[] logZ;
  private double[] rStar;
  private double[] p;

  /**
   * Each instance of this class encapsulates the computation of the smoothing
   * for one probability distribution.  The constructor takes two arguments
   * which are two parallel arrays.  The first is an array of counts, which must
   * be positive and in ascending order.  The second is an array of
   * corresponding counts of counts; that is, for each i, n[i] represents the
   * number of types which occurred with count r[i] in the underlying
   * collection.  See the documentation for main() for a concrete example.
   */
  public SimpleGoodTuring(int[] r, int[] n) {
    if (r == null) throw new IllegalArgumentException("r must not be null!");
    if (n == null) throw new IllegalArgumentException("n must not be null!");
    if (r.length != n.length) throw new IllegalArgumentException("r and n must have same size!");
    if (r.length < MIN_INPUT) throw new IllegalArgumentException("r must have size >= " + MIN_INPUT + "!");
    this.r = new int[r.length];
    this.n = new int[n.length];
    System.arraycopy(r, 0, this.r, 0, r.length); // defensive copy
    System.arraycopy(n, 0, this.n, 0, n.length); // defensive copy
    this.rows = r.length;
    compute();
    validate(TOLERANCE);
  }

  /**
   * Returns the probability allocated to types not seen in the underlying
   * collection.
   */
  public double getProbabilityForUnseen() {
    return pZero;
  }

  /**

View on GitHub (pinned to 1b7edd19c4)