Code Monkey home page Code Monkey logo

go-set-similarity-search's Introduction

Set Similarity Search in Go

Build Status GoDoc

This is a mirror implementation of the Python SetSimilaritySearch library in Go, with better performance.

Benchmarks

Run AllPairs algorithm on 3.5 GHz Intel Core i7, using similarity function jaccard and similarity threshold 0.5.

Dataset Input Sets Avg. Size go-set-similarity-search Runtime SetSimilaritySearch Runtime
Pokec social network (relationships): from-nodes are set IDs; to-nodes are elements 1432693 27.31 1m25s 10m49s
LiveJournal: from-nodes are set IDs; to-nodes are elements 4308452 16.01 4m11s 28m51s

Library Usage

For All-Pairs, it takes an input of a list of sets, and output pairs that meet the similarity threshold.

import (
    "fmt"
    "go-set-similarity-search"
)


func main() {
    // Each raw set must be a slice of unique string tokens.
    rawSets := [][]string{
        []string{"a"},
        []string{"a", "b"},
        []string{"a", "b", "c"},
        []string{"a", "b", "c", "d"},
        []string{"a", "b", "c", "d", "e"},
    }
    // Use frequency order transformation to replace the string tokens
    // with integers.
    sets, _ := SetSimilaritySearch.FrequencyOrderTransform(rawSets)
    // Run all-pairs algorithm, get a channel of pairs.
    pairs, _ := SetSimilaritySearch.AllPairs(sets,    
        /*similarityFunctionName=*/"jaccard", 
        /*similarityThreshold=*/0.1)
    for pair := range pairs {
        // X and Y are indexes to the original rawSets and sets slices.
        fmt.Println(pair.X, pair.Y, pair.Similarity)
    }
}

For Query, it takes an input of a list of sets, and builds a search index that can compute any number of queries. Currently the search index only supports a static collection of sets with no updates.

import (
    "fmt"
    "go-set-similarity-search"
)

func main() {
    // Each raw set must be a slice of unique string tokens.
    rawSets := [][]string{
        []string{"a"},
        []string{"a", "b"},
        []string{"a", "b", "c"},
        []string{"a", "b", "c", "d"},
        []string{"a", "b", "c", "d", "e"},
    }
    // Use frequency order transformation to replace the string tokens
    // with integers.
    sets, dict := SetSimilaritySearch.FrequencyOrderTransform(rawSets)
    // Build a search index.
    searchIndex, err := SetSimilaritySearch.NewSearchIndex(sets,
        /*similarityFunctionName=*/"jaccard", 
        /*similarityThreshold=*/0.1)
    // Use dictionary to transform a query set.
    querySet := dict.Transform([]string{"a", "c", "d"})
    // Query the search index.
    searchResults := searchIndex.Query(querySet)
    for _, result := range searchResults {
        // X is the index to the original rawSets and sets slices.
        fmt.Println(result.X, result.Similarity)
    }
}

Supported similarity functions (more to come):

  • Jaccard: intersection size divided by union size; set similarityFunctionName="jaccard".
  • Cosine: intersection size divided by square root of the product of sizes; set similarityFunctionName="cosine".
  • Containment: intersection size divided by the size of the first set (or query set); set similarityFunctionName="containment".

go-set-similarity-search's People

Contributors

ekzhu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.