Skip to content

evanmoss/SwiftSim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 

Repository files navigation

SwiftSim

Vector similarity utility in Swift. Currently, only arrays of Double are supported as input. In the next update, this will be changed to be more generic. The hope is that the numerical methods can operate on any arbitrary numerical input, and the set-based metrics can operate on any Hashable type. Future versions may have a more functional object Class called SwiftSwim. Future versions will also support arbitrary objects to be used and their data paths configurable (i.e., use objects). Here, the input vectors will be lazily constructed via closures. Another feature will be to pass in arrays of objects or arrays to find the top or bottom k entries according to some specified element.

Basic Usage

let Similarity = EMSimilarity()
let A = [0.0, 1.5, 3.0, 4.5, 6.0]
let B = [2.0, 4.0, 6.0, 8.0, 10.0]

// compute the cosine similarity of A and B
Similarity.compute(A, B: B)
// 0.984731927834662

Computation Modes

SwiftSim uses a stack to store its modes. To specify a new mode, you simply push a new computation mode to the stack. You can also pop modes off.

enum EMSimilarityMode {
    case Cosine
    case Tanimoto
    case Ochiai
    case JaccardIndex
    case JaccardDistance
    case Dice
    case Hamming
}
// push a new computation mode to the stack
Similarity.pushSimMode(.Hamming)
// compute the Hamming distance of A and B
Similarity.compute(A, B: B)
// 5.0
// go back to previous mode
Similarity.popSimMode()
Similarity.compute(A, B: B)
// 0.984731927834662

Vector Size Mismatch Mode

By default, if A and B have different lengths, and the mode is Cosine, Tanimoto, or Hamming, it will trigger a bail and return -1.0. However, you can specify a different mismatch mode like you do similarity mode.

enum EMVectorSizeMismatchMode {
    case Bail
    case Truncate
}

// truncate the larger vector
Similarity.pushMismatchMode(.Truncate)

Other Bail Conditions

Currently, another condition that will cause -1.0 to be returned is if both input arrays are empty and the similarity mode is not Hamming, or one of them is empty and the current mode is Cosine, Tanimoto, or Ochiai. This is to mitigate a divide by zero error, or to adhere to metric prequisites.

About

Vector similarity utility in Swift

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages