{"record":{"id":"efd6d8da36e4a132","repo":"stanfordnlp/CoreNLP","slug":"fastfactoredparser-cannot-provide-k-good-parses","errorCode":null,"errorMessage":"FastFactoredParser: cannot provide k good parses.","messagePattern":"FastFactoredParser: cannot provide k good parses\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/lexparser/FastFactoredParser.java","lineNumber":88,"sourceCode":"\n\n  private List<ScoredObject<Tree>> nGoodTrees = new ArrayList<>();\n\n\n\n  /**\n   * Return the list of N \"good\" parses of the sentence most recently parsed.\n   * (The first is guaranteed to be the best, but later ones are only\n   * guaranteed the best subject to the possibilities that disappear because\n   * the PCFG/Dep charts only store the best over each span.)\n   *\n   * @return The list of N best trees\n   */\n  public List<ScoredObject<Tree>> getKGoodParses(int k) {\n    if (k <= nGoodTrees.size()) {\n      return nGoodTrees.subList(0, k);\n    } else {\n      throw new UnsupportedOperationException(\"FastFactoredParser: cannot provide \" + k + \" good parses.\");\n    }\n  }\n\n\n  /** Use the DependencyGrammar to score the tree.\n   *\n   * @param tr A binarized tree (as returned by the PCFG parser\n   * @return The score for the tree according to the grammar\n   */\n  private double depScoreTree(Tree tr) {\n    // log.info(\"Here's our tree:\");\n    // tr.pennPrint();\n    // log.info(Trees.toDebugStructureString(tr));\n    Tree cwtTree = tr.deepCopy(new LabeledScoredTreeFactory(), new CategoryWordTagFactory());\n    cwtTree.percolateHeads(binHeadFinder);\n    // log.info(\"Here's what it went to:\");\n    // cwtTree.pennPrint();\n    List<IntDependency> deps = MLEDependencyGrammar.treeToDependencyList(cwtTree, wordIndex, tagIndex);","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/lexparser/FastFactoredParser.java#L70-L106","documentation":"FastFactoredParser.getKGoodParses(k) returns the first k of its cached nGoodTrees list. If more than the available number of good parses are requested, it throws UnsupportedOperationException since it cannot compute additional good parses on demand.","triggerScenarios":"Calling getKGoodParses(k) where k > nGoodTrees.size(), i.e. requesting more good parses than the parser retained (bounded by nGoodTrees size / parser settings).","commonSituations":"Requesting e.g. getKGoodParses(20) when the factored parser only keeps a handful of good parses; generic code assuming k is always satisfiable.","solutions":["Cap k to the number of available good parses (check nGoodTrees.size() or use a smaller k).","Catch UnsupportedOperationException and fall back to the available parses.","Use ExhaustivePCFGParser.getKBestParses(k) if the full k-best list is genuinely needed."],"exampleFix":"// before\nList<ScoredObject<Tree>> parses = pq.getKGoodParses(20);\n// after\nList<ScoredObject<Tree>> parses;\ntry {\n  parses = pq.getKGoodParses(20);\n} catch (UnsupportedOperationException e) {\n  parses = pq.getKGoodParses(1); // only the best is guaranteed\n}","handlingStrategy":"try-catch","validationCode":"int available = nGoodTreesSize(query); // reflectively or via API\nint safeK = Math.min(k, available);\nif (safeK < k) log.warning(\"requested \" + k + \" good parses, only \" + safeK + \" available\");","typeGuard":null,"tryCatchPattern":"try { parses = pq.getKGoodParses(k); } catch (UnsupportedOperationException e) { parses = pq.getKGoodParses(Math.max(1, k / 4)); }","preventionTips":["Cap requested k to the parser's retained good-parse list size.","Use ExhaustivePCFGParser.getKBestParses for guaranteed-size k lists.","Read FastFactoredParser configuration to know how many good parses it stores."],"tags":["java","nlp","parsing","unsupported-operation","k-best"],"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"}