tokenize

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package tokenize converts license text into normalized integer tokens.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ID

type ID uint32

ID identifies a normalized word in a Vocabulary.

const Unknown ID = 0

Unknown is the ID assigned to words absent from a Vocabulary.

type Offset

type Offset struct {
	Start int
	End   int
}

Offset is a half-open byte range in the original input.

type Tokens

type Tokens struct {
	IDs     []ID
	Offsets []Offset
}

Tokens contains token IDs and their corresponding input byte ranges.

type Vocabulary

type Vocabulary struct {
	// contains filtered or unexported fields
}

Vocabulary is an immutable mapping from normalized words to integer IDs.

func NewVocabulary

func NewVocabulary(texts [][]byte) (*Vocabulary, error)

NewVocabulary builds a deterministic vocabulary from texts. IDs are assigned in normalized lexical order and start at one, leaving zero for Unknown.

func NewVocabularyFromWords

func NewVocabularyFromWords(words []string) (*Vocabulary, error)

NewVocabularyFromWords loads an already normalized and sorted vocabulary.

func (*Vocabulary) Len

func (v *Vocabulary) Len() int

Len returns the number of known words, excluding Unknown.

func (*Vocabulary) Lookup

func (v *Vocabulary) Lookup(word string) (ID, bool)

Lookup returns the ID for a normalized word.

func (*Vocabulary) Tokenize

func (v *Vocabulary) Tokenize(input []byte) Tokens

Tokenize normalizes input and maps every word to an ID and byte range.

func (*Vocabulary) Word

func (v *Vocabulary) Word(id ID) string

Word returns the normalized word for id, or an empty string for an unknown ID.

func (*Vocabulary) Words

func (v *Vocabulary) Words() []string

Words returns the normalized words in ID order, excluding Unknown.

type Word

type Word struct {
	Text  string
	Start int
	End   int
}

Word is a normalized word and its byte range in the original input.

func Words

func Words(input []byte) []Word

Words returns normalized words and their byte ranges without mapping IDs.

Jump to

Keyboard shortcuts

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