similarity

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2021 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package similarity implements data types and methods used to calculate similarities between hashes.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNotSameLength = errors.New("hashes aren't the same length")

ErrNotSameLength is reported when the length of hashes doesn't match.

Functions

This section is empty.

Types

type Distance

type Distance float64

Distance represents a similatiry measure as float64 value.

func Hamming

func Hamming(h1, h2 hashtype.Binary) Distance

Hamming calculates distance between two binary hashes.

Example
hash1 := hashtype.Binary{15, 131, 192, 224, 192, 252, 255, 255}
hash2 := hashtype.Binary{24, 60, 126, 126, 126, 126, 60, 0}
hash3 := hashtype.Binary{63, 131, 192, 224, 192, 252, 255, 63}

fmt.Println(Hamming(hash1, hash2))
fmt.Println(Hamming(hash1, hash3))
Output:

42
4

func L2Float64

func L2Float64(h1, h2 hashtype.Float64) Distance

L2Float64 calculates the L2 distance between two float64 hashes.

Example
hash1 := hashtype.Float64{-6.582886393254827e-25, 8.709067220205253e-17, 8.575690996612257e-25}
hash2 := hashtype.Float64{7.006891104009164e-24, 2.9211456863128017e-16, 2.376195809939422e-22, 0.0011816509074528167, 2.9408079432297224e-09}
hash3 := hashtype.Float64{-1.983273625570263e-26, 6.064435932101452e-19, -6.695730840743158e-27, 0.0013973374569736076, 1.915611857766067e-09, 3.205292709962961e-13}

fmt.Println(L2Float64(hash1, hash2))
fmt.Println(L2Float64(hash1, hash3))
Output:

2.050238964293645e-16
8.648422860884238e-17

func L2UInt8

func L2UInt8(h1, h2 hashtype.UInt8) Distance

L2UInt8 calculates the L2 distance between two uint8 hashes.

Example
hash1 := hashtype.UInt8{60, 67, 86, 64, 58, 72, 68, 75}
hash2 := hashtype.UInt8{143, 213, 154, 170, 209, 125, 152, 173, 167, 181}
hash3 := hashtype.UInt8{0, 255, 247, 54, 127}

fmt.Println(L2UInt8(hash1, hash2))
fmt.Println(L2UInt8(hash1, hash3))
Output:

293.82818108547724
264.05681206891825

func PCCFloat64

func PCCFloat64(h1, h2 hashtype.Float64) (Distance, error)

PCCFloat64 calculates PCC distance for two float64 hashes.

Example
hash1 := hashtype.Float64{5.188434119386236e-09, 4.112809393169052e-11, 2.4301287246914963e-12}
hash2 := hashtype.Float64{-4.865894404868708e-23, 0.0012375374902843692, 1.6038385709207788e-10}
hash3 := hashtype.Float64{6.743101958369291e-09, 4.991671383226105e-12, 4.0181156557344116e-12}

res1, _ := PCCFloat64(hash1, hash2)
res2, _ := PCCFloat64(hash1, hash3)
fmt.Println(res1)
fmt.Println(res2)
Output:

0.9999789450188745
0.9986890376061919

func PCCUInt8

func PCCUInt8(h1, h2 hashtype.UInt8) (Distance, error)

PCCUInt8 calculates PCC distance for two uint8 hashes.

Example
hash1 := hashtype.UInt8{93, 134, 157, 144, 131, 89, 122, 148}
hash2 := hashtype.UInt8{140, 153, 121, 106, 129, 93, 134, 157}
hash3 := hashtype.UInt8{179, 87, 159, 70, 0, 106, 39, 66}

res1, _ := PCCUInt8(hash1, hash2)
res2, _ := PCCUInt8(hash1, hash3)
fmt.Println(res1)
fmt.Println(res2)
Output:

0.7313844312739906
0.5830834602203031

func (Distance) Equal

func (d Distance) Equal(dst Distance) bool

Equal checks if two distances are the same, it uses the epsilon approach for comparring floats.

Example
num1 := Distance(17.2299478)
num2 := Distance(184.909055172)
num3 := Distance(184.909055172)

fmt.Println(num1.Equal(num2))
fmt.Println(num2.Equal(num3))
Output:

false
true

Jump to

Keyboard shortcuts

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