PHP Classes

File: class.bkTree.php

Recommend this page to a friend!
  Classes of Brian   bkTree   class.bkTree.php   Download  
File: class.bkTree.php
Role: Class source
Content type: text/plain
Description: main class file
Class: bkTree
Insert and search text in a Burkhard-Keller tree
Author: By
Last change: fix typo
Date: 12 years ago
Size: 852 bytes
 

Contents

Class file image Download
class bkTree
{
    public $term;
    public $members;
   
    public function __construct($term)
    {
    $this->term = $term;
    }
   
    public function build(array $terms)
    {
    foreach($terms as $term)
    {
        $this->addTerm($term);
    }
    }
   
    public function addTerm($t)
    {
        $d = levenshtein($this->term, $t);
    if ($this->members[$d]) $this->members[$d]->addTerm($t);
    else $this->members[$d] = new bkTree($t);
    }
   
    public function query($t, $l, $d=false, $r=false)
    {
    if (!$r) $r = new stdClass();
    $cd = levenshtein($this->term, $t);
    if ( $cd <= $l and $cd > 0 ) $r->matches[] = $this->term;
    if (!$d) $d = levenshtein($t, $this->term);
    for($i=$d-$l; $i<=$d+$l; $i++)
    {
        if (isset($this->members[$i])) $this->members[$i]->query($t, $l, $d, $r);
    }
    return $r;
    }
}