{"record":{"id":"c66797a741ef57a4","repo":"stanfordnlp/CoreNLP","slug":"r-and-n-must-have-same-size","errorCode":null,"errorMessage":"r and n must have same size!","messagePattern":"r and n must have same size!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/SimpleGoodTuring.java","lineNumber":49,"sourceCode":"  private double[] z;\n  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","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/SimpleGoodTuring.java#L31-L67","documentation":"SimpleGoodTuring requires the r (frequencies) and n (counts of counts) arrays to be parallel — element i of n corresponds to r[i] — so the constructor throws IllegalArgumentException when their lengths differ, preventing index corruption during smoothing.","triggerScenarios":"new SimpleGoodTuring(r, n) with r.length != n.length — e.g. loading the two columns from separate sources where one has extra/missing entries, or filtering one array but not the other.","commonSituations":"Parsing a two-column counts file where blank lines were handled inconsistently; truncating one array; merging histograms from multiple runs and updating only one array.","solutions":["Verify r.length == n.length before constructing; zip them from a single source so they can't diverge","Filter both arrays together (same predicate) instead of independently","Re-derive n directly from r's source so they share one provenance"],"exampleFix":"// before\nSimpleGoodTuring sgt = new SimpleGoodTuring(freqs, counts);\n// after\nif (freqs.length != counts.length) {\n  throw new IllegalArgumentException(\"freqs.length=\" + freqs.length + \" counts.length=\" + counts.length);\n}\nSimpleGoodTuring sgt = new SimpleGoodTuring(freqs, counts);","handlingStrategy":"validation","validationCode":"if (r.length != n.length) throw new IllegalStateException(\"r and n lengths differ: \" + r.length + \" vs \" + n.length);","typeGuard":"boolean sameLength(int[] a, int[] b) { return a != null && b != null && a.length == b.length; }","tryCatchPattern":"try {\n  return new SimpleGoodTuring(r, n);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"same size\")) {\n    int m = Math.min(r.length, n.length);\n    return new SimpleGoodTuring(Arrays.copyOf(r, m), Arrays.copyOf(n, m));\n  }\n  throw e;\n}","preventionTips":["Parse the two-column counts file into paired records before splitting into arrays","Filter r and n with the same predicate/index set","Recompute n from the underlying corpus instead of maintaining it separately"],"tags":["java","argument-validation","smoothing"],"backgroundTag":"invalid-argument-value","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"}