{"record":{"id":"c942f589d2253e6e","repo":"stanfordnlp/CoreNLP","slug":"span-must-be-entirely-contained-in-the-sentence","errorCode":null,"errorMessage":"Span must be entirely contained in the sentence: ","messagePattern":"Span must be entirely contained in the sentence: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/simple/SentenceAlgorithms.java","lineNumber":337,"sourceCode":"  /** @see SentenceAlgorithms#allSpans(Function, int) */\n  public Iterable<List<String>> allSpans() {\n    return allSpans(Sentence::words, sentence.length());\n  }\n\n  /**\n   * Select the most common element of the given type in the given span.\n   * This is useful for, e.g., finding the most likely NER span of a given span, or the most\n   * likely POS tag of a given span.\n   * Null entries are removed.\n   *\n   * @param span The span of the sentence to find the mode element in. This must be entirely contained in the sentence.\n   * @param selector The property of the sentence we are getting the mode of. For example, <code>Sentence::posTags</code>\n   * @param <E> The type of the element we are getting.\n   * @return The most common element of the given property in the sentence.\n   */\n  public <E> E modeInSpan(Span span, Function<Sentence, List<E>> selector) {\n    if (!Span.fromValues(0, sentence.length()).contains(span)) {\n      throw new IllegalArgumentException(\"Span must be entirely contained in the sentence: \" + span + \" (sentence length=\" + sentence.length() + \")\");\n    }\n    Counter<E> candidates = new ClassicCounter<>();\n    for (int i : span) {\n      candidates.incrementCount(selector.apply(sentence).get(i));\n    }\n    candidates.remove(null);\n    return Counters.argmax(candidates);\n  }\n\n\n  /**\n   * Run a proper BFS over a dependency graph, finding the shortest path between two vertices.\n   *\n   * @param start The start index.\n   * @param end The end index.\n   * @param selector The selector to use for the word nodes.\n   *\n   * @return A path string, analogous to {@link #dependencyPathBetween(int, int)}","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/simple/SentenceAlgorithms.java#L319-L355","documentation":"modeInSpan computes the modal value of a sentence property over a given Span. Before iterating, it verifies the span lies within [0, sentence.length()) using Span.contains(); if the span extends beyond the sentence it throws IllegalArgumentException. This guards against out-of-bounds access when indexing into the selector's returned list.","triggerScenarios":"Calling sentenceAlgorithms.modeInSpan(span, selector) with a Span whose end index exceeds sentence.length(), a negative start, or a span built for a different (longer) sentence.","commonSituations":"Reusing span indices computed from a previous sentence or a longer document; off-by-one errors building spans with an inclusive end index; running the same algorithm over multiple sentences without recomputing spans.","solutions":["Verify the span is within 0..sentence.length() before calling: Span.fromValues(0, sentence.length()).contains(span)","Clamp or recompute the span against the current sentence length","Ensure the span was created from the same Sentence/SentenceAlgorithms instance the call targets"],"exampleFix":"// before\nSpan span = Span.fromValues(0, 15);\nalgs.modeInSpan(span, Sentence::posTags); // throws if sentence has 10 tokens\n// after\nSpan span = Span.fromValues(0, Math.min(15, algs.sentence.length()));\nalgs.modeInSpan(span, Sentence::posTags);","handlingStrategy":"validation","validationCode":"if (span.start() < 0 || span.end() > algs.sentence.length()) {\n  throw new IllegalArgumentException(\"Span out of bounds for sentence of length \" + algs.sentence.length());\n}","typeGuard":"boolean isValidSpan(Span span, int sentenceLength) {\n  return span.start() >= 0 && span.end() <= sentenceLength;\n}","tryCatchPattern":"try {\n  E mode = algs.modeInSpan(span, Sentence::posTags);\n} catch (IllegalArgumentException e) {\n  // recompute or clamp span\n}","preventionTips":["Always derive spans from the same Sentence instance passed to the algorithm","Remember spans are end-exclusive like Span.fromValues(start, end)","Recompute spans when switching sentences or documents"],"tags":["java","argument-validation","nlp","span"],"backgroundTag":"value-out-of-range","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"}