Giter Club home page Giter Club logo

bktree's Introduction

BK-Tree implementation in Ruby

If you don’t know what a BK-tree is, these links should provide a good explanation and introduction.

Usage

require "bk"
tree = BK::Tree.new # Use the default Levenshtein distance algorithm

Add items to the tree:

tree.add "cat"
tree.add "dog"
tree.add "monkey"
tree.add "donkey"

Find all items within distance 1 of ‘munkey’:

tree.query("munkey", 1)
# => {"monkey"=>1} 

Find all items within distance 2 of ‘munkey’:

tree.query("munkey", 2)
# => {"donkey"=>2, "monkey"=>1}

You can specify a custom distance algorithm by passing an object that responds to call(a, b) with a number:

custom_algorithm = lambda{ |a, b|
  Text::Levenshtein.distance(a, b)
}

tree = BK::Tree.new(custom_algorithm)

Note that the result must satisfy the triangle inequality, i.e. d(x,z) ≤ d(x,y) + d(y,z).

The precomputed tree can be exported to and reimported later from an IO-like object:

File.open("tree", "wb") do |f|
  tree.export(f)
end

File.open("tree", "rb") do |f|
  tree = BK::Tree.import(f)
end

Dependencies

  • text version 0.2.0 or newer.

Performance

Results of looking for words within distance 1 of ‘alien’ in a 20,000-word dictionary:

Loading 20000 words from dictionary ... 0.273s
Building tree ... 57.331s
Linear scan to find expected terms ... 5.711s
Query tree ... 0.133s
2.1% of tree was queried

This means that the BK-tree is about 40 times as fast as a linear search, although building the initial tree took 10 times as long as a linear search.

As the threshold increases, the benefit is reduced. At threshold 3:

Query tree ... 3.368s
62.9% of tree was queried

Limitations

  • Memory usage: around 6 MB for a 20,000-word tree.
  • Maximum tree depth is limited by the stack.

bktree's People

Contributors

kimtaro avatar threedaymonk avatar

Stargazers

 avatar

Watchers

 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.