text

package
v0.5.0-beta.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package text is the script pipeline shared by the Studio (M1), Stories and Audiobook (M3) and Dubbing (M4): segmentation into TTS chunks with source offsets, content-addressed chunk keys, word counts and runtime estimates.

Subpackages:

  • markup: the SSML-lite parser ([pause 500ms], [voice:NAME], # chapters…)
  • normalize: numbers, times, dates, currency and abbreviations → words
  • lexicon: the pronunciation dictionary and [[word|say]] overrides
  • autocast: speaker detection for Stories

Everything here is pure, allocation-light and goroutine-safe. Offsets are byte offsets into the source string; lengths that users see (max chars per chunk) count runes. Nothing here returns user-visible strings: problems are reported as Diagnostics (a code plus parameters) that the UI translates.

Index

Constants

View Source
const (
	WordsPerMinute = 155
	CharsPerSecond = 4.5
)

Reading rates used by Estimate: 155 words a minute for spaced scripts (the audiobook industry's narration rate) and 4.5 characters a second for unspaced ones (Mandarin/Japanese narration).

View Source
const DefaultMaxChars = 800

DefaultMaxChars is the TTS chunk limit (runes) when Options.MaxChars is 0.

Variables

This section is empty.

Functions

func Count

func Count(s string) (words, chars int)

Count counts the words of s the way a script editor's stats line does: words is the number of space-separated words in spaced scripts (Latin, Cyrillic, Hangul…); chars is the number of characters in unspaced scripts (Han, kana, Thai…), each of which is read on its own. Bracket tags are not counted.

func Estimate

func Estimate(words, chars int) time.Duration

Estimate is the expected narration time for words spaced-script words and chars unspaced-script characters.

func IsCJK

func IsCJK(r rune) bool

IsCJK reports whether r is a Han ideograph, kana, Bopomofo, Hangul or a CJK symbol/full-width form: characters that are typeset without spaces and counted one per "word" in CJK runtime estimates.

func IsHeading

func IsHeading(line string) bool

IsHeading reports whether line is a chapter heading: "#" alone or "#" followed by a space. "##" and deeper are body text.

func IsUnspaced

func IsUnspaced(r rune) bool

IsUnspaced reports whether r belongs to a script written without spaces between words (Han, kana, Thai, Lao, Khmer, Myanmar, Tibetan), where a term may match anywhere instead of only at word boundaries. Hangul is spaced and is not included.

func IsWordRune

func IsWordRune(r rune) bool

IsWordRune reports whether r is part of a word: a letter, a digit or a combining mark.

func Key

func Key(text string, params map[string]string) string

Key is the content address of a synthesised chunk: a hex SHA-256 of the normalised text and every parameter that changes the audio (model, revision, voice, language, seed, speed, steps…). Params are hashed in key order, so map iteration order does not matter; format numbers the same way every time (strconv.FormatFloat(v, 'g', -1, 64)).

func NormalizeForKey

func NormalizeForKey(s string) string

NormalizeForKey is the text identity used by Key: Unicode NFC, runs of white space collapsed to one space, trimmed. Case and punctuation are kept because they change how a sentence is spoken.

func Outside

func Outside(s string, fn func(string) string) string

Outside calls fn for each maximal piece of s outside bracket tags, in order, and returns the concatenation of fn's results with the tags kept verbatim. It is how normalisation and the lexicon skip "[…]" spans.

func RuneLen

func RuneLen(s string) int

RuneLen is utf8.RuneCountInString, named for readability at call sites.

func SplitOverride

func SplitOverride(tag string) (word, say string, ok bool)

SplitOverride splits an inline pronunciation override "[[word|say]]" into its parts (trimmed). ok is false when tag is not of that form.

Types

type Chunk

type Chunk struct {
	Span
	Text      string
	Paragraph int  // index of the paragraph the chunk starts in
	Heading   bool // a "# Title" line
	Cut       Cut
}

Chunk is one piece of a script, with its source offsets for highlighting and caching. Text is src[Start:End]: trimmed, otherwise verbatim (it may still contain line breaks and tags).

func Chunks

func Chunks(src string, opt Options) []Chunk

Chunks splits src into chunks according to opt. It never splits inside a bracket tag ("[pause 1.5s]") or an override ("[[word|say]]"); a single tag longer than MaxChars becomes an oversized chunk rather than being cut.

type Cut

type Cut uint8

Cut says how a chunk's end was chosen.

const (
	CutNatural Cut = iota // end of a sentence, paragraph or text
	CutClause             // at a comma, semicolon, colon or dash
	CutLine               // at a line break inside a paragraph
	CutSpace              // between words
	CutHard               // anywhere (no better place in the window)
)

type Diagnostic

type Diagnostic struct {
	Code     string
	Severity Severity
	Span     Span
	Params   map[string]string
}

Diagnostic is one finding about a script, for validation UIs. Code is a stable identifier ("markup.unknown_tag"); Params carry the values a translated message needs ({"tag": "[foo]"}). Span points into the source.

type Mode

type Mode uint8

Mode chooses what one chunk holds.

const (
	// Pack packs whole sentences into chunks of at most MaxChars: the TTS
	// default, few chunks with natural boundaries.
	Pack Mode = iota
	// Sentences makes one chunk per sentence (Stories "split by sentences").
	Sentences
	// Paragraphs makes one chunk per paragraph.
	Paragraphs
	// Chapters makes one chunk per chapter body; "# Title" headings are
	// chunks of their own in every mode.
	Chapters
)

type Options

type Options struct {
	Mode Mode
	// MaxChars caps a chunk's length in runes: 0 means DefaultMaxChars, a
	// negative value means no cap. Oversized sentences are cut at a clause
	// mark, then a line break, then a space, then anywhere outside a tag.
	MaxChars int
	// CrossParagraphs lets Pack fill a chunk across blank lines. Headings
	// still break chunks.
	CrossParagraphs bool
}

Options configure Chunks.

type Paragraph

type Paragraph struct {
	Span
	Heading bool // a "# Title" line, always its own paragraph
}

Paragraph is a block of text separated from the next by a blank line.

func Paras

func Paras(src string) []Paragraph

Paras splits src into paragraphs at blank lines. When src has no blank line at all, every non-empty line is a paragraph (a script typed one line per thought). "# Title" lines are always paragraphs of their own.

type Severity

type Severity uint8

Severity ranks a diagnostic.

const (
	Info Severity = iota
	Warning
	Error
)

func (Severity) String

func (s Severity) String() string

type Span

type Span struct {
	Start, End int
}

Span is a half-open byte range [Start, End) into a source string.

func SentenceSpans

func SentenceSpans(src string) []Span

SentenceSpans returns the sentences of src, trimmed, in order.

func Tags

func Tags(s string) []Span

Tags returns the bracketed spans of s in order: "[tag]" and "[[override]]". A tag never crosses a line break; an unmatched "[" is plain text. When a "[" appears before the closing "]", the scan restarts there, so in "[a [b]" only "[b]" is a tag.

func (Span) Contains

func (s Span) Contains(i int) bool

Contains reports whether byte offset i lies inside the span.

func (Span) Len

func (s Span) Len() int

Len is the span's length in bytes.

func (Span) Of

func (s Span) Of(src string) string

Of returns the part of src the span covers.

func (Span) TrimSpace

func (s Span) TrimSpace(src string) Span

TrimSpace shrinks the span to exclude src's white space (including U+3000) at both ends.

Directories

Path Synopsis
Package autocast detects who speaks each line of a story, for the Stories editor's Auto-cast: tagged lines ("[Mara] Hello"), screenplay lines ("MARA: Hello", "小明:你好") and quoted prose attributed with a speech verb (`"Hello," said Mara.`, `小明说:“你好。
Package autocast detects who speaks each line of a story, for the Stories editor's Auto-cast: tagged lines ("[Mara] Hello"), screenplay lines ("MARA: Hello", "小明:你好") and quoted prose attributed with a speech verb (`"Hello," said Mara.`, `小明说:“你好。
Package lexicon is the pronunciation dictionary: user entries that say how a term is pronounced, applied to script text before synthesis.
Package lexicon is the pronunciation dictionary: user entries that say how a term is pronounced, applied to script text before synthesis.
Package markup parses AudioSmith's SSML-lite script markup into a typed token stream with diagnostics, for the Studio, Stories and Audiobook editors and their validation UIs.
Package markup parses AudioSmith's SSML-lite script markup into a typed token stream with diagnostics, for the Studio, Stories and Audiobook editors and their validation UIs.
Package normalize rewrites written forms a TTS model may misread into spoken words, conservatively and per language: numbers (with grouping and decimals), negative numbers, percentages, ordinals, times ("3:30"), simple dates, currency ("$5", "¥5", "5 €"), number ranges ("3–5", "3~5"), temperatures and common abbreviations ("Dr.", "z.
Package normalize rewrites written forms a TTS model may misread into spoken words, conservatively and per language: numbers (with grouping and decimals), negative numbers, percentages, ordinals, times ("3:30"), simple dates, currency ("$5", "¥5", "5 €"), number ranges ("3–5", "3~5"), temperatures and common abbreviations ("Dr.", "z.
Package plan turns a script into what a TTS engine is asked to say: the markup parsed (internal/text/markup) and lowered for one engine, the pronunciation dictionary and inline overrides applied, written forms normalised, and the result cut into chunks the engine can take, with the silences and joins between them.
Package plan turns a script into what a TTS engine is asked to say: the markup parsed (internal/text/markup) and lowered for one engine, the pronunciation dictionary and inline overrides applied, written forms normalised, and the result cut into chunks the engine can take, with the silences and joins between them.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL