{"record":{"id":"93adb8d392042d48","repo":"stanfordnlp/CoreNLP","slug":"kbestsequencefinder-only-works-with-rightwindow","errorCode":null,"errorMessage":"KBestSequenceFinder only works with rightWindow == 0 not ","messagePattern":"KBestSequenceFinder only works with rightWindow == 0 not ","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/sequences/KBestSequenceFinder.java","lineNumber":46,"sourceCode":"  /**\n   * Runs the Viterbi algorithm on the sequence model, and then proceeds to efficiently\n   * backwards decode the best k label sequence assignments.\n   * This sequence finder only works on SequenceModel's with rightWindow == 0.\n   *\n   * @param ts The SequenceModel to find the best k label sequence assignments of\n   * @param k The number of top-scoring assignments to find.\n   * @return A Counter with k entries that map from a sequence assignment (int array) to a double score\n   */\n  @SuppressWarnings(\"MethodMayBeStatic\")\n  public Counter<int[]> kBestSequences(SequenceModel ts, int k) {\n\n    // Set up tag options\n    int length = ts.length();\n    int leftWindow = ts.leftWindow();\n    int rightWindow = ts.rightWindow();\n\n    if (rightWindow != 0) {\n      throw new IllegalArgumentException(\"KBestSequenceFinder only works with rightWindow == 0 not \" + rightWindow);\n    }\n\n    int padLength = length + leftWindow + rightWindow;\n\n    int[][] tags = new int[padLength][];\n    int[] tagNum = new int[padLength];\n    for (int pos = 0; pos < padLength; pos++) {\n      tags[pos] = ts.getPossibleValues(pos);\n      tagNum[pos] = tags[pos].length;\n    }\n\n    int[] tempTags = new int[padLength];\n\n    // Set up product space sizes\n    int[] productSizes = new int[padLength];\n\n    int curProduct = 1;\n    for (int i = 0; i < leftWindow; i++) {","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/sequences/KBestSequenceFinder.java#L28-L64","documentation":"KBestSequenceFinder.kBestSequences enumerates k-best label sequences with a left-to-right algorithm that requires the model to have no right context window (rightWindow == 0). Models with a non-zero right window throw IllegalArgumentException('KBestSequenceFinder only works with rightWindow == 0 not ' + rightWindow).","triggerScenarios":"Calling kBestSequences (or bestSequences/bestSequence/bestLabelsCounter, which delegate to it) on a SequenceModel whose rightWindow() is non-zero, at KBestSequenceFinder.java:46.","commonSituations":"Requesting n-best output from taggers/CRFs whose SequenceModel carries right-side context (e.g. models built with a right window for lookahead features); switching a pipeline from ExactBestSequenceFinder to KBestSequenceFinder without adjusting the model.","solutions":["Rebuild/wrap the SequenceModel so it has rightWindow == 0 (move right-context features into left context or drop them).","Use ExactBestSequenceFinder (or another finder supporting right windows) if you only need the single best sequence.","If you need k-best with right windows, implement a search that handles them, since this finder explicitly does not."],"exampleFix":"// before\nnew KBestSequenceFinder().kBestSequences(modelWithRightWindow, k); // model.rightWindow() == 1\n// after\nnew ExactBestSequenceFinder().bestSequence(modelWithRightWindow);\n// or construct the model with rightWindow = 0 and then:\nnew KBestSequenceFinder().kBestSequences(modelRightWindowZero, k);","handlingStrategy":"validation","validationCode":"// check the finder's precondition before calling\nif (ts.rightWindow() != 0)\n  throw new IllegalArgumentException(\"use ExactBestSequenceFinder for rightWindow=\" + ts.rightWindow());","typeGuard":"boolean supportsKBest(SequenceModel ts) { return ts.rightWindow() == 0; }","tryCatchPattern":null,"preventionTips":["Check rightWindow() before choosing a sequence finder.","Prefer ExactBestSequenceFinder when right context is needed.","Document right-window requirements in custom SequenceModel implementations."],"tags":["java","stanford-corenlp","sequence-model","kbest","unsupported-operation"],"backgroundTag":"unsupported-operation","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"}