language

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 8 Imported by: ? 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 language is a native module: natural language processing with Apple's NaturalLanguage framework, behind Go interfaces. Its implementation is the Swift package in swift/.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUndetermined is returned when the language of a text is unknown.
	ErrUndetermined = native.NewError("undetermined")
	// ErrNoEmbedding is returned for a language without a word embedding.
	ErrNoEmbedding = native.NewError("no_embedding")
)

Functions

This section is empty.

Types

type Analyzer

type Analyzer interface {
	// DetectLanguage returns the BCP 47 code of the text's language, such
	// as "en" or "zh-Hans".
	DetectLanguage(text string) (string, error)
	// Hypotheses returns the languages the text may be in, with their
	// probabilities, at most max of them.
	Hypotheses(text string, max int) (map[string]float64, error)
	// Tokens yields the words, sentences or paragraphs of the text.
	Tokens(ctx context.Context, text string, unit Unit) iter.Seq2[Token, error]
	// Embedding loads the word embedding of a language.
	Embedding(ctx context.Context, language string) (Embedding, error)
	// Suggest yields the neighbors of word in the embedding, best first by
	// the ranker's scores.
	Suggest(ctx context.Context, e Embedding, word string, r Ranker) iter.Seq2[string, error]
	// Count calls progress with each word of the text it counts, and
	// returns how many there are.
	Count(text string, progress func(word string, index int)) int
	// Ticks calls f every interval until the subscription is closed.
	Ticks(interval time.Duration, f func(n int)) io.Closer
	// Uppercase writes the upper case of text into buf, and returns how
	// many bytes it wrote.
	Uppercase(text string, buf *native.Buffer) (int, error)
	// Chunks yields the UTF-8 bytes of text, size at a time, the last
	// chunk padded with zeros, in buffers lent until the loop continues.
	Chunks(ctx context.Context, text string, size int) iter.Seq2[*native.Buffer, error]
	// Sleep waits for d, or until ctx is canceled.
	Sleep(ctx context.Context, d time.Duration) error
}

Analyzer reads natural language text.

func NewAnalyzer

func NewAnalyzer() (Analyzer, error)

NewAnalyzer makes the implementation of Analyzer on this platform. It returns errors.ErrUnsupported where the module has none, or the system lacks what it needs.

type Embedding

type Embedding interface {
	io.Closer
	// Distance is the distance between two words, 0 for the same.
	Distance(a, b string) float64
	// Neighbors returns the n words nearest word.
	Neighbors(word string, n int) []string
}

Embedding places words of a language by meaning.

type Ranker

type Ranker interface {
	// Score is higher for better suggestions.
	Score(word string) float64
	// Explain describes a suggestion, which may take a request.
	Explain(ctx context.Context, word string) (string, error)
}

Ranker decides the order of suggestions.

type Token

type Token struct {
	Start, End int
	Text       string
}

Token is a range of the text, in UTF-8 bytes.

type Unit

type Unit string

Unit is what Tokens yields.

const (
	Word      Unit = "word"
	Sentence  Unit = "sentence"
	Paragraph Unit = "paragraph"
)

Jump to

Keyboard shortcuts

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