stanfordnlp/CoreNLP · error · UnsupportedOperationException

unimplemented heuristic

Error message

unimplemented heuristic

What it means

Inside segmentWords, the DP loop switches on the MatchHeuristic to decide edge costs; only a subset of heuristics is implemented. Passing a heuristic value with no case in the switch reaches the else branch and throws UnsupportedOperationException('unimplemented heuristic').

Solutions

  1. Use one of the supported MatchHeuristic values (e.g. max word count) when calling segmentWords
  2. Implement the missing case in the switch inside segmentWords before using a custom heuristic
  3. Check the MatchHeuristic javadoc/enumeration for implemented options

Example fix

// before
List<Word> words = segmenter.segmentWords(myCustomHeuristic);
// after
List<Word> words = segmenter.segmentWords(MatchHeuristic.MAX_WORDS); // supported heuristic
Defensive patterns

Strategy: validation

Validate before calling

Set<MatchHeuristic> supported = EnumSet.of(MatchHeuristic.MAX_WORDS); // per implemented switch cases
if (!supported.contains(h)) {
  h = MatchHeuristic.MAX_WORDS;
}

Try / catch

try {
  words = segmenter.segmentWords(h);
} catch (UnsupportedOperationException e) {
  if (e.getMessage().contains("unimplemented heuristic")) {
    words = segmenter.segmentWords(MatchHeuristic.MAX_WORDS);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling segmentWords with a MatchHeuristic other than the implemented ones (e.g. a heuristic not covered by the lattice cost switch).

Common situations: Extending MatchHeuristic with a new enum constant without updating segmentWords; passing a null/default heuristic; using an old API that maps heuristics differently.

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/f9bb416d3b4927d5. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/wordseg/MaxMatchSegmenter.java:236

        DFSAState<Word, Integer> toState = tr.getTarget();
        double lcost = tr.score();
        int end = toState.stateID();
        //logger.debug("start="+start+" end="+end+" word="+tr.getInput());
        if (h == MatchHeuristic.MINWORDS) {
          // Minimize number of words:
          if (costs[start]+1 < costs[end]) {
            costs[end] = costs[start]+lcost;
            bptrs.set(end, tr);
            //logger.debug("start="+start+" end="+end+" word="+tr.getInput());
          }
        } else if (h == MatchHeuristic.MAXWORDS) {
          // Maximze number of words:
          if (costs[start]+1 < costs[end]) {
            costs[end] = costs[start]-lcost;
            bptrs.set(end, tr);
          }
        } else {
          throw new UnsupportedOperationException("unimplemented heuristic");
        }
      }
    }
    // Extract min-cost path:
    int i=len;
    while (i>0) {
      DFSATransition<Word, Integer> tr = bptrs.get(i);
      DFSAState<Word, Integer> fromState = tr.getSource();
      Word word = tr.getInput();
      if (!word.word().equals(" "))
        segmentedWords.add(0, word);
      i = fromState.stateID();
    }
    if(DEBUG) {
      // Print lattice density ([1,+inf[) : if equal to 1, it means
      // there is only one segmentation using words of the lexicon.
      double density = edgesNb*1.0/segmentedWords.size();
      logger.debug("latticeDensity: "+density+" cost: "+costs[len]);

View on GitHub (pinned to 1b7edd19c4)