{"record":{"id":"2aaa6163ec54f950","repo":"stanfordnlp/CoreNLP","slug":"error-attempted-to-conditionalize-a-generalized","errorCode":null,"errorMessage":"Error -- attempted to conditionalize a GeneralizedCounter of depth depth() on a vector of length n","messagePattern":"Error -- attempted to conditionalize a GeneralizedCounter of depth depth\\(\\) on a vector of length n","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/GeneralizedCounter.java","lineNumber":409,"sourceCode":"      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;\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);","sourceCodeStart":391,"sourceCodeEnd":427,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/GeneralizedCounter.java#L391-L427","documentation":"conditionalize(List l) conditions a GeneralizedCounter on a sequence of context keys; the list length must be strictly less than the counter's depth, since each conditioned key consumes one level of nesting and at least one level must remain. If n >= depth(), the operation cannot leave a usable sub-counter and a RuntimeException reporting both depth and list size is thrown.","triggerScenarios":"Calling gc.conditionalize(list) where list.size() >= gc.depth(), e.g. passing a full context of length equal to the table's order into a trigram (depth 3) table.","commonSituations":"Off-by-one in n-gram code: passing the full n-gram history including the predicted word instead of only its preceding context; reusing one conditionalize call count across tables of different depth.","solutions":["Trim the context list so its size is strictly less than depth(): l = l.subList(0, depth() - 1) or fewer","Check l.size() < gc.depth() before calling","Use conditionalizeOnce for single-key conditioning and verify depth afterwards"],"exampleFix":"// before\nList<String> ctx = Arrays.asList(\"w1\", \"w2\"); // depth-2 gc\nGeneralizedCounter<String> sub = gc.conditionalize(ctx); // n == depth -> throws\n// after\nList<String> ctx = Arrays.asList(\"w2\"); // n < depth\nGeneralizedCounter<String> sub = gc.conditionalize(ctx);","handlingStrategy":"validation","validationCode":"if (l.size() >= gc.depth()) {\n  throw new IllegalArgumentException(\"context length \" + l.size() + \" must be < depth \" + gc.depth());\n}","typeGuard":null,"tryCatchPattern":"try {\n  GeneralizedCounter<K> sub = gc.conditionalize(l);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"on a vector of length\")) { /* trim context list and retry */ }\n}","preventionTips":["Pass only the history part of an n-gram, excluding the predicted token","Assert l.size() < gc.depth() in n-gram helpers","Reuse a single conditioning utility that truncates context defensively"],"tags":["java","generalized-counter","conditionalize","argument-length"],"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"}