stanfordnlp/CoreNLP · error · IOException

Error on line

Error message

Error on line 

What it means

UnaryGrammar.readData parses one unary rule per line from a serialized grammar file; if constructing/adding a UnaryRule from a line throws any exception, it is rethrown as IOException('Error on line ' + lineNum). It identifies a malformed line in the unary-rules section of the model file.

Solutions

  1. Restore the grammar file from a clean, matching-version copy (official model jar).
  2. Inspect the reported line number and fix the malformed unary rule line (correct format: parent child score).
  3. Regenerate the grammar by retraining/serializing with your current parser version.
  4. Check for invisible characters or Windows/Unix line-ending corruption if the file was transferred across platforms.

Example fix

// before (malformed unary rule line in grammar file)
NP-JJ
// after
NP JJ 0.123
Defensive patterns

Strategy: validation

Validate before calling

List<String> lines = Files.readAllLines(grammarPath);
for (int ln = 0; ln < lines.size(); ln++) {
  String l = lines.get(ln);
  if (l.length() > 0 && l.trim().split("\\s+").length < 3)
    System.err.println("Suspicious unary rule at line " + (ln+1) + ": " + l);
}

Try / catch

try {
  parser = LexicalizedParser.loadModel(path);
} catch (IOException e) {
  if (e.getMessage() != null && e.getMessage().startsWith("Error on line ")) {
    throw new IOException("Malformed unary rule in grammar file " + path + ": " + e.getMessage(), e);
  }
  throw e;
}

Prevention

When it happens

Trigger: Loading a parser grammar text file where a line in the unary rules section is blank-inconsistent, has wrong field count/format, or references labels not present in the index — e.g., an edited or version-mismatched grammar file.

Common situations: Hand-edited grammar files, truncated/corrupted downloads, or loading a model written by a different parser version into a newer loader.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/parser/lexparser/UnaryGrammar.java:297

    init();
  }

  /**
   * Populates data in this UnaryGrammar from a character stream.
   *
   * @param in The Reader the grammar is read from.
   * @throws IOException If there is a reading problem
   */
  public void readData(BufferedReader in) throws IOException {
    String line;
    int lineNum = 1;
    // all lines have one rule per line
    line = in.readLine();
    while (line != null && line.length() > 0) {
      try {
        addRule(new UnaryRule(line, index));
      } catch (Exception e) {
        throw new IOException("Error on line " + lineNum);
      }
      lineNum++;
      line = in.readLine();
    }
    purgeRules();
  }

  /**
   * Writes out data from this Object.
   * @param w Data is written to this Writer
   */
  public void writeData(Writer w) {
    PrintWriter out = new PrintWriter(w);
    // all lines have one rule per line
    for (UnaryRule ur : this) {
      out.println(ur.toString(index));
    }
    out.flush();

View on GitHub (pinned to 1b7edd19c4)