similarity

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package similarity provides a SequenceMatcher implementation equivalent to Python's difflib.SequenceMatcher for computing string similarity ratios.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetRatio

func SetRatio(a, b []string) float64

SetRatio computes the similarity ratio between two string sets using intersection/union (Jaccard similarity).

func StringRatio

func StringRatio(a, b string) float64

StringRatio is a convenience function that returns the similarity ratio between two strings.

Types

type Match

type Match struct {
	A    int // position in sequence a
	B    int // position in sequence b
	Size int // length of match
}

Match represents a matching block between two sequences.

type OpCode

type OpCode struct {
	Tag byte // 'e' equal, 'r' replace, 'i' insert, 'd' delete
	I1  int
	I2  int
	J1  int
	J2  int
}

OpCode represents a single edit operation.

type SequenceMatcher

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

SequenceMatcher compares two sequences and finds the longest common subsequences. It is a Go port of Python's difflib.SequenceMatcher.

func NewSequenceMatcher

func NewSequenceMatcher(a, b string) *SequenceMatcher

NewSequenceMatcher creates a new SequenceMatcher for the given sequences.

func (*SequenceMatcher) GetMatchingBlocks

func (sm *SequenceMatcher) GetMatchingBlocks() []Match

GetMatchingBlocks returns a list of matching blocks. The last block is a sentinel (len(a), len(b), 0).

func (*SequenceMatcher) GetOpcodes

func (sm *SequenceMatcher) GetOpcodes() []OpCode

GetOpcodes returns a list of 5-tuples describing how to turn a into b.

func (*SequenceMatcher) QuickRatio

func (sm *SequenceMatcher) QuickRatio() float64

QuickRatio returns an upper bound on Ratio() relatively quickly.

func (*SequenceMatcher) Ratio

func (sm *SequenceMatcher) Ratio() float64

Ratio returns a float in [0, 1] measuring similarity. 2.0 * M / T where M = matching characters, T = total characters in both.

func (*SequenceMatcher) SetA

func (sm *SequenceMatcher) SetA(a string)

SetA replaces sequence a.

func (*SequenceMatcher) SetB

func (sm *SequenceMatcher) SetB(b string)

SetB replaces sequence b and reindexes.

func (*SequenceMatcher) SetSeqs

func (sm *SequenceMatcher) SetSeqs(a, b string)

SetSeqs replaces both sequences and resets caches.

Jump to

Keyboard shortcuts

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