{"record":{"id":"392ebe3dba444245","repo":"stanfordnlp/CoreNLP","slug":"conditionallogprobsgivenprevious-requires-given-on","errorCode":null,"errorMessage":"conditionalLogProbsGivenPrevious requires given one less than clique size ( ) but was ","messagePattern":"conditionalLogProbsGivenPrevious requires given one less than clique size \\( \\) but was ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/crf/FactorTable.java","lineNumber":291,"sourceCode":"//    int i = indexOf(given, of);\n//    // if (SloppyMath.isDangerous(z) || SloppyMath.isDangerous(table[i])) {\n//    // log.info(\"z=\"+z);\n//    // log.info(\"t=\"+table[i]);\n//    // }\n//\n//    return table[i];\n//  }\n\n  /**\n   * Computes the probabilities of the tag at the end of the table given that\n   * the previous tag sequence in table is GIVEN. given is at the beginning,\n   * position in question is at the end\n   *\n   * @return the probabilities of the tag at the end of the table\n   */\n  public double[] conditionalLogProbsGivenPrevious(int[] given) {\n    if (given.length != windowSize - 1) {\n      throw new IllegalArgumentException(\"conditionalLogProbsGivenPrevious requires given one less than clique size (\" +\n          windowSize + \") but was \" + Arrays.toString(given));\n    }\n    double[] result = new double[numClasses];\n    for (int i = 0; i < numClasses; i++) {\n      result[i] = table[indexOf(given, i)];\n    }\n    ArrayMath.logNormalize(result);\n    return result;\n  }\n\n  /**\n   * Computes the probability of the sequence OF being at the end of the table\n   * given that the first tag in table is GIVEN. given is at the beginning, of is\n   * at the end\n   *\n   * @return the probability of the sequence of being at the end of the table\n   */\n  public double conditionalLogProbGivenFirst(int given, int[] of) {","sourceCodeStart":273,"sourceCodeEnd":309,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/crf/FactorTable.java#L273-L309","documentation":"Like conditionalLogProbGivenPrevious, conditionalLogProbsGivenPrevious (plural — returns the distribution over all classes) requires the conditioning context to be exactly windowSize-1 labels, since a FactorTable of windowSize needs one slot left for the label being predicted. Any other `given` length would index outside the table's logical layout, so it throws IllegalArgumentException immediately.","triggerScenarios":"Calling conditionalLogProbsGivenPrevious(int[] given) with an array whose length is not windowSize-1: full-size clique arrays, empty arrays, or contexts sized from a different window.","commonSituations":"Migrating code between CRF models with different markov orders/window sizes; hand-assembling label histories in tests; an off-by-one when slicing a label sequence.","solutions":["Ensure given.length == windowSize-1, taking windowSize from the FactorTable instance.","When slicing a label sequence for position i, take the windowSize-1 labels immediately preceding i.","Validate the array length yourself before the call and throw a descriptive error."],"exampleFix":"// before\nint[] given = labels; // full sequence, wrong length\ndouble[] probs = ft.conditionalLogProbsGivenPrevious(given);\n\n// after\nint[] given = Arrays.copyOfRange(labels, pos - (ft.windowSize - 1), pos);\ndouble[] probs = ft.conditionalLogProbsGivenPrevious(given);","handlingStrategy":"validation","validationCode":"// Java\nif (given.length != ft.windowSize - 1) {\n  given = Arrays.copyOfRange(labels, pos - (ft.windowSize - 1), pos);\n}\ndouble[] probs = ft.conditionalLogProbsGivenPrevious(given);","typeGuard":null,"tryCatchPattern":"// Java\ntry {\n  result = ft.conditionalLogProbsGivenPrevious(given);\n} catch (IllegalArgumentException e) {\n  // log window-size mismatch, rebuild `given` with correct length\n  result = ft.conditionalLogProbsGivenPrevious(\n      Arrays.copyOf(given, ft.windowSize - 1));\n}","preventionTips":["Centralize context construction in one utility per CRF order.","Unit-test FactorTable queries with arrays of the exact expected length.","When switching models with different orders, audit all query call sites."],"tags":["java","nlp","crf","argument-validation"],"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"}