Nasty.AST.Relation (Nasty v0.3.0)

View Source

Represents a semantic relation between two entities.

Relations capture structured information like employment ("works_at"), organization structure ("founded"), location ("located_in"), etc.

Examples

%Relation{
  type: :works_at,
  subject: %Entity{text: "John Smith", type: :person},
  object: %Entity{text: "Google", type: :org},
  confidence: 0.9,
  evidence: "John Smith works at Google",
  span: {{1, 1}, {1, 30}},
  language: :en
}

Summary

Functions

Filters relations by minimum confidence threshold.

Filters relations by type.

Returns the inverse of a relation type.

Inverts a relation (swaps subject and object, inverts type).

Gets the text representation of object.

Sorts relations by confidence (descending).

Gets the text representation of subject.

Converts a relation to a human-readable string.

Types

relation_type()

@type relation_type() ::
  :works_at
  | :employed_by
  | :founded
  | :acquired_by
  | :subsidiary_of
  | :located_in
  | :based_in
  | :headquarters_in
  | :born_in
  | :educated_at
  | :member_of
  | :ceo_of
  | :part_of
  | :occurred_on
  | :founded_in
  | atom()

t()

@type t() :: %Nasty.AST.Relation{
  confidence: float(),
  evidence: String.t() | nil,
  language: Nasty.AST.Node.language(),
  metadata: map(),
  object: Nasty.AST.Semantic.Entity.t() | String.t(),
  span: Nasty.AST.Node.span() | nil,
  subject: Nasty.AST.Semantic.Entity.t() | String.t(),
  type: relation_type()
}

Functions

filter_by_confidence(relations, min_confidence)

@spec filter_by_confidence([t()], float()) :: [t()]

Filters relations by minimum confidence threshold.

Examples

iex> Relation.filter_by_confidence(relations, 0.7)
[%Relation{confidence: 0.9}, %Relation{confidence: 0.8}]

filter_by_type(relations, type)

@spec filter_by_type([t()], relation_type()) :: [t()]

Filters relations by type.

Examples

iex> Relation.filter_by_type(relations, :works_at)
[%Relation{type: :works_at}, ...]

inverse_type(type)

@spec inverse_type(relation_type()) :: relation_type()

Returns the inverse of a relation type.

Examples

iex> Relation.inverse_type(:works_at)
:employed_by

iex> Relation.inverse_type(:founded)
:founded_by

invert(relation)

@spec invert(t()) :: t()

Inverts a relation (swaps subject and object, inverts type).

Examples

iex> relation = Relation.new(:works_at, john, google, :en)
iex> Relation.invert(relation)
%Relation{type: :employed_by, subject: google, object: john}

new(type, subject, object, language, opts \\ [])

Creates a new relation.

Examples

iex> Relation.new(:works_at, subject, object, :en)
%Relation{type: :works_at, subject: subject, object: object, language: :en}

iex> Relation.new(:works_at, subject, object, :en, confidence: 0.8)
%Relation{type: :works_at, confidence: 0.8, ...}

object_text(relation)

@spec object_text(t()) :: String.t()

Gets the text representation of object.

sort_by_confidence(relations)

@spec sort_by_confidence([t()]) :: [t()]

Sorts relations by confidence (descending).

Examples

iex> relations = [%Relation{confidence: 0.5}, %Relation{confidence: 0.9}]
iex> Relation.sort_by_confidence(relations)
[%Relation{confidence: 0.9}, %Relation{confidence: 0.5}]

subject_text(relation)

@spec subject_text(t()) :: String.t()

Gets the text representation of subject.

to_string(relation)

@spec to_string(t()) :: String.t()

Converts a relation to a human-readable string.

Examples

iex> Relation.to_string(relation)
"John Smith works_at Google (confidence: 0.9)"