flair.data.Relation#

class flair.data.Relation(first, second)View on GitHub#

Bases: _PartOfSentence

Represents a directed relationship between two Spans in the same Sentence.

Used for Relation Extraction. Caching via __new__ ensures uniqueness.

first#

The head Span of the relation.

Type:

Span

second#

The tail Span of the relation.

Type:

Span

__init__(first, second)View on GitHub#

Initializes the Relation (called only once per unique relation via __new__).

Parameters:
  • first (Span) – The head span.

  • second (Span) – The tail span.

Methods

__init__(first, second)

Initializes the Relation (called only once per unique relation 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

Placeholder for relation embedding (usually computed on the fly).

end_position

Character offset of the latest end position of the two spans.

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 of the earliest start position of the two spans.

tag

Shortcut property for the value of the first label added.

text

'<head_text_preview> -> <tail_text_preview>'.

unlabeled_identifier

String identifier including span indices and text previews.

property tag#

Shortcut property for the value of the first label added.

property text: str#

‘<head_text_preview> -> <tail_text_preview>’.

Type:

A simple textual representation

property unlabeled_identifier: str#

String identifier including span indices and text previews.

property start_position: int#

Character offset of the earliest start position of the two spans.

property end_position: int#

Character offset of the latest end position of the two spans.

property embedding: Tensor#

Placeholder for relation embedding (usually computed on the fly).

to_dict(tag_type=None)View on GitHub#