TriplesNumericLiteralsFactory

class TriplesNumericLiteralsFactory(*, numeric_literals: ndarray, literals_to_id: Mapping[str, int], **kwargs)[source]

Bases: TriplesFactory

Create multi-modal instances given the path to triples.

Initialize the multi-modal triples factory.

Parameters:
  • numeric_literals (ndarray) – shape: (num_entities, num_literals) the numeric literals as a dense matrix.

  • literals_to_id (Mapping[str, int]) – a mapping from literal names to their IDs, i.e., the columns in the numeric_literals matrix.

  • kwargs – additional keyword-based parameters passed to TriplesFactory.__init__().

Attributes Summary

file_name_literal_to_id

file_name_numeric_literals

literal_shape

Return the shape of the literals.

Methods Summary

clone_and_exchange_triples(mapped_triples[, ...])

Create a new triples factory sharing everything except the triples.

from_labeled_triples(triples, *[, ...])

Create a new triples factory from label-based triples.

from_path(path, *[, path_to_numeric_triples])

Create a new triples factory from triples stored in a file.

get_numeric_literals_tensor()

Return the numeric literals as a tensor.

iter_extra_repr()

Iterate over extra_repr components.

to_path_binary(path)

Save triples factory to path in (PyTorch's .pt) binary format.

Attributes Documentation

file_name_literal_to_id: ClassVar[str] = 'literal_to_id'
file_name_numeric_literals: ClassVar[str] = 'literals'
literal_shape

Return the shape of the literals.

Methods Documentation

clone_and_exchange_triples(mapped_triples: Tensor, extra_metadata: dict[str, Any] | None = None, keep_metadata: bool = True, create_inverse_triples: bool | None = None) TriplesNumericLiteralsFactory[source]

Create a new triples factory sharing everything except the triples.

Note

We use shallow copies.

Parameters:
  • mapped_triples (Tensor) – The new mapped triples.

  • extra_metadata (dict[str, Any] | None) – Extra metadata to include in the new triples factory. If keep_metadata is true, the dictionaries will be unioned with precedence taken on keys from extra_metadata.

  • keep_metadata (bool) – Pass the current factory’s metadata to the new triples factory

  • create_inverse_triples (bool | None) – Change inverse triple creation flag. If None, use flag from this factory.

Returns:

The new factory.

Return type:

TriplesNumericLiteralsFactory

classmethod from_labeled_triples(triples: ndarray, *, numeric_triples: ndarray = None, **kwargs) TriplesNumericLiteralsFactory[source]

Create a new triples factory from label-based triples.

Parameters:
  • triples (ndarray) – shape: (n, 3), dtype: str The label-based triples.

  • create_inverse_triples – Whether to create inverse triples.

  • entity_to_id – The mapping from entity labels to ID. If None, create a new one from the triples.

  • relation_to_id – The mapping from relations labels to ID. If None, create a new one from the triples.

  • compact_id – Whether to compact IDs such that the IDs are consecutive.

  • filter_out_candidate_inverse_relations – Whether to remove triples with relations with the inverse suffix.

  • metadata – Arbitrary key/value pairs to store as metadata

  • numeric_triples (ndarray)

Returns:

A new triples factory.

Return type:

TriplesNumericLiteralsFactory

classmethod from_path(path: str | Path | TextIO, *, path_to_numeric_triples: None | str | Path | TextIO = None, **kwargs) TriplesNumericLiteralsFactory[source]

Create a new triples factory from triples stored in a file.

Parameters:
  • path (str | Path | TextIO) – The path where the label-based triples are stored.

  • create_inverse_triples – Whether to create inverse triples.

  • entity_to_id – The mapping from entity labels to ID. If None, create a new one from the triples.

  • relation_to_id – The mapping from relations labels to ID. If None, create a new one from the triples.

  • compact_id – Whether to compact IDs such that the IDs are consecutive.

  • metadata – Arbitrary key/value pairs to store as metadata with the triples factory. Do not include path as a key because it is automatically taken from the path kwarg to this function.

  • load_triples_kwargs – Optional keyword arguments to pass to load_triples(). Could include the delimiter or a column_remapping.

  • kwargs – additional keyword-based parameters, which are ignored.

  • path_to_numeric_triples (None | str | Path | TextIO)

Returns:

A new triples factory.

Return type:

TriplesNumericLiteralsFactory

get_numeric_literals_tensor() Tensor[source]

Return the numeric literals as a tensor.

Return type:

Tensor

iter_extra_repr() Iterable[str][source]

Iterate over extra_repr components.

Return type:

Iterable[str]

to_path_binary(path: str | Path | TextIO) Path[source]

Save triples factory to path in (PyTorch’s .pt) binary format.

Parameters:

path (str | Path | TextIO) – The path to store the triples factory to.

Returns:

The path to the file that got dumped

Return type:

Path