{"record":{"id":"f9bb416d3b4927d5","repo":"stanfordnlp/CoreNLP","slug":"unimplemented-heuristic","errorCode":null,"errorMessage":"unimplemented heuristic","messagePattern":"unimplemented heuristic","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/wordseg/MaxMatchSegmenter.java","lineNumber":236,"sourceCode":"        DFSAState<Word, Integer> toState = tr.getTarget();\n        double lcost = tr.score();\n        int end = toState.stateID();\n        //logger.debug(\"start=\"+start+\" end=\"+end+\" word=\"+tr.getInput());\n        if (h == MatchHeuristic.MINWORDS) {\n          // Minimize number of words:\n          if (costs[start]+1 < costs[end]) {\n            costs[end] = costs[start]+lcost;\n            bptrs.set(end, tr);\n            //logger.debug(\"start=\"+start+\" end=\"+end+\" word=\"+tr.getInput());\n          }\n        } else if (h == MatchHeuristic.MAXWORDS) {\n          // Maximze number of words:\n          if (costs[start]+1 < costs[end]) {\n            costs[end] = costs[start]-lcost;\n            bptrs.set(end, tr);\n          }\n        } else {\n          throw new UnsupportedOperationException(\"unimplemented heuristic\");\n        }\n      }\n    }\n    // Extract min-cost path:\n    int i=len;\n    while (i>0) {\n      DFSATransition<Word, Integer> tr = bptrs.get(i);\n      DFSAState<Word, Integer> fromState = tr.getSource();\n      Word word = tr.getInput();\n      if (!word.word().equals(\" \"))\n        segmentedWords.add(0, word);\n      i = fromState.stateID();\n    }\n    if(DEBUG) {\n      // Print lattice density ([1,+inf[) : if equal to 1, it means\n      // there is only one segmentation using words of the lexicon.\n      double density = edgesNb*1.0/segmentedWords.size();\n      logger.debug(\"latticeDensity: \"+density+\" cost: \"+costs[len]);","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/wordseg/MaxMatchSegmenter.java#L218-L254","documentation":"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').","triggerScenarios":"Calling segmentWords with a MatchHeuristic other than the implemented ones (e.g. a heuristic not covered by the lattice cost switch).","commonSituations":"Extending MatchHeuristic with a new enum constant without updating segmentWords; passing a null/default heuristic; using an old API that maps heuristics differently.","solutions":["Use one of the supported MatchHeuristic values (e.g. max word count) when calling segmentWords","Implement the missing case in the switch inside segmentWords before using a custom heuristic","Check the MatchHeuristic javadoc/enumeration for implemented options"],"exampleFix":"// before\nList<Word> words = segmenter.segmentWords(myCustomHeuristic);\n// after\nList<Word> words = segmenter.segmentWords(MatchHeuristic.MAX_WORDS); // supported heuristic","handlingStrategy":"validation","validationCode":"Set<MatchHeuristic> supported = EnumSet.of(MatchHeuristic.MAX_WORDS); // per implemented switch cases\nif (!supported.contains(h)) {\n  h = MatchHeuristic.MAX_WORDS;\n}","typeGuard":null,"tryCatchPattern":"try {\n  words = segmenter.segmentWords(h);\n} catch (UnsupportedOperationException e) {\n  if (e.getMessage().contains(\"unimplemented heuristic\")) {\n    words = segmenter.segmentWords(MatchHeuristic.MAX_WORDS);\n  } else throw e;\n}","preventionTips":["Check MaxMatchSegmenter source for the set of heuristics with implemented cases","When adding new MatchHeuristic constants, update segmentWords' switch in the same change","Prefer the driver method maxMatchSegmentation, which uses supported defaults"],"tags":["java","segmentation","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}