stanfordnlp/CoreNLP · error · UnsupportedOperationException

BiLexPCFGParser doesn't support k sampled parses

Error message

BiLexPCFGParser doesn't support k sampled parses

What it means

BiLexPCFGParser also does not implement stochastic sampling of k parses; getKSampledParses is a stub that throws UnsupportedOperationException, indicating sampling is unavailable in this constrained parser implementation.

Solutions

  1. Use the exhaustive PCFG parser query for k sampled parses
  2. Set k to default single-parse behavior and use getBestParse() instead
  3. Skip sampling logic when the query is a BiLexPCFGParser

Example fix

// before
List<ScoredObject<Tree>> samples = pq.getKSampledParses(5);
// after
if (!(pq instanceof BiLexPCFGParser)) {
  List<ScoredObject<Tree>> samples = pq.getKSampledParses(5);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (pq instanceof BiLexPCFGParser) {
  throw new IllegalStateException("sampling unsupported under BiLex parsing");
}

Type guard

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

Try / catch

try {
  return pq.getKSampledParses(k);
} catch (UnsupportedOperationException e) {
  return Collections.emptyList(); // sampling not available
}

Prevention

When it happens

Trigger: Calling getKSampledParses(k) on a BiLexPCFGParser query — e.g. code that samples multiple parses (like certain reranking or oracle experiments) while BiLex constrained parsing is selected.

Common situations: Oracle/parse-sampling experiments run with constrained parsing enabled; generic code paths that try sampling on any ParserQuery.

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/84ceb2d6b65ce553. Report an issue: GitHub.

Appendix: source

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

  /** 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
   *         each accompanied by its score
   */
  public List<ScoredObject<Tree>> getKSampledParses(int k) {
    throw new UnsupportedOperationException("BiLexPCFGParser doesn't support k sampled parses");
  }

  protected Edge tempEdge;

  protected void combine(Edge edge, Hook hook) {
    if (VERBOSE) {
      log.info("Combining: " + edge + " and " + hook);
    }
    // make result edge
    if (hook.isPreHook()) {
      tempEdge.start = edge.start;
      tempEdge.end = hook.end;
    } else {
      tempEdge.start = hook.start;
      tempEdge.end = edge.end;
    }
    tempEdge.state = hook.state;
    tempEdge.head = hook.head;

View on GitHub (pinned to 1b7edd19c4)