{"record":{"id":"7944259455f0f5fb","repo":"stanfordnlp/CoreNLP","slug":"only-works-with-corelabels-trees","errorCode":null,"errorMessage":"Only works with CoreLabels trees","messagePattern":"Only works with CoreLabels trees","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/international/french/scripts/SplitCanditoTrees.java","lineNumber":166,"sourceCode":"\n    for (Tree t : treeMap.values()) {\n      MWEPreprocessor.countMWEStatistics(t, unigramTagger,\n                                         labelPreterm, pretermLabel,\n                                         labelTerm, termLabel);\n    }\n\n    for (Tree t : treeMap.values()) {\n      MWEPreprocessor.traverseAndFix(t, pretermLabel, unigramTagger);\n    }\n  }\n\n  public static void mungeLeaves(Tree tree, boolean lemmasAsLeaves, boolean addMorphoToLeaves) {\n    List<Label> labels = tree.yield();\n    \n    for (Label label : labels) {\n      ++nTokens;\n      if (!(label instanceof CoreLabel)) {\n        throw new IllegalArgumentException(\"Only works with CoreLabels trees\");\n      }\n\n      CoreLabel coreLabel = (CoreLabel) label;\n      String lemma = coreLabel.lemma();\n      //PTB escaping since we're going to put this in the leaf\n      if (lemma == null) {\n        // No lemma, so just add the surface form\n        lemma = coreLabel.word();\n      } else if (ESCAPE_PARENS && lemma.equals(\"(\")) {\n        lemma = \"-LRB-\";\n      } else if (ESCAPE_PARENS && lemma.equals(\")\")) {\n        lemma = \"-RRB-\";\n      }\n\n      if (lemmasAsLeaves) {\n        String escapedLemma = lemma;\n        coreLabel.setWord(escapedLemma);\n        coreLabel.setValue(escapedLemma);","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/international/french/scripts/SplitCanditoTrees.java#L148-L184","documentation":"SplitCanditoTrees.mungeLeaves processes tree leaf labels to extract lemma/morphology, but requires each leaf to be a CoreLabel carrying annotations. If a leaf is a plain Label/StringLabel, it throws this IllegalArgumentException since the needed annotation keys are unavailable.","triggerScenarios":"Reading Candito treebank trees with a TreeReaderFactory that produces plain labels (not CoreLabels) and then calling outputSplits, which invokes mungeLeaves.","commonSituations":"Using a default Treebank/TreeReader without setting a CoreLabel-producing factory (e.g. LabeledScoredTreeReaderFactory with StringLabels); mixing corpora read with different label factories.","solutions":["Configure the tree reader with a factory producing CoreLabels, e.g. use a TreeReaderFactory that wraps labels via CoreLabelTokenFactory or TreeCoreAnnotations","Convert the tree's labels to CoreLabel before calling mungeLeaves","Check which Treebank factory was used to load the trees and switch it to one that preserves CoreLabels"],"exampleFix":"// before\nTreebank tb = new DiskTreebank();\n// after\nTreebank tb = new DiskTreebank(new TreeReaderFactory() {\n  public TreeReader newTreeReader(Reader in) {\n    return new PennTreeReader(new BufferedReader(in), new LabeledScoredTreeFactory(CoreLabel.factory()));\n  }\n});","handlingStrategy":"type-guard","validationCode":"for (Label l : tree.yield()) { if (!(l instanceof CoreLabel)) throw new IllegalStateException(\"Tree leaves must be CoreLabels\"); }","typeGuard":"boolean hasCoreLabelLeaves(Tree tree) { return tree.yield().stream().allMatch(l -> l instanceof CoreLabel); }","tryCatchPattern":"try { mungeLeaves(tree, lemmasAsLeaves, addMorphoToLeaves); } catch (IllegalArgumentException e) { log.error(\"Wrong label type: \" + e.getMessage()); }","preventionTips":["Configure Treebank with a CoreLabel-producing TreeReaderFactory","Assert label types right after reading trees","Avoid mixing readers with different label factories in one pipeline"],"tags":["trees","corpus","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}