Nasty.Rendering.Text (Nasty v0.3.0)

View Source

Renders AST nodes back to natural language text.

This module implements surface realization: converting the abstract syntactic structure back into readable text with proper word order, agreement, punctuation, and capitalization.

Features

  • Surface realization (choose word forms)
  • Agreement (subject-verb, determiner-noun)
  • Word order (handle variations)
  • Punctuation insertion
  • Formatting (capitalization, spacing)

Examples

iex> token = %Nasty.AST.Token{text: "cat", pos_tag: :noun, language: :en, span: span}
iex> Nasty.Rendering.Text.render(token)
{:ok, "cat"}

iex> Nasty.Rendering.Text.render(document)
{:ok, "The quick brown fox jumps over the lazy dog."}

Summary

Types

Rendering options.

Functions

Applies subject-verb agreement rules for English.

Renders an AST node to text.

Renders an AST node to text, raising on error.

Types

options()

@type options() :: [
  capitalize_sentences: boolean(),
  add_punctuation: boolean(),
  paragraph_separator: String.t(),
  format: :text | :markdown | :html
]

Rendering options.

  • :capitalize_sentences - Whether to capitalize first word of sentences (default: true)
  • :add_punctuation - Whether to add sentence-ending punctuation (default: true)
  • :paragraph_separator - String to separate paragraphs (default: "\n\n")
  • :format - Output format (default: :text)

Functions

apply_agreement(subject, verb, language)

@spec apply_agreement(String.t(), String.t(), atom()) :: {String.t(), String.t()}

Applies subject-verb agreement rules for English.

This is a helper for generating text with correct agreement.

Examples

iex> Nasty.Rendering.Text.apply_agreement("cat", "run", :en)
{"cat", "runs"}

iex> Nasty.Rendering.Text.apply_agreement("cats", "run", :en)
{"cats", "run"}

render(node, opts \\ [])

@spec render(term(), options()) :: {:ok, String.t()} | {:error, term()}

Renders an AST node to text.

Examples

iex> Nasty.Rendering.Text.render(document)
{:ok, "The cat sat on the mat."}

iex> Nasty.Rendering.Text.render(document, capitalize_sentences: false)
{:ok, "the cat sat on the mat."}

render!(node, opts \\ [])

@spec render!(term(), options()) :: String.t()

Renders an AST node to text, raising on error.

Examples

iex> Nasty.Rendering.Text.render!(document)
"The cat sat on the mat."