Git Product home page Git Product logo

pybktree's Introduction

pyBKTree

By Stephen Holiday

This a python implementation of a BKTree (Burkhard-Keller). Essentially it allows searching for an object in a metric space.

One common use is for fuzzy string matching, like in a spell checker. The search is performed by looking at the distance of the test with the current node and moving according to the distance of the test to the children.

This technique is faster than brute force as it does not need to look at every possible node in the space.

Implemented according to this post

This is meant as a way for me to ensure I understand the algorithm. I could have written it more 'pythonic' but this is meant to be a prototype to a C++ version.

This is NOT optimized. It's designed to be simple. But I would be happy to accept suggestions/pull requests.

The code is under the Apache 2.0 license.

Requirements

Example usage

>>> from BKTree import BKTree, StringObject
>>> tree=BKTree()
>>> tree.insert(StringObject('hat'))
>>> print tree
BKTree<root:BKNode<obj:IMetricDistance<value:hat>, children:[]>>
>>> tree.insert(StringObject('hats'))
>>> print tree
BKTree<root:BKNode<obj:IMetricDistance<value:hat>, children:[1,BKNode<obj:IMetricDistance<value:hats>, children:[]>]>>
>>> tree.insert(StringObject('hates'))
>>> tree.insert(StringObject('hater'))
>>> print tree
BKTree<root:BKNode<obj:IMetricDistance<value:hat>, children:[1,BKNode<obj:IMetricDistance<value:hats>, children:[]>][2,BKNode<obj:IMetricDistance<value:hates>, children:[1,BKNode<obj:IMetricDistance<value:hater>, children:[]>]>]>>
>>> tree.insert(StringObject('mat'))
>>> tree.insert(StringObject('matte'))
>>> tree.find(StringObject('mats'),1)
<generator object find at 0x100603550>
>>> for obj in tree.find(StringObject('mats'),1):
...     print obj
... 
IMetricDistance<value:hats>
IMetricDistance<value:mat>
>>> for obj in tree.find(StringObject('hate'),1):
...     print obj
... 
IMetricDistance<value:hat>
IMetricDistance<value:hats>
IMetricDistance<value:hates>
IMetricDistance<value:hater>
>>> for obj in tree.find(StringObject('haters'),1):
...     print obj
... 
IMetricDistance<value:hates>
IMetricDistance<value:hater>
>>> for obj in tree.find(StringObject('haters'),0):
...     print obj
... 
>>>

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.