{"record":{"id":"6a04c6805e0d6187","repo":"stanfordnlp/CoreNLP","slug":"conditionallogprobgivennext-requires-given-one-les","errorCode":null,"errorMessage":"conditionalLogProbGivenNext requires given one less than clique size ( ) but was ","messagePattern":"conditionalLogProbGivenNext 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":360,"sourceCode":"    // compute P(given)\n    // double probGiven = logProbFront(given);\n    // double probGiven = unnormalizedLogProbFront(given);\n\n    // compute P(given, of) / P(given)\n    // return probAll - probGiven;\n    return probAll;\n  }\n\n  /**\n   * Computes the probability of the tag OF being at the beginning of the table\n   * given that the tag sequence GIVEN is at the end of the table. given is at\n   * the end, of is at the beginning\n   *\n   * @return the probability of the tag of being at the beginning of the table\n   */\n  public double conditionalLogProbGivenNext(int[] given, int of) {\n    if (given.length != windowSize - 1) {\n      throw new IllegalArgumentException(\"conditionalLogProbGivenNext requires given one less than clique size (\" +\n          windowSize + \") but was \" + Arrays.toString(given));\n    }\n    int[] label = indicesEnd(given);\n    double[] masses = new double[label.length];\n    for (int i = 0; i < masses.length; i++) {\n      masses[i] = table[label[i]];\n    }\n    return table[indexOf(of, given)] - ArrayMath.logSum(masses);\n  }\n\n  public double unnormalizedLogProbFront(int[] labels) {\n    int startIndex = indicesFront(labels);\n    int numCellsToSum = SloppyMath.intPow(numClasses, windowSize - labels.length);\n    // double[] masses = new double[labels.length];\n    // for (int i = 0; i < masses.length; i++) {\n    //   masses[i] = table[labels[i]];\n    // }\n    return ArrayMath.logSum(table, startIndex, startIndex + numCellsToSum);","sourceCodeStart":342,"sourceCodeEnd":378,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/crf/FactorTable.java#L342-L378","documentation":"conditionalLogProbGivenNext computes the log probability of a label at the END of the window given the NEXT windowSize-1 labels. It validates that `given` has exactly windowSize-1 entries so the given context plus the predicted label fills the clique. Other lengths cannot be mapped onto the table's indices and throw IllegalArgumentException.","triggerScenarios":"Calling conditionalLogProbGivenNext(int[] given, int of) with given.length != windowSize-1 — e.g. passing the whole following sequence instead of the next windowSize-1 labels, or reusing an array sized for another order.","commonSituations":"Bidirectional/backward scanning code over a label sequence that slices the wrong window length; demos/tests (this method is only reached via main per the call graph) with hardcoded label arrays; mixing order-1 and order-2 assumptions.","solutions":["Pass exactly windowSize-1 following labels, taking windowSize from the FactorTable.","When scanning backwards, slice labels from i+1 to i+windowSize (exclusive), yielding windowSize-1 entries.","Validate lengths defensively before calling FactorTable query methods."],"exampleFix":"// before\nint[] given = Arrays.copyOfRange(labels, pos+1, labels.length);\nft.conditionalLogProbGivenNext(given, labels[pos]);\n\n// after\nint[] given = Arrays.copyOfRange(labels, pos+1, pos+1 + (ft.windowSize - 1));\nft.conditionalLogProbGivenNext(given, labels[pos]);","handlingStrategy":"validation","validationCode":"// Java\nint end = Math.min(pos + 1 + (ft.windowSize - 1), labels.length);\nint[] given = Arrays.copyOfRange(labels, pos + 1, end);\nif (given.length != ft.windowSize - 1)\n  throw new IllegalArgumentException(\"insufficient following context\");\ndouble lp = ft.conditionalLogProbGivenNext(given, label);","typeGuard":null,"tryCatchPattern":"// Java\ntry {\n  lp = ft.conditionalLogProbGivenNext(given, of);\n} catch (IllegalArgumentException e) {\n  // rebuild given with exactly windowSize-1 following labels\n}","preventionTips":["For backward scans, slice exactly windowSize-1 labels after the current position.","Guard sequence boundaries — near the end of the sequence there may not be enough following context.","Reuse a shared context-building helper across all FactorTable query methods."],"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"}