{"record":{"id":"9e6bab5ddefe9dd0","repo":"stanfordnlp/CoreNLP","slug":"found-two-roots-for-sentence-sentence","errorCode":null,"errorMessage":"Found two roots for sentence \" + sentence","messagePattern":"Found two roots for sentence \" \\+ sentence","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/sentiment/ReadSentimentDataset.java","lineNumber":242,"sourceCode":"    Tree[] subtrees = new Tree[maxNode + 1];\n    for (int i = 0; i < sentence.size(); ++i) {\n      CoreLabel word = new CoreLabel();\n      word.setValue(sentence.get(i));\n      Tree leaf = new LabeledScoredTreeNode(word);\n      subtrees[i] = new LabeledScoredTreeNode(new CoreLabel());\n      subtrees[i].addChild(leaf);\n    }\n\n    for (int i = sentence.size(); i <= maxNode; ++i) {\n      subtrees[i] = new LabeledScoredTreeNode(new CoreLabel());\n    }\n\n    boolean[] connected = new boolean[maxNode + 1];\n    Tree root = null;\n    for (int index = 0; index < parentPointers.size(); ++index) {\n      if (parentPointers.get(index) == -1) {\n        if (root != null) {\n          throw new RuntimeException(\"Found two roots for sentence \" + sentence);\n        }\n        root = subtrees[index];\n      } else {\n        // Walk up the tree structure to make sure that leftmost\n        // phrases are added first.  Otherwise, if the numbers are\n        // inverted, we might get the right phrase added to a parent\n        // first, resulting in \"case zero in this\", for example,\n        // instead of \"in this case zero\"\n        // Note that because we keep track of which ones are already\n        // connected, we process this at most once per parent, so the\n        // overall construction time is still efficient.\n        connect(parentPointers, subtrees, connected, index);\n      }\n    }\n\n    for (int i = 0; i <= maxNode; ++i) {\n      List<Tree> leaves = subtrees[i].getLeaves();\n      List<String> words = CollectionUtils.transformAsList(leaves, TRANSFORM_TREE_TO_WORD);","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/sentiment/ReadSentimentDataset.java#L224-L260","documentation":"When converting the raw dataset format into Trees, ReadSentimentDataset expects exactly one node with parent pointer -1 (the root) per sentence. If the parent-pointer list contains two or more -1 entries, the converter cannot determine a single root and throws RuntimeException.","triggerScenarios":"Parsing a corrupted or hand-edited sentence file in the sentiment dataset format where more than one line has parent index -1, or a bug in custom dataset generation producing disconnected components.","commonSituations":"Manually modified train.txt/dev.txt files, partial downloads that merged chunks of two sentences, or custom-preprocessed data with wrong parent indexing.","solutions":["Inspect the sentence's parent-pointer lines and ensure exactly one has -1","Re-extract or re-download the pristine Stanford Sentiment Treebank dataset","Fix your preprocessing script so only the top node has parent -1"],"exampleFix":"// before (data)\n0\n-1\n-1\n// after (data)\n0\n-1\n1","handlingStrategy":"validation","validationCode":"long roots = parentPointers.stream().filter(p -> p == -1).count();\nif (roots != 1) throw new IllegalArgumentException(\"Expected exactly 1 root, found \" + roots);","typeGuard":null,"tryCatchPattern":"try { converter.main(args); } catch (RuntimeException e) { if (e.getMessage().contains(\"Found two roots\")) { reportCorruptSentence(e); } else throw e; }","preventionTips":["Validate dataset files (one root per sentence) before conversion","Never hand-edit SST data files; regenerate from source","Checksum dataset archives after download"],"tags":["data-corruption","tree-parsing"],"backgroundTag":"internal-invariant-violation","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"}