{"record":{"id":"335664dd181319b9","repo":"stanfordnlp/CoreNLP","slug":"chinesecharacterbasedlexicon-has-no-rule-iterator","errorCode":null,"errorMessage":"ChineseCharacterBasedLexicon has no rule iterator!","messagePattern":"ChineseCharacterBasedLexicon has no rule iterator!","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/lexparser/ChineseCharacterBasedLexicon.java","lineNumber":363,"sourceCode":"\n    return buf.toString();\n  }\n\n  /**\n   * Samples over words regardless of POS: first samples POS, then samples\n   * word according to that POS\n   *\n   * @return a sampled word\n   */\n  public String sampleFrom() {\n    String POS = POSDistribution.sampleFrom();\n    return sampleFrom(POS);\n  }\n\n  // don't think this should be used, but just in case...\n  @Override\n  public Iterator<IntTaggedWord> ruleIteratorByWord(int word, int loc, String featureSpec) {\n    throw new UnsupportedOperationException(\"ChineseCharacterBasedLexicon has no rule iterator!\");\n  }\n\n  // don't think this should be used, but just in case...\n  @Override\n  public Iterator<IntTaggedWord> ruleIteratorByWord(String word, int loc, String featureSpec) {\n    throw new UnsupportedOperationException(\"ChineseCharacterBasedLexicon has no rule iterator!\");\n  }\n\n  /** Returns the number of rules (tag rewrites as word) in the Lexicon.\n   *  This method isn't yet implemented in this class.\n   *  It currently just returns 0, which may or may not be helpful.\n   */\n  @Override\n  public int numRules() {\n    return 0;\n  }\n\n  private Distribution<Integer> getWordLengthDistribution() {","sourceCodeStart":345,"sourceCodeEnd":381,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/lexparser/ChineseCharacterBasedLexicon.java#L345-L381","documentation":"The int-word variant of ruleIteratorByWord is deliberately unimplemented in ChineseCharacterBasedLexicon because the character-based model has no per-word rule iterator; any caller gets UnsupportedOperationException.","triggerScenarios":"Parser code or user code calling ruleIteratorByWord(int word, int loc, String featureSpec) — e.g., exact/reranking paths or unseen-word handling that enumerates rules per word — against a ChineseCharacterBasedLexicon.","commonSituations":"Swapping this lexicon into a parser configuration that expects a classic rule-iterating lexicon; custom scoring code iterating rules for a specific word index.","solutions":["Do not use rule iteration with this lexicon; use its score/sample API (score, sampleFrom) instead","Select a lexicon implementation that supports rule iteration if your pipeline requires it","Type-check the lexicon before calling ruleIteratorByWord and branch to an alternative scoring path","Patch the class to return an empty iterator if the caller merely iterates defensively"],"exampleFix":"// before\nIterator<IntTaggedWord> it = lexicon.ruleIteratorByWord(wordId, loc, null);\n// after\nif (lexicon instanceof ChineseCharacterBasedLexicon) {\n  double score = ((ChineseCharacterBasedLexicon) lexicon).score(...);\n} else {\n  Iterator<IntTaggedWord> it = lexicon.ruleIteratorByWord(wordId, loc, null);\n}","handlingStrategy":"type-guard","validationCode":"if (lexicon instanceof ChineseCharacterBasedLexicon)\n  throw new IllegalStateException(\"Use score()/sampleFrom(), not ruleIteratorByWord, with character lexicons\");","typeGuard":"boolean hasRuleIterator(Lexicon l) {\n  return !(l instanceof ChineseCharacterBasedLexicon);\n}","tryCatchPattern":"try { return lexicon.ruleIteratorByWord(word, loc, spec); } catch (UnsupportedOperationException e) { return Collections.<IntTaggedWord>emptyIterator(); }","preventionTips":["Do not mix character-based lexicons with rule-iterating parser paths","Read the lexicon class docs before calling iterator APIs","Prefer the score/sample API for this lexicon"],"tags":["parser","lexicon","unsupported-operation","iterator"],"backgroundTag":"unsupported-operation","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}