{"record":{"id":"d3447f5e84cba9f6","repo":"stanfordnlp/CoreNLP","slug":"trees-not-of-equal-length","errorCode":null,"errorMessage":"Trees not of equal length","messagePattern":"Trees not of equal length","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/sentiment/ExternalEvaluate.java","lineNumber":40,"sourceCode":"\n  public ExternalEvaluate(RNNOptions op, List<Tree> predictedTrees) {\n    super(op);\n    this.predicted = predictedTrees;\n  }\n\n  @Override\n  public void populatePredictedLabels(List<Tree> trees) {\n    if (trees.size() != this.predicted.size()) {\n      throw new IllegalArgumentException(\"Number of gold and predicted trees not equal!\");\n    }\n    for (int i = 0; i < trees.size(); i++) {\n      Iterator<Tree> goldTree = trees.get(i).iterator();\n      Iterator<Tree> predictedTree = this.predicted.get(i).iterator();\n      while (goldTree.hasNext() || predictedTree.hasNext()) {\n        Tree goldNode = goldTree.next();\n        Tree predictedNode = predictedTree.next();\n        if (goldNode == null || predictedNode == null) {\n          throw new IllegalArgumentException(\"Trees not of equal length\");\n        }\n        if (goldNode.isLeaf()) {\n          continue;\n        }\n        CoreLabel label = (CoreLabel) goldNode.label();\n        label.set(RNNCoreAnnotations.PredictedClass.class,\n                RNNCoreAnnotations.getPredictedClass(predictedNode));\n      }\n    }\n  }\n\n  /**\n   * Expected arguments are {@code -gold gold -predicted predicted }\n   *\n   * For example <br>\n   * {@code java edu.stanford.nlp.sentiment.ExternalEvaluate annotatedTrees.txt predictedTrees.txt }\n   */\n  public static void main(String[] args) {","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/sentiment/ExternalEvaluate.java#L22-L58","documentation":"While propagating predicted labels, ExternalEvaluate iterates gold and predicted trees in parallel; the iterators must yield nodes in lockstep. If either iterator is exhausted while the other still has nodes (or a next() returns null), it throws IllegalArgumentException 'Trees not of equal length'.","triggerScenarios":"A gold/predicted tree pair with different numbers of nodes — e.g. predictions computed on non-binarized or differently preprocessed trees, or a truncated/malformed predicted tree.","commonSituations":"Predictions produced without the same binarization/filtering as the gold trees; tokenization differences changing tree shape; corrupted prediction output lines.","solutions":["Generate predicted trees with the same preprocessing (binarizer, filterUnknown) as the gold trees","Verify each tree pair node-by-node (size/shape) before evaluation","Check the prediction file for malformed or truncated trees"],"exampleFix":"// before\nList<Tree> pred = SentimentUtils.readTreesWithGoldLabels(rawPredPath); // unbinarized\n// after\nTreeBinarizer b = TreeBinarizer.simpleTreeBinarizer(hf, tlp);\nList<Tree> pred = rawTrees.stream().map(t -> b.transformTree(t)).collect(toList());","handlingStrategy":"validation","validationCode":"for (int i = 0; i < gold.size(); i++) {\n  if (gold.get(i).size() != pred.get(i).size())\n    throw new IllegalArgumentException(\"Node-count mismatch at tree \" + i);\n}","typeGuard":"static boolean sameShape(Tree a, Tree b) { return a.size() == b.size(); }","tryCatchPattern":"try {\n  externalEval.populatePredictedLabels(goldTrees);\n} catch (IllegalArgumentException e) {\n  log.error(\"Tree shape mismatch: \" + e.getMessage());\n}","preventionTips":["Binarize predicted trees with the same TreeBinarizer settings as gold","Tokenize prediction inputs identically to the gold treebank","Validate node counts per tree pair before scoring"],"tags":["sentiment","evaluation","tree-structure"],"backgroundTag":"shape-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"}