{"record":{"id":"66e75c8a89f92091","repo":"stanfordnlp/CoreNLP","slug":"error-attempted-to-conditionalize-a-generalized-66e75c","errorCode":null,"errorMessage":"Error -- attempted to conditionalize a GeneralizedCounter of depth depth()","messagePattern":"Error -- attempted to conditionalize a GeneralizedCounter of depth depth\\(\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/GeneralizedCounter.java","lineNumber":425,"sourceCode":"    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;\n    }\n  }\n\n  /**\n   * Returns a GeneralizedCounter conditioned on the given top level object.\n   * This is just shorthand (and more efficient) for <code>conditionalize(new Object[] { o })</code>.\n   */\n  public GeneralizedCounter<K> conditionalizeOnce(K o) {\n    if (depth() < 1) {\n      throw new RuntimeException(\"Error -- attempted to conditionalize a GeneralizedCounter of depth \" + depth());\n    } else {\n      return conditionalizeHelper(o);\n    }\n  }\n\n  /**\n   * equivalent to incrementCount(l,o,1.0).\n   */\n  public void incrementCount(List<K> l, K o) {\n    incrementCount(l, o, 1.0);\n  }\n\n  /**\n   * same as incrementCount(List, double) but as if Object o were at the end of the list\n   */\n  public void incrementCount(List<K> l, K o, double count) {\n    if (l.size() != depth - 1) {\n      wrongDepth();","sourceCodeStart":407,"sourceCodeEnd":443,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/GeneralizedCounter.java#L407-L443","documentation":"conditionalizeOnce(o) is shorthand for conditioning on a single top-level key, which requires the GeneralizedCounter to have at least depth 1... but the guard actually rejects depth() < 1 only for the check's phrasing while conditionalizeHelper requires depth > 1 to descend; the thrown RuntimeException reports the current depth(). In effect, calling it on a counter with no remaining levels to condition on fails.","triggerScenarios":"Calling conditionalizeOnce(o) on a GeneralizedCounter whose depth() is too small to condition on a top-level key (depth reduced by prior conditionalize/conditionalizeOnce calls, or a leaf-level counter).","commonSituations":"Iterative smoothing/lookup code that repeatedly calls conditionalizeOnce without tracking remaining depth; passing a flattened (depth-1) counter where a hierarchical one was expected.","solutions":["Guard with if (gc.depth() > 1) before calling conditionalizeOnce, since a depth-1 counter cannot be conditioned further","Track how many conditioning steps you have performed and stop at depth - 1 steps","Use getCount() directly on shallow counters instead of conditioning"],"exampleFix":"// before\nGeneralizedCounter<String> gc = ...;\ngc = gc.conditionalizeOnce(\"a\");\ngc = gc.conditionalizeOnce(\"b\"); // depth exhausted -> throws\n// after\nif (gc.depth() > 1) {\n  gc = gc.conditionalizeOnce(\"b\");\n} else {\n  double c = gc.getCount(Collections.singletonList(\"b\"));\n}","handlingStrategy":"validation","validationCode":"if (gc.depth() < 2) { // helper needs a level to descend into\n  throw new IllegalStateException(\"cannot conditionalizeOnce at depth \" + gc.depth());\n}","typeGuard":"boolean canConditionOnce(GeneralizedCounter<?> gc) { return gc.depth() > 1; }","tryCatchPattern":"try {\n  GeneralizedCounter<K> sub = gc.conditionalizeOnce(o);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"attempted to conditionalize\")) { /* stop conditioning; read leaf counts */ }\n}","preventionTips":["Count conditioning steps and cap at depth - 1","Check depth() > 1 before each conditionalizeOnce","Prefer conditionalize(List) with a bounded context over ad-hoc repeated calls"],"tags":["java","generalized-counter","depth","conditionalize"],"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"}