{"record":{"id":"227fa6acb810a43d","repo":"stanfordnlp/CoreNLP","slug":"error-can-t-conditionalize-a-distribution-of-de","errorCode":null,"errorMessage":"Error -- can't conditionalize a distribution of depth 1","messagePattern":"Error -- can't conditionalize a distribution of depth 1","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/GeneralizedCounter.java","lineNumber":397,"sourceCode":"      o = i.next();\n      j++;\n    }\n    counts[depth] = next.getCount(o);\n\n    return counts;\n  }\n\n  /* haven't decided about access for this one yet */\n  private GeneralizedCounter<K> conditionalizeHelper(K o) {\n    if (depth > 1) {\n      GeneralizedCounter<K> next = ErasureUtils.uncheckedCast(map.get(o));\n      if (next == null) // adds a new GeneralizedCounter if needed\n      {\n        map.put(o, (next = new GeneralizedCounter<>(depth - 1)));\n      }\n      return next;\n    } else {\n      throw new RuntimeException(\"Error -- can't conditionalize a distribution of depth 1\");\n    }\n  }\n\n  /**\n   * returns a GeneralizedCounter conditioned on the objects in the\n   * {@link List} argument. The length of the argument {@link List}\n   * must be less than the depth of the GeneralizedCounter.\n   */\n  public GeneralizedCounter<K> conditionalize(List<K> l) {\n    int n = l.size();\n    if (n >= depth()) {\n      throw new RuntimeException(\"Error -- attempted to conditionalize a GeneralizedCounter of depth \" + depth() + \" on a vector of length \" + n);\n    } else {\n      GeneralizedCounter<K> next = this;\n      for (K o: l) {\n        next = next.conditionalizeHelper(o);\n      }\n      return next;","sourceCodeStart":379,"sourceCodeEnd":415,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/GeneralizedCounter.java#L379-L415","documentation":"GeneralizedCounter.conditionalizeHelper() descends one level of the nested counter structure; a depth-1 GeneralizedCounter holds only leaf counts and has no sub-counters to condition on, so conditioning deeper is impossible and a RuntimeException is thrown. Note this exception has no depth value interpolated into the message (unlike its siblings).","triggerScenarios":"Calling conditionalize(List) or conditionalizeOnce(o) on a depth-1 GeneralizedCounter, or chaining conditionalizeOnce enough times to exhaust the depth (each call reduces depth by 1, so the second call on a depth-2 counter fails).","commonSituations":"Building bigram-style tables (depth 2) and accidentally conditioning twice; reusing a counter variable after a prior conditionalizeOnce reduced its depth.","solutions":["Only condition while depth() > 1; check depth() before each conditionalizeOnce call","Keep a reference to the original GeneralizedCounter rather than overwriting it with the conditioned (depth-reduced) result","For depth-1 counters, read counts directly via getCount() instead of conditioning"],"exampleFix":"// before\nGeneralizedCounter<String> gc = ...; // depth 2\ngc = gc.conditionalizeOnce(\"a\");\ngc = gc.conditionalizeOnce(\"b\"); // depth now 1 -> throws\n// after\nGeneralizedCounter<String> cond = gc.conditionalizeOnce(\"a\"); // keep original gc\n// use cond.getCount(Collections.singletonList(\"b\")) for leaf lookups","handlingStrategy":"validation","validationCode":"if (gc.depth() <= 1) {\n  throw new IllegalStateException(\"cannot conditionalize a depth-1 GeneralizedCounter\");\n}","typeGuard":"boolean conditionable(GeneralizedCounter<?> gc) { return gc.depth() > 1; }","tryCatchPattern":"try {\n  GeneralizedCounter<K> sub = gc.conditionalizeOnce(o);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"depth 1\")) { /* use getCount on the leaf instead */ }\n}","preventionTips":["Track depth as you chain conditionalizeOnce calls","Keep the original counter reference; never overwrite with conditioned results","Check depth() before every conditioning call"],"tags":["java","generalized-counter","depth","statistics"],"backgroundTag":"invalid-state-transition","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"}