Help us shape the future of Go! Take the 2026 Go Developer Survey

similarity

package
v0.25.4 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LibraryName = "scriptling.similarity"
	LibraryDesc = "String matching and similarity utilities including fuzzy search and MinHash"
)

Variables

View Source
var (
	ErrDimensionMismatch = errors.New("vectors must have the same length")
	ErrEmptyVector       = errors.New("vectors must not be empty")
)

Functions

func CosineSimilarity added in v0.17.0

func CosineSimilarity(a, b []float64) (float64, error)

CosineSimilarity computes the cosine of the angle between two vectors. Returns 0.0 if either vector has zero magnitude.

func Register

func Register(registrar interface{ RegisterLibrary(*object.Library) })

Register registers the similarity library with the given registrar.

func ToFloat64Slice added in v0.17.0

func ToFloat64Slice(obj object.Object) ([]float64, error)

ToFloat64Slice converts a scriptling Object (FloatArray or List of numbers) to a []float64. Shared by ai and similarity libraries.

func VectorFromText added in v0.17.0

func VectorFromText(text string, dims int) []float64

VectorFromText produces a fixed-dimensional vector from text using the feature-hashing trick (the "hashing trick"). Each token is mapped to a dimension via FNV-1a and contributes +1 or −1 based on a sign hash. The resulting vector is L2-normalised so it can be compared directly with CosineSimilarity. This is a fast, deterministic, CPU-only method that captures lexical overlap — similar texts produce similar vectors.

Types

type ScoredVector added in v0.17.0

type ScoredVector struct {
	Index int
	Score float64
}

ScoredVector is a ranked result from MostSimilar.

func MostSimilar added in v0.17.0

func MostSimilar(query []float64, vectors [][]float64, topK int) []ScoredVector

MostSimilar ranks vectors by cosine similarity to query, returning the top-k results sorted by descending score. If topK <= 0 all results are returned.

Jump to

Keyboard shortcuts

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