stanfordnlp/CoreNLP · error · IllegalArgumentException

ERROR: negative count dblCount for item item!

Error message

ERROR: negative count dblCount for item item!

What it means

The same validateCounter helper rejects negative counts, since a probability distribution cannot be built from negative frequencies. If any item's count is < 0, an IllegalArgumentException reports the negative value and the item. Good-Turing smoothing assumes non-negative count-of-count statistics, so negatives break the math downstream.

Solutions

  1. Inspect and fix the counter construction so counts are never negative (clip at 0 or fix the subtraction)
  2. Validate with Counters or a loop before calling simpleGoodTuring
  3. If deltas are intended, take absolute values or rebuild the statistic so smoothing operates on valid frequencies

Example fix

// before
Counter<String> c = deltaCounters(c1, c2); // may have negatives
Distribution<String> d = Distribution.simpleGoodTuring(c, totalKeys);
// after
for (String k : new ArrayList<>(c.keySet())) {
  if (c.getCount(k) < 0) c.setCount(k, 0.0);
}
Distribution<String> d = Distribution.simpleGoodTuring(c, totalKeys);
Defensive patterns

Strategy: validation

Validate before calling

for (Map.Entry<E, Double> e : counter.entrySet()) {
  if (e.getValue() != null && e.getValue() < 0) throw new IllegalStateException("negative count for " + e.getKey());
}

Try / catch

try {
  Distribution<E> d = Distribution.simpleGoodTuring(counter, n);
} catch (IllegalArgumentException e) {
  if (e.getMessage().startsWith("ERROR: negative count")) { /* clean counter and retry */ }
}

Prevention

When it happens

Trigger: Calling simpleGoodTuring() on a counter whose entries include negative values, typically produced by subtracting counts, applying weights incorrectly, or ingesting noisy external data.

Common situations: Differencing two counters (e.g. computing count deltas between corpora) yielding negative entries; faulty preprocessing pipelines that scale counts with negative factors.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/stats/Distribution.java:493

      Integer count = (int) Math.round(entry.getValue());
      dist.counter.setCount(item, probsByCount.getCount(count));
    }
    dist.numberOfKeys = numberOfKeys;
    dist.reservedMass = sgt.getProbabilityForUnseen();
    return dist;

  }

  /* Helper to simpleGoodTuringSmoothedCounter() */
  private static <E> void validateCounter(Counter<E> counts) {
    for (Map.Entry<E, Double> entry : counts.entrySet()) {
      E item = entry.getKey();
      Double dblCount = entry.getValue();
      if (dblCount == null) {
        throw new IllegalArgumentException("ERROR: null count for item " + item + "!");
      }
      if (dblCount < 0) {
        throw new IllegalArgumentException("ERROR: negative count " + dblCount + " for item " + item + "!");
      }
    }
  }

  /* Helper to simpleGoodTuringSmoothedCounter() */
  private static <E> Counter<Integer> collectCountCounts(Counter<E> counts) {
    Counter<Integer> cc = new ClassicCounter<>(); // counts of counts
    for (Map.Entry<E, Double> entry : counts.entrySet()) {
      //E item = entry.getKey();
      Integer count = (int) Math.round(entry.getValue());
      cc.incrementCount(count);
    }
    return cc;
  }

  /* Helper to simpleGoodTuringSmoothedCounter() */
  private static int[][] countCounts2IntArrays(Counter<Integer> countCounts) {
    int size = countCounts.size();

View on GitHub (pinned to 1b7edd19c4)