Record Class TermVector

java.lang.Object
java.lang.Record
opennlp.tools.termvector.TermVector
Record Components:
term - The term string. Must not be null.
frequency - The number of occurrences in the document. Must be at least one.
spans - The occurrence spans in original text coordinates, one per occurrence, or an empty list for a scoring-only vector. Must not be null or contain null and, when non-empty, must hold exactly frequency spans.

@ThreadSafe public record TermVector(String term, int frequency, List<Span> spans) extends Record
One entry of a term vector layer: a term, how often it occurs in the document, and where.

The term() is the term's identity as the producing annotator determined it, typically a normalized form; the spans() are the occurrence offsets and always point into the document's original text, never into a normalized form, so an index consumer can highlight every occurrence in what the caller supplied.

A term vector comes in one of two shapes, told apart by whether spans() is empty. A full vector carries one span per occurrence, so spans().size() == frequency(). A scoring-only vector carries no spans at all, so consumers that only need term frequencies do not pay for offset storage. There is no third shape: a non-empty span list must match the frequency exactly.

Instances are immutable: the span list is copied on construction and the copy is unmodifiable.

Since:
3.0.0
  • Constructor Details

    • TermVector

      public TermVector(String term, int frequency, List<Span> spans)
      Validates the term vector and detaches the span list from the caller's input.
      Throws:
      IllegalArgumentException - Thrown if term is null, frequency is below one, spans is or contains null, or a non-empty spans list does not hold exactly frequency spans.
  • Method Details

    • withSpans

      public static TermVector withSpans(String term, List<Span> spans)
      Creates a full TermVector whose frequency is derived from the occurrence spans.
      Parameters:
      term - The term string. Must not be null.
      spans - The occurrence spans in original text coordinates. Must not be null or empty.
      Returns:
      A TermVector with frequency() == spans.size(). Never null.
      Throws:
      IllegalArgumentException - Thrown if term is null or spans is null or empty.
    • count

      public static TermVector count(String term, int frequency)
      Creates a scoring-only TermVector that carries the occurrence count without any offsets.
      Parameters:
      term - The term string. Must not be null.
      frequency - The number of occurrences in the document. Must be at least one.
      Returns:
      A TermVector whose spans() is empty. Never null.
      Throws:
      IllegalArgumentException - Thrown if term is null or frequency is below one.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • term

      public String term()
      Returns the value of the term record component.
      Returns:
      the value of the term record component
    • frequency

      public int frequency()
      Returns the value of the frequency record component.
      Returns:
      the value of the frequency record component
    • spans

      public List<Span> spans()
      Returns the value of the spans record component.
      Returns:
      the value of the spans record component