worddiff

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package worddiff compares compose documents word by word, formatting included: the unit is a word (a CJK character, a run of whitespace, a punctuation mark) with its style and link.

Index

Constants

View Source
const DefaultBudget = 2_000_000

DefaultBudget is the comparisons a Diff may make before it gives up.

Variables

This section is empty.

Functions

func Ratio

func Ratio(blocks []BlockDiff) float64

Ratio is how much changed: (words deleted + words inserted) / (old words + new words), from 0 to 1; whitespace is no word. 0 when both are empty.

func Tokenize

func Tokenize(d richtext.Doc) [][]Token

Tokenize is d's words, a slice per block: Latin text splits at whitespace and punctuation (whitespace runs are tokens too), CJK text at each character.

func Words

func Words(ts []Token) int

Words counts the tokens that are not whitespace.

Types

type BlockDiff

type BlockDiff struct {
	Old, New int
	Ops      []Op
}

BlockDiff is a block's changes: the old block Old and the new block New (indexes into Diff's arguments), -1 for a block added or removed.

func Diff

func Diff(old, new [][]Token, budget int) (blocks []BlockDiff, ok bool)

Diff aligns the blocks of old and new, then compares the words of each pair; blocks with no partner are added or removed whole. Tokens are equal when their text and their style, link included, are. ok is false once it would make more than budget comparisons.

type Op

type Op struct {
	Kind   OpKind
	Tokens []Token
}

Op is a run of tokens kept, deleted or inserted.

type OpKind

type OpKind int

OpKind is what an Op does.

const (
	Same OpKind = iota
	Del
	Ins
)

type Token

type Token struct {
	Text  string
	Style richtext.Inline
}

Token is a word in its style. Style's Text is empty: it holds the style and the link only.

Jump to

Keyboard shortcuts

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