stanfordnlp/CoreNLP · error · RuntimeException

Tried to compare two Distribution

Error message

Tried to compare two Distribution<K> objects but d1.numberOfKeys != d2.numberOfKeys

What it means

Distributions.getSetOfAllKeys() unions the key sets of two distributions that are assumed to be defined over the same key space. If d1.getNumberOfKeys() != d2.getNumberOfKeys(), the two distributions cover different universes and a comparison is meaningless, so a RuntimeException is thrown before computing the union.

Solutions

  1. Rebuild both distributions with the same numberOfKeys (same universe size)
  2. Verify numberOfKeys with d.getNumberOfKeys() on both sides before comparing
  3. If the key spaces genuinely differ, compute the key union manually instead of using Distributions comparison helpers

Example fix

// before
Distribution<String> d1 = Distribution.getDistribution(c1); // numberOfKeys implicit
Distribution<String> d2 = Distribution.getDistribution(c2);
// after
int n = Math.max(totalKeys1, totalKeys2);
d1.setNumberOfKeys(n); d2.setNumberOfKeys(n); // or rebuild both with same n
double overlap = Distributions.overlap(d1, d2);
Defensive patterns

Strategy: validation

Validate before calling

if (d1.getNumberOfKeys() != d2.getNumberOfKeys()) {
  throw new IllegalArgumentException("distributions must share numberOfKeys");
}

Try / catch

try {
  Set<K> keys = Distributions.allKeys(d1, d2);
} catch (RuntimeException e) {
  if (e.getMessage().contains("numberOfKeys !=")) { /* rebuild distributions consistently */ }
}

Prevention

When it happens

Trigger: Calling overlap, intersection, or related Distributions utilities (which route through getSetOfAllKeys via allKeys) on two Distribution objects built with different numberOfKeys values.

Common situations: Comparing a distribution learned from one corpus/vocabulary with one from another where the total key counts differ; reusing a stale numberOfKeys after adding vocabulary.

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/7afb0b1455ff33e6. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/stats/Distributions.java:26

 * Static methods for operating on {@link Distributions}s.
 *
 * In general, if a method is operating on a pair of Distribution objects, we imagine that the
 * set of possible keys for each Distribution is the same.
 * Therefore we require that d1.numberOFKeys = d2.numberOfKeys and that the number of keys in the union
 * of the two key sets &lt;= numKeys
 *
 *
 * @author Jeff Michels (jmichels@stanford.edu)
 */
public class Distributions {

  private Distributions() {
  }


  protected static <K> Set<K> getSetOfAllKeys(Distribution<K> d1, Distribution<K> d2) {
    if (d1.getNumberOfKeys() != d2.getNumberOfKeys()){
      throw new RuntimeException("Tried to compare two Distribution<K> objects but d1.numberOfKeys != d2.numberOfKeys");
    }

    Set<K> allKeys = Generics.newHashSet(d1.getCounter().keySet());
    allKeys.addAll(d2.getCounter().keySet());
    if (allKeys.size() > d1.getNumberOfKeys()){
      throw new RuntimeException("Tried to compare two Distribution<K> objects but d1.counter intersect d2.counter > numberOfKeys");
    }
    return allKeys;
  }

  /**
   * Returns a double between 0 and 1 representing the overlap of d1 and d2.
   * Equals 0 if there is no overlap, equals 1 iff d1==d2
   */
  public static <K> double overlap(Distribution<K> d1, Distribution<K> d2) {
    Set<K> allKeys = getSetOfAllKeys(d1, d2);

    double result = 0.0;

View on GitHub (pinned to 1b7edd19c4)