flair.data.Span#

class flair.data.Span(tokens)View on GitHub#

Bases: _PartOfSentence

Represents a contiguous sequence of Tokens within a Sentence.

Used for entities, phrases, etc. Implements caching via __new__ within Sentence.

tokens#

The list of tokens constituting the span.

Type:

list[Token]

__init__(tokens)View on GitHub#

Initializes the Span (called only once per unique span via __new__).

Parameters:

tokens (list[Token]) – The list of tokens forming the span.

Methods

__init__(tokens)

Initializes the Span (called only once per unique span via __new__).

add_label(typename, value[, score])

Adds a label, propagating it to the parent Sentence's layer.

add_metadata(key, value)

Adds a key-value pair to the data point's metadata.

clear_embeddings([embedding_names])

Removes stored embeddings to free memory.

get_each_embedding([embedding_names])

Retrieves a list of individual embedding tensors.

get_embedding([names])

Retrieves embeddings, concatenating if multiple names are given or if names is None.

get_label([label_type, zero_tag_value])

Retrieves the primary label for a given type, or a default 'O' label.

get_labels([typename])

Retrieves all labels for a specific annotation layer.

get_metadata(key)

Retrieves metadata associated with the given key.

has_label(typename)

Checks if the data point has at least one label for the given annotation type.

has_metadata(key)

Checks if the data point has metadata for the given key.

remove_labels(typename)

Removes labels of a type, also removing them from the parent Sentence layer.

set_embedding(name, vector)

Stores an embedding tensor under a given name.

set_label(typename, value[, score])

Sets a label (overwriting), propagating the change to the parent Sentence.

to(device[, pin_memory])

Moves all stored embedding tensors to the specified device.

to_dict([tag_type])

Attributes

embedding

Returns embeddings stored directly on the Span object (if any).

end_position

Character offset where the span ends (end of the last token, exclusive).

labels

Returns a list of all labels from all annotation layers.

score

Shortcut property for the score of the first label added.

start_position

Character offset where the span begins (start of the first token).

tag

Shortcut property for the value of the first label added.

text

The combined text of tokens in the span, respecting whitespace offsets.

unlabeled_identifier

<end_idx>]: "<text_preview>"'.

property start_position: int#

Character offset where the span begins (start of the first token).

property end_position: int#

Character offset where the span ends (end of the last token, exclusive).

property text: str#

The combined text of tokens in the span, respecting whitespace offsets.

property unlabeled_identifier: str#

<end_idx>]: “<text_preview>”’.

Type:

String identifier

Type:

‘Span[<start_idx>

property embedding: Tensor#

Returns embeddings stored directly on the Span object (if any).

to_dict(tag_type=None)View on GitHub#