{"record":{"id":"0ae45f26bd973e9a","repo":"stanfordnlp/CoreNLP","slug":"segmentwords-must-be-run-first","errorCode":null,"errorMessage":"segmentWords must be run first","messagePattern":"segmentWords must be run first","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/wordseg/MaxMatchSegmenter.java","lineNumber":202,"sourceCode":"  public ArrayList<Word> maxMatchSegmentation() {\n    return segmentWords(MatchHeuristic.MINWORDS);\n  }\n\n  /**\n   * Returns the lexicon-based segmentation following heuristic h.\n   * Note that buildSegmentationLattice must be run first.\n   * Two heuristics are currently available -- MINWORDS and MAXWORDS --\n   * to respectively minimize and maximize the number of segment\n   * (where each segment is a lexicon word, if possible).\n   *\n   * @param h Heuristic to use for segmentation.\n   * @return Segmented sentence.\n   * @throws UnsupportedOperationException\n   * @see #buildSegmentationLattice\n   */\n  public ArrayList<Word> segmentWords(MatchHeuristic h) throws UnsupportedOperationException {\n    if(lattice==null || len < 0)\n      throw new UnsupportedOperationException(\"segmentWords must be run first\");\n    List<Word> segmentedWords = new ArrayList<>();\n    // Init dynamic programming:\n    double[] costs = new double[len+1];\n    List<DFSATransition<Word, Integer>> bptrs = new ArrayList<>();\n    for (int i = 0; i < len + 1; ++i) {\n      bptrs.add(null);\n    }\n    costs[0]=0.0;\n    for (int i=1; i<=len; ++i)\n       costs[i] = Double.MAX_VALUE;\n    // DP:\n    for (int start=0; start<len; ++start) {\n      DFSAState<Word, Integer> fromState = states.get(start);\n      Collection<DFSATransition<Word, Integer>> trs = fromState.transitions();\n      for (DFSATransition<Word, Integer> tr : trs) {\n        DFSAState<Word, Integer> toState = tr.getTarget();\n        double lcost = tr.score();\n        int end = toState.stateID();","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/wordseg/MaxMatchSegmenter.java#L184-L220","documentation":"MaxMatchSegmenter.segmentWords(MatchHeuristic) performs dynamic programming over the segmentation lattice, which must first be built by a previous call. If the lattice is null or len < 0, the method throws UnsupportedOperationException because there is nothing to segment.","triggerScenarios":"Calling segmentWords(h) before buildSegmentationLattice (or before the internal lattice-producing pass) has been invoked, or calling it a second time after state was reset.","commonSituations":"Using MaxMatchSegmenter programmatically and calling the DP method directly instead of going through the driver that builds the lattice first; calling main-style flows out of order.","solutions":["Call the lattice-building step (buildSegmentationLattice / maxMatchSegmentation) before segmentWords","Restructure code so segmentWords is only invoked after successful lattice construction","Check lattice state before calling and rebuild if null"],"exampleFix":"// before\nList<Word> words = segmenter.segmentWords(MatchHeuristic.MAX_WORDS);\n// after\nsegmenter.buildSegmentationLattice(sentence);\nList<Word> words = segmenter.segmentWords(MatchHeuristic.MAX_WORDS);","handlingStrategy":"validation","validationCode":"if (segmenterLatticeReady == false) {\n  segmenter.buildSegmentationLattice(sentence);\n}\nList<Word> words = segmenter.segmentWords(heuristic);","typeGuard":null,"tryCatchPattern":"try {\n  words = segmenter.segmentWords(h);\n} catch (UnsupportedOperationException e) {\n  if (e.getMessage().contains(\"must be run first\")) {\n    segmenter.buildSegmentationLattice(sentence);\n    words = segmenter.segmentWords(h);\n  } else throw e;\n}","preventionTips":["Call buildSegmentationLattice (or the driver method) before any DP call","Wrap multi-step segmenter usage in a single facade method that enforces ordering","Never call segmentWords twice on stale/reset state"],"tags":["java","segmentation","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"}