diff

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 3 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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DiffMatchPatch

func DiffMatchPatch(refText string, asrText string) []diffmatchpatch.Diff

func DiffPrettyHtml

func DiffPrettyHtml(diffs []diffmatchpatch.Diff) string

func ErrorCount

func ErrorCount(diffs []CDiff) int

func IsHyphen

func IsHyphen(ch rune) bool

func IsLikeApostrophe

func IsLikeApostrophe(ch rune) bool

func IsMatch

func IsMatch(diffs []diffmatchpatch.Diff) bool

func ReadableDiff

func ReadableDiff(diffs []Diff) string

Types

type CDiff

type CDiff struct {
	Type    Operation
	Char    rune
	Replace rune
}

func CharDiff

func CharDiff(refText string, asrText string) []CDiff

func CharLevenshtein

func CharLevenshtein(refText string, asrText string) []CDiff

CharLevenshtein aligns refText (reference) against asrText (hypothesis) at the character (rune) level using a classic Levenshtein dynamic-programming alignment where substitution, insertion and deletion each cost 1.

Unlike the diff-match-patch path, substitution is a first-class operation, so a changed character comes back as a single OpReplace (Char=ref rune, Replace=asr rune) rather than a delete+insert pair — and because it works on atomic runes it never splits a multibyte rune or exploits incidental shared characters between unrelated words.

The result is a minimal-cost alignment; when several are minimal, ties are broken in a fixed order (match/substitute, then delete, then insert), which left-aligns edits consistently.

func Diff2CDiff

func Diff2CDiff(diffs []Diff) []CDiff

type Counts

type Counts struct {
	Hits          int // equal
	Substitutions int // replace
	Deletions     int // delete (in reference, not in hypothesis)
	Insertions    int // insert (in hypothesis, not in reference)
	RefLen        int // reference tokens = Hits + Substitutions + Deletions
}

Counts holds the S/D/I/hits breakdown of an alignment.

func Count

func Count(diffs []CDiff) Counts

func CountDiff

func CountDiff(diffs []Diff) Counts

func CountWords

func CountWords(diffs []Diff) Counts

CountWords tallies a word-level []Diff into S/D/I/hits. Use the resulting Counts.ErrorRate() to get WER: (S+D+I)/RefLen.

func (*Counts) Add

func (c *Counts) Add(cnt Counts)

func (*Counts) ErrorCount

func (c *Counts) ErrorCount() int

func (*Counts) ErrorRate

func (c *Counts) ErrorRate() float64

ErrorRate returns (S+D+I)/RefLen — CER when the tokens are runes. Returns 0 for an empty reference.

type Diff

type Diff struct {
	Type    Operation
	Text    string
	Replace string
}

func Convert2Diff

func Convert2Diff(diffs []diffmatchpatch.Diff) []Diff

func DiffConvert

func DiffConvert(refText string, asrText string) []Diff

DiffConvert converts diffmatchpathch tree to a diff.Diff tree without modifying it.

func DiffReplace

func DiffReplace(refText string, asrText string) []Diff

func WordLevenshtein

func WordLevenshtein(refText string, asrText string) []Diff

WordLevenshtein aligns refText (reference) against asrText (hypothesis) at the WORD level using Levenshtein DP where substitution, insertion and deletion each cost 1. This is the standard basis for Word Error Rate (WER).

Words are split with strings.Fields, so leading/trailing whitespace is ignored and runs of whitespace collapse; the alignment is over words only, so exact spacing is not reconstructable from the result (use the character path when you need that). Normalize (lowercase, strip punctuation, etc.) BEFORE calling if you want those handled — this compares whole words verbatim.

It emits []Diff, matching the DiffReplace/CharDiff convention: Text holds the reference word for Equal/Replace/Delete and the ASR word for Insert; Replace holds the ASR word on a Replace.

type Operation

type Operation int

type Operation int

const (
	OpEqual Operation = iota
	OpInsert
	OpDelete
	OpReplace
)

func (Operation) String

func (op Operation) String() string

Jump to

Keyboard shortcuts

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