opennlp.tools.util

## Class Span

- java.lang.Object
- - opennlp.tools.util.Span

- All Implemented Interfaces:
    Serializable, Comparable<Span>
  Direct Known Subclasses:
    LinkedSpan

      public class Span
      extends Object
      implements Comparable<Span>, Serializable

  Class for storing start and end integer offsets.
  See Also:
    Serialized Form

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `Span(int s, int e)` Initializes a new Span Object.
    `Span(int s, int e, double prob)`
    `Span(int s, int e, String type)` Initializes a new Span Object.
    `Span(int s, int e, String type, double prob)` Initializes a new Span Object.
    `Span(Span span, double prob)` Creates a new immutable span based on an existing span, where the existing span did not include the prob
    `Span(Span span, int offset)` Initializes a new Span object with an existing Span which is shifted by an offset.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `compareTo(Span s)` Compares the specified span to the current span.
    `boolean` | `contains(int index)` Returns true if the specified index is contained inside this span.
    `boolean` | `contains(Span s)` Returns true if the specified span is contained by this span.
    `boolean` | `crosses(Span s)` Returns true is the specified span crosses this span.
    `boolean` | `equals(Object o)` Checks if the specified span is equal to the current span.
    `CharSequence` | `getCoveredText(CharSequence text)` Retrieves the string covered by the current span of the specified text.
    `int` | `getEnd()` Return the end of a span.
    `double` | `getProb()`
    `int` | `getStart()` Return the start of a span.
    `String` | `getType()` Retrieves the type of the span.
    `int` | `hashCode()` Generates a hash code of the current span.
    `boolean` | `intersects(Span s)` Returns true if the specified span intersects with this span.
    `int` | `length()` Returns the length of this span.
    `static String[]` | `spansToStrings(Span[] spans, CharSequence s)` Converts an array of `Span`s to an array of `String`s.
    `static String[]` | `spansToStrings(Span[] spans, String[] tokens)`
    `boolean` | `startsWith(Span s)` Returns true if the specified span is the begin of this span and the specified span is contained in this span.
    `String` | `toString()` Generates a human readable string.
    `Span` | `trim(CharSequence text)` Return a copy of this span with leading and trailing white spaces removed.

    - ### Methods inherited from class java.lang.Object

      `getClass, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### Span

          public Span(int s,
                      int e,
                      String type)

      Initializes a new Span Object. Sets the prob to 0 as default.
      Parameters:
        `s` - start of span.
        `e` - end of span, which is +1 more than the last element in the span.
        `type` - the type of the span

    - #### Span

          public Span(int s,
                      int e,
                      String type,
                      double prob)

      Initializes a new Span Object.
      Parameters:
        `s` - start of span.
        `e` - end of span, which is +1 more than the last element in the span.
        `type` - the type of the span
        `prob` - probability of span.

    - #### Span

          public Span(int s,
                      int e)

      Initializes a new Span Object. Sets the prob to 0 as default
      Parameters:
        `s` - start of span.
        `e` - end of span.

    - #### Span

          public Span(int s,
                      int e,
                      double prob)

      Parameters:
        `s` - the start of the span (the token index, not the char index)
        `e` - the end of the span (the token index, not the char index)
        `prob` -

    - #### Span

          public Span(Span span,
                      int offset)

      Initializes a new Span object with an existing Span which is shifted by an offset.
      Parameters:
        `span` -
        `offset` -

    - #### Span

          public Span(Span span,
                      double prob)

      Creates a new immutable span based on an existing span, where the existing span did not include the prob
      Parameters:
        `span` - the span that has no prob or the prob is incorrect and a new Span must be generated
        `prob` - the probability of the span

  - ### Method Detail

    - #### getStart

          public int getStart()

      Return the start of a span.
      Returns:
        the start of a span.

    - #### getEnd

          public int getEnd()

      Return the end of a span. Note: that the returned index is one past the actual end of the span in the text, or the first element past the end of the span.
      Returns:
        the end of a span.

    - #### getType

          public String getType()

      Retrieves the type of the span.
      Returns:
        the type or null if not set

    - #### length

          public int length()

      Returns the length of this span.
      Returns:
        the length of the span.

    - #### contains

          public boolean contains(Span s)

      Returns true if the specified span is contained by this span. Identical spans are considered to contain each other.
      Parameters:
        `s` - The span to compare with this span.
      Returns:
        true is the specified span is contained by this span; false otherwise.

    - #### contains

          public boolean contains(int index)

      Returns true if the specified index is contained inside this span. An index with the value of end is considered outside the span.
      Parameters:
        `index` - the index to test with this span.
      Returns:
        true if the span contains this specified index; false otherwise.

    - #### startsWith

          public boolean startsWith(Span s)

      Returns true if the specified span is the begin of this span and the specified span is contained in this span.
      Parameters:
        `s` - The span to compare with this span.
      Returns:
        true if the specified span starts with this span and is contained in this span; false otherwise

    - #### intersects

          public boolean intersects(Span s)

      Returns true if the specified span intersects with this span.
      Parameters:
        `s` - The span to compare with this span.
      Returns:
        true is the spans overlap; false otherwise.

    - #### crosses

          public boolean crosses(Span s)

      Returns true is the specified span crosses this span.
      Parameters:
        `s` - The span to compare with this span.
      Returns:
        true is the specified span overlaps this span and contains a non-overlapping section; false otherwise.

    - #### getCoveredText

          public CharSequence getCoveredText(CharSequence text)

      Retrieves the string covered by the current span of the specified text.
      Parameters:
        `text` -
      Returns:
        the substring covered by the current span

    - #### trim

          public Span trim(CharSequence text)

      Return a copy of this span with leading and trailing white spaces removed.
      Parameters:
        `text` -
      Returns:
        the trimmed span or the same object if already trimmed

    - #### compareTo

          public int compareTo(Span s)

      Compares the specified span to the current span.
      Specified by:
        `compareTo` in interface `Comparable<Span>`

    - #### hashCode

          public int hashCode()

      Generates a hash code of the current span.
      Overrides:
        `hashCode` in class `Object`

    - #### equals

          public boolean equals(Object o)

      Checks if the specified span is equal to the current span.
      Overrides:
        `equals` in class `Object`

    - #### toString

          public String toString()

      Generates a human readable string.
      Overrides:
        `toString` in class `Object`

    - #### spansToStrings

          public static String[] spansToStrings(Span[] spans,
                                                CharSequence s)

      Converts an array of `Span`s to an array of `String`s.
      Parameters:
        `spans` -
        `s` -
      Returns:
        the strings

    - #### spansToStrings

          public static String[] spansToStrings(Span[] spans,
                                                String[] tokens)

    - #### getProb

          public double getProb()

Copyright © 2026 The Apache Software Foundation. All rights reserved.