{"record":{"id":"cf8a857be4f75b26","repo":"stanfordnlp/CoreNLP","slug":"corelabels-required","errorCode":null,"errorMessage":"CoreLabels required!","messagePattern":"CoreLabels required!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/sentiment/SentimentUtils.java","lineNumber":40,"sourceCode":" * @author John Bauer\n */\npublic class SentimentUtils {\n  private SentimentUtils() {\n  } // static methods only\n\n    public static void attachLabels(Tree tree, Class<? extends CoreAnnotation<Integer>> annotationClass) {\n        if (tree.isLeaf()) {\n            return;\n        }\n        for (Tree child : tree.children()) {\n            attachLabels(child, annotationClass);\n        }\n        // In the sentiment data set, the node labels are simply the gold\n        // class labels.  There are no categories encoded.\n        int numericLabel = Integer.valueOf(tree.label().value());\n        Label label = tree.label();\n        if (!(label instanceof CoreLabel)) {\n            throw new IllegalArgumentException(\"CoreLabels required!\");\n        }\n        ((CoreLabel) label).set(annotationClass, numericLabel);\n\n    }\n\n    /**\n     * Given a file name, reads in those trees and returns them as a List\n     */\n    public static List<Tree> readTreesWithGoldLabels(String path) {\n        return readTreesWithLabels(path, RNNCoreAnnotations.GoldClass.class);\n    }\n\n    /**\n     * Given a file name, reads in those trees and returns them as list with\n     * labels attached as predictions\n     */\n    public static List<Tree> readTreesWithPredictedLabels(String path) {\n        return readTreesWithLabels(path, RNNCoreAnnotations.PredictedClass.class);","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/sentiment/SentimentUtils.java#L22-L58","documentation":"SentimentUtils.attachLabels assigns a numeric gold sentiment class to each tree node via CoreLabel.set(annotationClass, value). The node label must be a CoreLabel instance; if it is any other Label implementation the method throws IllegalArgumentException('CoreLabels required!').","triggerScenarios":"Calling attachLabels (directly or via readTreesWithLabels) on trees whose node labels are not CoreLabel — e.g. trees read with a different Label factory, or a tree whose label().value() parses as an int but whose Label is a StringLabel/Word, at SentimentUtils.java:40.","commonSituations":"Loading custom tree files with Tree.valueOf() default label factory instead of CoreLabel factory, converting trees from another parser output, or mixing LabeledScoredTreeReader output with sentiment code.","solutions":["Read the trees with a CoreLabel factory, e.g. Tree.valueOf with a CoreLabel factory or use PennTreeReader with a CoreLabelTreeNormalizer / LabeledScoredTreeReader(new CoreLabel().labelFactory()).","Convert trees before calling attachLabels: replace each label with a CoreLabel copying the value.","If constructing trees programmatically, create nodes with new CoreLabel() rather than StringLabel/Word."],"exampleFix":"// before\nTree tree = Tree.valueOf(\"(3 (2 It) (4 was))\"); // StringLabel nodes\nList<Tree> trees = SentimentUtils.readTreesWithLabels(file);\n// after\nTree tree = Tree.valueOf(\"(3 (2 It) (4 was))\", new CoreLabel().labelFactory());\nList<Tree> trees = SentimentUtils.readTreesWithLabels(file, new NumberRangesFileScanner(...));","handlingStrategy":"type-guard","validationCode":"// verify all node labels are CoreLabels before attaching\nfor (Tree t : trees)\n  for (Tree node : t)\n    if (!(node.label() instanceof CoreLabel)) throw new IllegalArgumentException(\"non-CoreLabel at \" + node);","typeGuard":"boolean hasOnlyCoreLabels(Tree t) {\n  return Trees.getLeaves(t).stream().allMatch(n -> n.label() instanceof CoreLabel)\n      && (t.label() == null || t.label() instanceof CoreLabel);\n}","tryCatchPattern":null,"preventionTips":["Read trees with a CoreLabel label factory.","Never mix StringLabel/Word-based trees with sentiment code.","Convert labels once at load time, not at use time."],"tags":["java","stanford-corenlp","labels","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"}