{"record":{"id":"4e4c94781d44809c","repo":"stanfordnlp/CoreNLP","slug":"conditionallogprobgivenprevious-requires-given-one","errorCode":null,"errorMessage":"conditionalLogProbGivenPrevious requires given one less than clique size ( ) but was ","messagePattern":"conditionalLogProbGivenPrevious 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":242,"sourceCode":"\n  public double logProb(int[] label) {\n    return unnormalizedLogProb(label) - totalMass();\n  }\n\n  public double prob(int[] label) {\n    return Math.exp(unnormalizedLogProb(label) - totalMass());\n  }\n\n  /**\n   * Computes the probability of the tag OF being at the end of the table given\n   * that the previous tag sequence in table is GIVEN. given is at the beginning,\n   * of is at the end.\n   *\n   * @return the probability of the tag OF being at the end of the table\n   */\n  public double conditionalLogProbGivenPrevious(int[] given, int of) {\n    if (given.length != windowSize - 1) {\n      throw new IllegalArgumentException(\"conditionalLogProbGivenPrevious requires given one less than clique size (\" +\n              windowSize + \") but was \" + Arrays.toString(given));\n    }\n    // Note: other similar methods could be optimized like this one, but this is the one the CRF uses....\n    /*\n    int startIndex = indicesFront(given);\n    int numCellsToSum = SloppyMath.intPow(numClasses, windowSize - given.length);\n    double z = ArrayMath.logSum(table, startIndex, startIndex + numCellsToSum);\n    int i = indexOf(given, of);\n    System.err.printf(\"startIndex is %d, numCellsToSum is %d, i is %d (of is %d)%n\", startIndex, numCellsToSum, i, of);\n    */\n    int startIndex = indicesFront(given);\n    double z = ArrayMath.logSum(table, startIndex, startIndex + numClasses);\n    int i = startIndex + of;\n    // System.err.printf(\"startIndex is %d, numCellsToSum is %d, i is %d (of is %d)%n\", startIndex, numClasses, i, of);\n\n    return table[i] - z;\n  }\n","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/crf/FactorTable.java#L224-L260","documentation":"FactorTable models a clique of `windowSize` labels. conditionalLogProbGivenPrevious computes the log probability of the last label given the previous `windowSize-1` labels, and it validates that the `given` array is exactly windowSize-1 long before indexing. Passing any other length means the caller has built the conditioning context for the wrong clique size.","triggerScenarios":"Calling conditionalLogProbGivenPrevious(int[] given, int of) with given.length != windowSize-1 — e.g. passing a full clique of labels, an empty array, or a context built for a different window size.","commonSituations":"Copy-pasting example code written for a different CRF window size (e.g. order-1 vs order-2 CRFs); constructing the `given` array manually with off-by-one length; calling it in unit tests or `main` with hardcoded label arrays.","solutions":["Build `given` with exactly windowSize-1 labels: new int[table.windowSize()-1].","If the code supports multiple window sizes, derive the length from the FactorTable (windowSize) instead of hardcoding.","If you have the full clique, drop the last (or the position you're predicting) element before calling."],"exampleFix":"// before\nint[] given = {0, 1, 2}; // 3 labels for a window of 3\nft.conditionalLogProbGivenPrevious(given, 4);\n\n// after\nint[] given = {1, 2}; // windowSize-1 = 2 labels\nft.conditionalLogProbGivenPrevious(given, 4);","handlingStrategy":"validation","validationCode":"// Java: check before calling\nif (given.length != ft.windowSize - 1)\n  throw new IllegalArgumentException(\"need \" + (ft.windowSize-1) + \" labels, got \" + given.length);\ndouble lp = ft.conditionalLogProbGivenPrevious(given, of);","typeGuard":null,"tryCatchPattern":"// Java\ntry {\n  double lp = ft.conditionalLogProbGivenPrevious(given, of);\n} catch (IllegalArgumentException e) {\n  // rebuild context with windowSize-1 labels and retry\n  int[] fixed = Arrays.copyOf(given, ft.windowSize - 1);\n  double lp = ft.conditionalLogProbGivenPrevious(fixed, of);\n}","preventionTips":["Always derive context length from the FactorTable's windowSize, never hardcode.","Slice label histories with Arrays.copyOfRange(seq, pos-(windowSize-1), pos).","Write a helper method that builds clique contexts for all FactorTable queries."],"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"}