stanfordnlp/CoreNLP · error · UnsupportedOperationException

ChineseCharacterBasedLexicon has no rule iterator!

Error message

ChineseCharacterBasedLexicon has no rule iterator!

What it means

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.

Solutions

  1. Do not use rule iteration with this lexicon; use its score/sample API (score, sampleFrom) instead
  2. Select a lexicon implementation that supports rule iteration if your pipeline requires it
  3. Type-check the lexicon before calling ruleIteratorByWord and branch to an alternative scoring path
  4. Patch the class to return an empty iterator if the caller merely iterates defensively

Example fix

// before
Iterator<IntTaggedWord> it = lexicon.ruleIteratorByWord(wordId, loc, null);
// after
if (lexicon instanceof ChineseCharacterBasedLexicon) {
  double score = ((ChineseCharacterBasedLexicon) lexicon).score(...);
} else {
  Iterator<IntTaggedWord> it = lexicon.ruleIteratorByWord(wordId, loc, null);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (lexicon instanceof ChineseCharacterBasedLexicon)
  throw new IllegalStateException("Use score()/sampleFrom(), not ruleIteratorByWord, with character lexicons");

Type guard

boolean hasRuleIterator(Lexicon l) {
  return !(l instanceof ChineseCharacterBasedLexicon);
}

Try / catch

try { return lexicon.ruleIteratorByWord(word, loc, spec); } catch (UnsupportedOperationException e) { return Collections.<IntTaggedWord>emptyIterator(); }

Prevention

When it happens

Trigger: 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.

Common situations: Swapping this lexicon into a parser configuration that expects a classic rule-iterating lexicon; custom scoring code iterating rules for a specific word index.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/parser/lexparser/ChineseCharacterBasedLexicon.java:363

    return buf.toString();
  }

  /**
   * Samples over words regardless of POS: first samples POS, then samples
   * word according to that POS
   *
   * @return a sampled word
   */
  public String sampleFrom() {
    String POS = POSDistribution.sampleFrom();
    return sampleFrom(POS);
  }

  // don't think this should be used, but just in case...
  @Override
  public Iterator<IntTaggedWord> ruleIteratorByWord(int word, int loc, String featureSpec) {
    throw new UnsupportedOperationException("ChineseCharacterBasedLexicon has no rule iterator!");
  }

  // don't think this should be used, but just in case...
  @Override
  public Iterator<IntTaggedWord> ruleIteratorByWord(String word, int loc, String featureSpec) {
    throw new UnsupportedOperationException("ChineseCharacterBasedLexicon has no rule iterator!");
  }

  /** Returns the number of rules (tag rewrites as word) in the Lexicon.
   *  This method isn't yet implemented in this class.
   *  It currently just returns 0, which may or may not be helpful.
   */
  @Override
  public int numRules() {
    return 0;
  }

  private Distribution<Integer> getWordLengthDistribution() {

View on GitHub (pinned to 1b7edd19c4)