{"record":{"id":"7afb0b1455ff33e6","repo":"stanfordnlp/CoreNLP","slug":"tried-to-compare-two-distribution-k-objects-but-d","errorCode":null,"errorMessage":"Tried to compare two Distribution<K> objects but d1.numberOfKeys != d2.numberOfKeys","messagePattern":"Tried to compare two Distribution<K> objects but d1\\.numberOfKeys != d2\\.numberOfKeys","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/Distributions.java","lineNumber":26,"sourceCode":" * Static methods for operating on {@link Distributions}s.\n *\n * In general, if a method is operating on a pair of Distribution objects, we imagine that the\n * set of possible keys for each Distribution is the same.\n * Therefore we require that d1.numberOFKeys = d2.numberOfKeys and that the number of keys in the union\n * of the two key sets &lt;= numKeys\n *\n *\n * @author Jeff Michels (jmichels@stanford.edu)\n */\npublic class Distributions {\n\n  private Distributions() {\n  }\n\n\n  protected static <K> Set<K> getSetOfAllKeys(Distribution<K> d1, Distribution<K> d2) {\n    if (d1.getNumberOfKeys() != d2.getNumberOfKeys()){\n      throw new RuntimeException(\"Tried to compare two Distribution<K> objects but d1.numberOfKeys != d2.numberOfKeys\");\n    }\n\n    Set<K> allKeys = Generics.newHashSet(d1.getCounter().keySet());\n    allKeys.addAll(d2.getCounter().keySet());\n    if (allKeys.size() > d1.getNumberOfKeys()){\n      throw new RuntimeException(\"Tried to compare two Distribution<K> objects but d1.counter intersect d2.counter > numberOfKeys\");\n    }\n    return allKeys;\n  }\n\n  /**\n   * Returns a double between 0 and 1 representing the overlap of d1 and d2.\n   * Equals 0 if there is no overlap, equals 1 iff d1==d2\n   */\n  public static <K> double overlap(Distribution<K> d1, Distribution<K> d2) {\n    Set<K> allKeys = getSetOfAllKeys(d1, d2);\n\n    double result = 0.0;","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/Distributions.java#L8-L44","documentation":"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.","triggerScenarios":"Calling overlap, intersection, or related Distributions utilities (which route through getSetOfAllKeys via allKeys) on two Distribution objects built with different numberOfKeys values.","commonSituations":"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.","solutions":["Rebuild both distributions with the same numberOfKeys (same universe size)","Verify numberOfKeys with d.getNumberOfKeys() on both sides before comparing","If the key spaces genuinely differ, compute the key union manually instead of using Distributions comparison helpers"],"exampleFix":"// before\nDistribution<String> d1 = Distribution.getDistribution(c1); // numberOfKeys implicit\nDistribution<String> d2 = Distribution.getDistribution(c2);\n// after\nint n = Math.max(totalKeys1, totalKeys2);\nd1.setNumberOfKeys(n); d2.setNumberOfKeys(n); // or rebuild both with same n\ndouble overlap = Distributions.overlap(d1, d2);","handlingStrategy":"validation","validationCode":"if (d1.getNumberOfKeys() != d2.getNumberOfKeys()) {\n  throw new IllegalArgumentException(\"distributions must share numberOfKeys\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  Set<K> keys = Distributions.allKeys(d1, d2);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"numberOfKeys !=\")) { /* rebuild distributions consistently */ }\n}","preventionTips":["Construct all compared distributions from the same key universe","Assert equal getNumberOfKeys() before any Distributions comparison helper","Centralize distribution construction so numberOfKeys is derived once"],"tags":["java","distribution-comparison","key-count","statistics"],"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-15T23:17:13.987Z"}