stanfordnlp/CoreNLP · error · RuntimeException

-removeCodeTrees only works on a single file

Error message

-removeCodeTrees only works on a single file

What it means

The -removeCodeTrees option of Treebanks.main strips "(CODE ...)" pseudo-trees via a whole-file string replacement, so it can only operate on a single file. If the treebank argument is a directory, it throws RuntimeException.

Solutions

  1. Pass a single file path as the treebank when using -removeCodeTrees.
  2. Script a loop that applies the tool to each file in the directory individually.
  3. Pre-concatenate directory files into one file, then run with -removeCodeTrees.

Example fix

// before
java ... Treebanks -removeCodeTrees wsj/  // directory
// after
java ... Treebanks -removeCodeTrees wsj/0001.mrg  // single file
Defensive patterns

Strategy: validation

Validate before calling

if (removeCodeTrees && new File(treebankPath).isDirectory()) { throw new IllegalArgumentException("-removeCodeTrees requires a single file"); }

Try / catch

try { Treebanks.main(args); } catch (RuntimeException e) { /* rerun per-file over directory contents */ }

Prevention

When it happens

Trigger: Running Treebanks with -removeCodeTrees while the treebank path (args[i]) points to a directory instead of one file.

Common situations: Pointing command-line treebank options at the standard directory-of-files corpus layout; scripting bulk cleanup over a folder expecting recursive handling.

Understand the failure class

Background: "Unknown argument", "Invalid value", and "must be one of": invalid CLI argument errors explained — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/e4105ab24256e5c3. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/trees/Treebanks.java:311

        }
      });
    }

    if (decimate) {
      Writer w1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(decimatePrefix + "-train.txt"), encoding));
      Writer w2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(decimatePrefix + "-dev.txt"), encoding));
      Writer w3 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(decimatePrefix + "-test.txt"), encoding));
      treebank.decimate(w1, w2, w3);
    }

    if (timing) {
      runTiming(treebank);
    }

    if (removeCodeTrees) {
      // this is a bit of a hack. It only works on an individual file
      if (new File(args[i]).isDirectory()) {
        throw new RuntimeException("-removeCodeTrees only works on a single file");
      }
      String treebankStr = IOUtils.slurpFile(args[i]);
      treebankStr = treebankStr.replaceAll("\\( \\(CODE <[^>]+>\\)\\)", "");
      Writer w = new OutputStreamWriter(new FileOutputStream(args[i]), encoding);
      w.write(treebankStr);
      w.close();
    }
  } // end main()


  private static void printPunct(Treebank treebank, TreebankLanguagePack tlp, PrintWriter pw) {
    if (tlp == null) {
      log.info("The -punct option requires you to specify -tlp");
    } else {
      Predicate<String> punctTagFilter = tlp.punctuationTagAcceptFilter();
      for (Tree t : treebank) {
        List<TaggedWord> tws = t.taggedYield();
        for (TaggedWord tw : tws) {

View on GitHub (pinned to 1b7edd19c4)