Nasty.AST.Document (Nasty v0.3.0)

View Source

Document node representing the root of the AST.

A document is the top-level structure containing one or more paragraphs. It represents an entire text unit (article, email, book chapter, etc.) with metadata about the source and language.

Summary

Types

Document metadata.

t()

Functions

Returns all sentences in the document (flattened).

Returns the first paragraph of the document.

Counts total number of paragraphs.

Counts total number of sentences across all paragraphs.

Types

metadata()

@type metadata() :: %{required(atom()) => term()}

Document metadata.

Optional information about the document:

  • title - Document title
  • author - Author name(s)
  • date - Creation/modification date
  • source - Original source (URL, file path, etc.)
  • Custom fields as needed

t()

@type t() :: %Nasty.AST.Document{
  coref_chains: [Nasty.AST.Semantic.CorefChain.t()] | nil,
  language: Nasty.AST.Node.language(),
  metadata: metadata(),
  paragraphs: [Nasty.AST.Paragraph.t()],
  semantic_frames: [Nasty.AST.Semantic.Frame.t()] | nil,
  span: Nasty.AST.Node.span()
}

Functions

all_sentences(document)

@spec all_sentences(t()) :: [Nasty.AST.Sentence.t()]

Returns all sentences in the document (flattened).

Examples

iex> doc = %Nasty.AST.Document{paragraphs: [p1, p2], ...}
iex> sentences = Nasty.AST.Document.all_sentences(doc)
iex> is_list(sentences)
true

first_paragraph(document)

@spec first_paragraph(t()) :: Nasty.AST.Paragraph.t() | nil

Returns the first paragraph of the document.

Examples

iex> doc = %Nasty.AST.Document{paragraphs: [p1, p2], ...}
iex> Nasty.AST.Document.first_paragraph(doc)
p1

new(paragraphs, language, span, opts \\ [])

Creates a new document.

Examples

iex> span = Nasty.AST.Node.make_span({1, 0}, 0, {100, 0}, 5000)
iex> paragraphs = [p1, p2, p3]
iex> doc = Nasty.AST.Document.new(paragraphs, :en, span)
iex> length(doc.paragraphs)
3

iex> doc = Nasty.AST.Document.new(paragraphs, :en, span, 
...>   metadata: %{title: "My Essay", author: "Jane Doe"})
iex> doc.metadata.title
"My Essay"

paragraph_count(document)

@spec paragraph_count(t()) :: non_neg_integer()

Counts total number of paragraphs.

Examples

iex> doc = %Nasty.AST.Document{paragraphs: [p1, p2, p3], ...}
iex> Nasty.AST.Document.paragraph_count(doc)
3

sentence_count(doc)

@spec sentence_count(t()) :: non_neg_integer()

Counts total number of sentences across all paragraphs.

Examples

iex> doc = %Nasty.AST.Document{paragraphs: [p1, p2, p3], ...}
iex> Nasty.AST.Document.sentence_count(doc)
10