similar

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package similar finds near-clone code via MinHash over token shingles — the M4 SIMILAR_TO pass. No embeddings or model: a fixed-size MinHash signature per symbol estimates pairwise Jaccard similarity cheaply, and LSH banding (later) turns the O(n^2) all-pairs comparison into candidate buckets.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Edges

func Edges(project string, docs []Doc, threshold float64) []graph.Edge

Edges returns SIMILAR_TO edges between docs whose estimated Jaccard similarity is at least threshold. Candidate pairs come from LSH banding (not an all-pairs scan); each surviving pair yields one symmetric edge (smaller QN -> larger) carrying the score. Docs too short to form a shingle are ignored (trivial bodies are not clones).

func EstJaccard

func EstJaccard(a, b []uint64) float64

EstJaccard estimates the Jaccard similarity of two token streams from their MinHash signatures: the fraction of positions that agree. Signatures must be the same size.

func Signature

func Signature(tokens []string, k, numHashes int) []uint64

Signature is the MinHash signature of the token stream: numHashes positions, each the minimum over the shingle-hash set of a distinct strong-mix permutation (splitmix64 of the shingle XOR a per-position seed). The fraction of equal positions between two signatures estimates the Jaccard similarity of their shingle sets (see EstJaccard). A strong avalanche permutation matters: the cheap a*h+b universal hash is only 2-universal and its MinHash estimate is too noisy at a tight threshold.

func Tokenize

func Tokenize(src string) []string

Tokenize splits source into a token stream for similarity: each identifier/number run is one token, and every other non-space rune (operators, brackets, punctuation) is its own token; whitespace is dropped. Cheap and language-agnostic — near-clones share most of this stream regardless of formatting.

Types

type Doc

type Doc struct {
	QN     string
	Tokens []string
}

Doc is a symbol to compare for near-cloning: its qualified name and token stream.

Jump to

Keyboard shortcuts

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