{"record":{"id":"2d893dd4963c6b34","repo":"stanfordnlp/CoreNLP","slug":"bilexpcfgparser-doesn-t-support-k-best-parses","errorCode":null,"errorMessage":"BiLexPCFGParser doesn't support k best parses","messagePattern":"BiLexPCFGParser doesn't support k best parses","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/lexparser/BiLexPCFGParser.java","lineNumber":165,"sourceCode":"   * @return The list of k best trees\n   */\n  public List<ScoredObject<Tree>> getKGoodParses(int k) {\n    List<ScoredObject<Tree>> nGoodTreesList = new ArrayList<>(op.testOptions.printFactoredKGood);\n    for (Edge e : nGoodTrees) {\n      nGoodTreesList.add(new ScoredObject<>(extractParse(e), e.iScore));\n    }\n    return nGoodTreesList;\n  }\n\n  /** Get the exact k best parses for the sentence.\n   *\n   *  @param k The number of best parses to return\n   *  @return The exact k best parses for the sentence, with\n   *         each accompanied by its score (typically a\n   *         negative log probability).\n   */\n  public List<ScoredObject<Tree>> getKBestParses(int k) {\n    throw new UnsupportedOperationException(\"BiLexPCFGParser doesn't support k best parses\");\n  }\n\n\n  /** Get a complete set of the maximally scoring parses for a sentence,\n   *  rather than one chosen at random.  This set may be of size 1 or larger.\n   *\n   *  @return All the equal best parses for a sentence, with each\n   *         accompanied by its score\n   */\n  public List<ScoredObject<Tree>> getBestParses() {\n    throw new UnsupportedOperationException(\"BiLexPCFGParser doesn't support best parses\");\n  }\n\n  /** Get k parse samples for the sentence.  It is expected that the\n   *  parses are sampled based on their relative probability.\n   *\n   *  @param k The number of sampled parses to return\n   *  @return A list of k parse samples for the sentence, with","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/lexparser/BiLexPCFGParser.java#L147-L183","documentation":"BiLexPCFGParser (the NParallel/BiLex constrained parser) implements only single best/sampled parsing; k-best enumeration is not implemented. getKBestParses is an explicit UnsupportedOperationException stub documenting that limitation.","triggerScenarios":"Calling getKBestParses(k) on a ParserQuery that is actually a BiLexPCFGParser query — e.g. requesting k-best output (-k N / kbest options) while using a parser configuration that selects BiLexPCFGParser.","commonSituations":"Running the LexicalizedParser with k-best output enabled while constraints/BiLex parsing is active; code that generically queries ParserQuery for k best parses without checking support.","solutions":["Disable the option that selects BiLexPCFGParser and use the default ExhaustivePCFGParser query for k-best parses","Turn off k-best output (k=1) when using BiLex constrained parsing","Guard calls with instanceof / support checks before requesting k-best parses"],"exampleFix":"// before\nList<ScoredObject<Tree>> kbest = pq.getKBestParses(10);\n// after\nif (pq instanceof BiLexPCFGParser.NBestBiLexPCFGParser == false) {\n  List<ScoredObject<Tree>> kbest = pq.getKBestParses(10);\n} else {\n  Tree best = pq.getBestParse();\n}","handlingStrategy":"type-guard","validationCode":"if (pq.getClass().getName().contains(\"BiLexPCFGParser\")) {\n  throw new IllegalStateException(\"k-best not available under BiLex constrained parsing\");\n}","typeGuard":"function supportsKBest(pq) { return !(pq instanceof BiLexPCFGParser); }","tryCatchPattern":"try {\n  return pq.getKBestParses(k);\n} catch (UnsupportedOperationException e) {\n  log.info(\"falling back to single best parse\");\n  return Collections.singletonList(new ScoredObject<>(pq.getBestParse(), pq.getBestScore()));\n}","preventionTips":["Don't enable k-best output flags together with BiLex constrained parsing","Check ParserQuery implementation before calling k-best APIs","Read the parser class docs for which query methods are implemented"],"tags":["java","stanford-parser","unsupported-operation","kbest"],"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"}