Nasty.AST.Answer (Nasty v0.3.0)

View Source

Answer node representing an extracted answer to a question.

Used by question answering systems to represent candidate answers with confidence scores and supporting evidence.

Summary

Types

Answer span location within a document.

t()

Functions

Checks if answer meets a minimum confidence threshold.

Sorts answers by confidence (highest first).

Types

answer_span()

@type answer_span() ::
  {sentence_idx :: non_neg_integer(), token_start :: non_neg_integer(),
   token_end :: non_neg_integer()}

Answer span location within a document.

The span consists of:

  • sentence_idx - Index of the sentence containing the answer
  • token_start - Starting token index within the sentence
  • token_end - Ending token index (inclusive) within the sentence

t()

@type t() :: %Nasty.AST.Answer{
  confidence: float(),
  language: Nasty.AST.Node.language(),
  reasoning: String.t() | nil,
  source_sentence: Nasty.AST.Sentence.t() | nil,
  span: answer_span() | nil,
  text: String.t()
}

Functions

confident?(arg1, threshold)

@spec confident?(t(), float()) :: boolean()

Checks if answer meets a minimum confidence threshold.

Examples

iex> answer = Nasty.AST.Answer.new("test", 0.8, :en)
iex> Nasty.AST.Answer.confident?(answer, 0.7)
true
iex> Nasty.AST.Answer.confident?(answer, 0.9)
false

new(text, confidence, language, opts \\ [])

@spec new(String.t(), float(), Nasty.AST.Node.language(), keyword()) :: t()

Creates a new answer.

Examples

iex> answer = Nasty.AST.Answer.new("John Smith", 0.95, :en)
iex> answer.text
"John Smith"
iex> answer.confidence
0.95

sort_by_confidence(answers)

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

Sorts answers by confidence (highest first).

Examples

iex> answers = [
...>   Nasty.AST.Answer.new("low", 0.3, :en),
...>   Nasty.AST.Answer.new("high", 0.9, :en),
...>   Nasty.AST.Answer.new("mid", 0.6, :en)
...> ]
iex> sorted = Nasty.AST.Answer.sort_by_confidence(answers)
iex> Enum.map(sorted, & &1.text)
["high", "mid", "low"]