finder

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2023 License: MIT Imports: 9 Imported by: 2

Documentation

Index

Constants

View Source
const (
	WorstScoreValue = -1 * math.MaxFloat32
	BestScoreValue  = math.MaxFloat32
)

These constants hold the value of the lowest and highest possible scores. Compatible with JSON serialization. It's not ideal to mix presentation with business logic but in this instance it was convenient and similarly effective as math.Inf(-1)

Variables

View Source
var (
	ErrNoAlgorithmDefined    = errors.New("no algorithm defined")
	ErrPrefixExceedsInputLen = errors.New("prefix length exceeds input length")
)

Functions

This section is empty.

Types

type Algorithm

type Algorithm func(a, b string) float64

Algorithm the type to comply with to create your own algorithm Note that the return value must be greater than WorstScoreValue and less than BestScoreValue

func NewDamerauLevenshtein added in v0.1.3

func NewDamerauLevenshtein() Algorithm

NewDamerauLevenshtein returns the DamerauLevenshtein algorithm

func NewJaro added in v0.1.3

func NewJaro() Algorithm

NewJaro returns the default Jaro algorithm @see Original https://rosettacode.org/wiki/Jaro_distance#Go Relevant discussions: https://github.com/xrash/smetrics/issues/7#issuecomment-664794681 Changes over original: - Reduced allocations - Added rounding on the unaligned matches as per: http://www.alias-i.com/lingpipe/docs/api/com/aliasi/spell/JaroWinklerDistance.html - Added support for 1 character inputs, by making sure the match distances is never negative

func NewJaroWinkler added in v0.1.3

func NewJaroWinkler(boostThreshold float64, prefixLength int) Algorithm

NewJaroWinkler returns the JaroWinkler algorithm

func NewJaroWinklerDefaults added in v0.1.3

func NewJaroWinklerDefaults() Algorithm

NewJaroWinklerDefaults returns the Jaro Winkler algorithm with 0.7 boost threshold and a prefix length of 4

func NewWagnerFischer added in v0.1.3

func NewWagnerFischer(insert, delete, substitution int) Algorithm

NewWagnerFischer returns the NewWagnerFischer algorithm, sensible defaults are: i:1, d:3, s:1

type Finder

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

Finder is the type to find the nearest reference

func New

func New(list []string, options ...Option) (*Finder, error)

New creates a new instance of Finder. The order of the list is significant

func (*Finder) Exact added in v0.1.3

func (t *Finder) Exact(input string) bool

Exact returns true if the input is an exact match.

func (*Finder) Find

func (t *Finder) Find(input string) (string, float64, bool)

Find returns the best alternative, score and if it was an exact match or not. Since algorithms can define their own upper-bound, there is no "best" value.

func (*Finder) FindCtx

func (t *Finder) FindCtx(ctx context.Context, input string) (string, float64, bool)

FindCtx is the same as Find, with context support.

func (*Finder) FindTopRankingCtx added in v0.0.3

func (t *Finder) FindTopRankingCtx(ctx context.Context, input string) ([]string, float64, bool)

FindTopRankingCtx returns a list (of at least one element) of references with the same "best" score

func (*Finder) FindTopRankingPrefixCtx added in v0.1.3

func (t *Finder) FindTopRankingPrefixCtx(ctx context.Context, input string, prefixLength uint) (list []string, exact bool, err error)

FindTopRankingPrefixCtx requires the references to have an exact prefix match on N characters of the input. prefixLength cannot exceed length of input

func (*Finder) GetMatchingPrefix added in v0.1.3

func (t *Finder) GetMatchingPrefix(ctx context.Context, prefix string, max uint) ([]string, error)

GetMatchingPrefix returns up to max ref's, that start with the prefix argument

func (*Finder) Refresh added in v0.1.3

func (t *Finder) Refresh(list []string)

Refresh replaces the internal reference list.

type Option

type Option func(sug *Finder)

Option is the type accepted by finder to set specific options

func WithAlgorithm

func WithAlgorithm(alg Algorithm) Option

WithAlgorithm allows you to set any algorithm

func WithLengthTolerance added in v0.0.2

func WithLengthTolerance(t float64) Option

WithLengthTolerance defines a percentage of length above we no longer consider a length difference a typo, but instead we consider it as "completely wrong". A value of 0.2 specifies a tolerance of at most ~20% difference in size, with a minimum of 1 character. A value of 0 (the default) disables this feature.

func WithPreProcessor added in v0.1.5

func WithPreProcessor(p ...Processor) Option

func WithPrefixBuckets added in v0.1.3

func WithPrefixBuckets(enable bool) Option

WithPrefixBuckets splits the reference list into buckets by their first letter. At a trade-off that the first character must be correct, this will significantly improve performance as it has a much smaller list to consider

type Processor added in v0.1.5

type Processor func(string) string

func MarkingTypoProcessor added in v0.1.5

func MarkingTypoProcessor() Processor

Jump to

Keyboard shortcuts

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