stanfordnlp/CoreNLP · error · UnsupportedOperationException

BiLexPCFGParser doesn't support k best parses

Error message

BiLexPCFGParser doesn't support k best parses

What it means

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.

Solutions

  1. Disable the option that selects BiLexPCFGParser and use the default ExhaustivePCFGParser query for k-best parses
  2. Turn off k-best output (k=1) when using BiLex constrained parsing
  3. Guard calls with instanceof / support checks before requesting k-best parses

Example fix

// before
List<ScoredObject<Tree>> kbest = pq.getKBestParses(10);
// after
if (pq instanceof BiLexPCFGParser.NBestBiLexPCFGParser == false) {
  List<ScoredObject<Tree>> kbest = pq.getKBestParses(10);
} else {
  Tree best = pq.getBestParse();
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (pq.getClass().getName().contains("BiLexPCFGParser")) {
  throw new IllegalStateException("k-best not available under BiLex constrained parsing");
}

Type guard

function supportsKBest(pq) { return !(pq instanceof BiLexPCFGParser); }

Try / catch

try {
  return pq.getKBestParses(k);
} catch (UnsupportedOperationException e) {
  log.info("falling back to single best parse");
  return Collections.singletonList(new ScoredObject<>(pq.getBestParse(), pq.getBestScore()));
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/2d893dd4963c6b34. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/parser/lexparser/BiLexPCFGParser.java:165

   * @return The list of k best trees
   */
  public List<ScoredObject<Tree>> getKGoodParses(int k) {
    List<ScoredObject<Tree>> nGoodTreesList = new ArrayList<>(op.testOptions.printFactoredKGood);
    for (Edge e : nGoodTrees) {
      nGoodTreesList.add(new ScoredObject<>(extractParse(e), e.iScore));
    }
    return nGoodTreesList;
  }

  /** Get the exact k best parses for the sentence.
   *
   *  @param k The number of best parses to return
   *  @return The exact k best parses for the sentence, with
   *         each accompanied by its score (typically a
   *         negative log probability).
   */
  public List<ScoredObject<Tree>> getKBestParses(int k) {
    throw new UnsupportedOperationException("BiLexPCFGParser doesn't support k best parses");
  }


  /** Get a complete set of the maximally scoring parses for a sentence,
   *  rather than one chosen at random.  This set may be of size 1 or larger.
   *
   *  @return All the equal best parses for a sentence, with each
   *         accompanied by its score
   */
  public List<ScoredObject<Tree>> getBestParses() {
    throw new UnsupportedOperationException("BiLexPCFGParser doesn't support best parses");
  }

  /** Get k parse samples for the sentence.  It is expected that the
   *  parses are sampled based on their relative probability.
   *
   *  @param k The number of sampled parses to return
   *  @return A list of k parse samples for the sentence, with

View on GitHub (pinned to 1b7edd19c4)