stanfordnlp/CoreNLP · error · RuntimeException
Attempt to create ChineseSimWordAvgDepGrammar before…
Error message
Attempt to create ChineseSimWordAvgDepGrammar before Lexicon!!!
What it means
ChineseTreebankParserParams's dependency grammar extractor creates a ChineseSimWordAvgDepGrammar in formResult() and immediately requires the lexicon field to already be set. If lex is still null — meaning the lexicon was created after (or without) the dependency grammar extractor — formResult throws RuntimeException telling you the grammar was created before the Lexicon.
Solutions
- Ensure the Lexicon is constructed and assigned (tlpParams.lex / extractor lex field) before formResult() is invoked
- Use the standard LexicalizedParser training entry points which create the lexicon first
- Patch your custom extractor to lazily obtain or defer dg.setLex(lex) until the lexicon exists
Example fix
// before MLEDependencyGrammar dg = extractor.formResult(); // lex == null -> throws // after lex = op.tlpParams.lex(op, wordIndex, tagIndex); // create lexicon first dg = extractor.formResult();
Defensive patterns
Strategy: try-catch
Validate before calling
// ensure lexicon exists before formResult
if (tlpParams.lex == null) { tlpParams.lex = op.tlpParams.lex(op, wordIndex, tagIndex); } Type guard
boolean lexiconReady(ChineseTreebankParserParams p) { return p.lex != null; } Try / catch
try { dg = extractor.formResult(); } catch (RuntimeException e) { if (e.getMessage().contains("before Lexicon")) { initializeLexiconFirst(); } } Prevention
- Always create the Lexicon before invoking the dependency grammar extractor's formResult()
- Use standard LexicalizedParser training entry points rather than hand-wired pipelines
When it happens
Trigger: Using ChineseTreebankParserParams with a similar-word dependency grammar extractor where the training pipeline calls formResult() before a Lexicon has been assigned to the params/extractor, leaving the lex field null.
Common situations: Custom training scripts that build the grammar extractor directly instead of going through the standard train path that first creates the lexicon; ordering mistakes when wiring Options/tlpParams manually.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- attempt to get word when sentence and lattice are null!
- Attribute already defined:
- Attribute already defined
- Attribute match already defined:
- Attribute match already defined
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/8ecd17ab2baa620a.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/parser/lexparser/ChineseTreebankParserParams.java:1198
wfe2.setFeatureLevel(depGramFeatureLevel);
MaxentDependencyGrammar dg = new MaxentDependencyGrammar(op.tlpParams, wfe, wfe2, true, false, false);
dg.train(trees);
return dg;
}
public Object extract(Iterator<Tree> iterator, Function<Tree, Tree> f) {
throw new UnsupportedOperationException();
}
};
} else ------- */
if (useSimilarWordMap) {
return new MLEDependencyGrammarExtractor(op, wordIndex, tagIndex) {
@Override
public MLEDependencyGrammar formResult() {
wordIndex.addToIndex(Lexicon.UNKNOWN_WORD);
ChineseSimWordAvgDepGrammar dg = new ChineseSimWordAvgDepGrammar(tlpParams, directional, useDistance, useCoarseDistance, op.trainOptions.basicCategoryTagsInDependencyGrammar, op, wordIndex, tagIndex);
if (lex == null) {
throw new RuntimeException("Attempt to create ChineseSimWordAvgDepGrammar before Lexicon!!!");
} else {
dg.setLex(lex);
}
for (IntDependency dependency : dependencyCounter.keySet()) {
dg.addRule(dependency, dependencyCounter.getCount(dependency));
}
return dg;
}
};
} else {
return new MLEDependencyGrammarExtractor(op, wordIndex, tagIndex);
}
}
/**
* Return a default sentence for the language (for testing)
*/View on GitHub (pinned to 1b7edd19c4)