{"record":{"id":"8ecd17ab2baa620a","repo":"stanfordnlp/CoreNLP","slug":"attempt-to-create-chinesesimwordavgdepgrammar-befo","errorCode":null,"errorMessage":"Attempt to create ChineseSimWordAvgDepGrammar before Lexicon!!!","messagePattern":"Attempt to create ChineseSimWordAvgDepGrammar before Lexicon!!!","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/lexparser/ChineseTreebankParserParams.java","lineNumber":1198,"sourceCode":"          wfe2.setFeatureLevel(depGramFeatureLevel);\n          MaxentDependencyGrammar dg = new MaxentDependencyGrammar(op.tlpParams, wfe, wfe2, true, false, false);\n          dg.train(trees);\n          return dg;\n        }\n\n        public Object extract(Iterator<Tree> iterator, Function<Tree, Tree> f) {\n          throw new UnsupportedOperationException();\n        }\n      };\n    } else ------- */\n    if (useSimilarWordMap) {\n      return new MLEDependencyGrammarExtractor(op, wordIndex, tagIndex) {\n        @Override\n        public MLEDependencyGrammar formResult() {\n          wordIndex.addToIndex(Lexicon.UNKNOWN_WORD);\n          ChineseSimWordAvgDepGrammar dg = new ChineseSimWordAvgDepGrammar(tlpParams, directional, useDistance, useCoarseDistance, op.trainOptions.basicCategoryTagsInDependencyGrammar, op, wordIndex, tagIndex);\n          if (lex == null) {\n            throw new RuntimeException(\"Attempt to create ChineseSimWordAvgDepGrammar before Lexicon!!!\");\n          } else {\n            dg.setLex(lex);\n          }\n          for (IntDependency dependency : dependencyCounter.keySet()) {\n            dg.addRule(dependency, dependencyCounter.getCount(dependency));\n          }\n          return dg;\n        }\n\n     };\n    } else {\n      return new MLEDependencyGrammarExtractor(op, wordIndex, tagIndex);\n    }\n  }\n\n  /**\n   * Return a default sentence for the language (for testing)\n   */","sourceCodeStart":1180,"sourceCodeEnd":1216,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/lexparser/ChineseTreebankParserParams.java#L1180-L1216","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nMLEDependencyGrammar dg = extractor.formResult(); // lex == null -> throws\n// after\nlex = op.tlpParams.lex(op, wordIndex, tagIndex); // create lexicon first\ndg = extractor.formResult();","handlingStrategy":"try-catch","validationCode":"// ensure lexicon exists before formResult\nif (tlpParams.lex == null) { tlpParams.lex = op.tlpParams.lex(op, wordIndex, tagIndex); }","typeGuard":"boolean lexiconReady(ChineseTreebankParserParams p) { return p.lex != null; }","tryCatchPattern":"try { dg = extractor.formResult(); } catch (RuntimeException e) { if (e.getMessage().contains(\"before Lexicon\")) { initializeLexiconFirst(); } }","preventionTips":["Always create the Lexicon before invoking the dependency grammar extractor's formResult()","Use standard LexicalizedParser training entry points rather than hand-wired pipelines"],"tags":["java","parser","initialization-order","state"],"backgroundTag":"invalid-state-transition","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"}