{"record":{"id":"6559bf75b19e8479","repo":"stanfordnlp/CoreNLP","slug":"exhaustivepcfgparser-doesn-t-sample","errorCode":null,"errorMessage":"ExhaustivePCFGParser doesn't sample.","messagePattern":"ExhaustivePCFGParser doesn't sample\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/lexparser/ExhaustivePCFGParser.java","lineNumber":1807,"sourceCode":"   *  @param k The number of good parses to return\n   *  @return A list of k good parses for the sentence, with\n   *         each accompanied by its score\n   */\n  @Override\n  public List<ScoredObject<Tree>> getKGoodParses(int k) {\n    return getKBestParses(k);\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\n   *         each accompanied by its score\n   */\n  @Override\n  public List<ScoredObject<Tree>> getKSampledParses(int k) {\n    throw new UnsupportedOperationException(\"ExhaustivePCFGParser doesn't sample.\");\n  }\n\n\n  //\n  // BEGIN K-BEST STUFF\n  // taken straight out of \"Better k-best Parsing\" by Liang Huang and David\n  // Chiang\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  @Override\n  public List<ScoredObject<Tree>> getKBestParses(int k) {","sourceCodeStart":1789,"sourceCodeEnd":1825,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/lexparser/ExhaustivePCFGParser.java#L1789-L1825","documentation":"Sentinel UnsupportedOperationException signalling a capability gap: ExhaustivePCFGParser only performs exact exhaustive parsing and can return k-best or good parses (getKGoodParses just delegates to getKBestParses), but it contains no sampling machinery to draw parses according to their relative probability. The error fires whenever getKSampledParses is called on this parser.","triggerScenarios":"Calling getKSampledParses(k) on a ParserQuery produced by ExhaustivePCFGParser after parsing.","commonSituations":"Code that uniformly requests sampled parses across parser implementations; PCFG parser has no sampling machinery despite having k-best support.","solutions":["Sample manually from getKBestParses(k) output weighted by their scores.","Use getKGoodParses(k) or getKBestParses(k) instead if an approximate list suffices.","Implement external sampling: draw parses from the returned scored list using exp(-score) weights."],"exampleFix":"// before\nList<ScoredObject<Tree>> samples = pq.getKSampledParses(10);\n// after\nList<ScoredObject<Tree>> best = pq.getKBestParses(100);\nList<ScoredObject<Tree>> samples = weightedSampleWithoutReplacement(best, 10, new Random());","handlingStrategy":"try-catch","validationCode":"boolean supportsSampling = query.getClass().getSimpleName().contains(\"Factored\") && !query.getClass().getSimpleName().contains(\"ExhaustivePCFG\");","typeGuard":null,"tryCatchPattern":"try { samples = pq.getKSampledParses(k); } catch (UnsupportedOperationException e) { samples = weightedSample(pq.getKBestParses(100), k); }","preventionTips":["Assume ExhaustivePCFGParser provides only exact/approximate k-best, not sampling.","Derive samples from scored k-best lists with probability exp(score).","Keep sampling logic outside the parser abstraction layer."],"tags":["java","nlp","parsing","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"}