Class: BK::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/bk.rb,
lib/bk/dump.rb,
lib/bk/dot_graph.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(distancer = LevenshteinDistancer.new) ⇒ Tree

Returns a new instance of Tree.



48
49
50
51
# File 'lib/bk.rb', line 48

def initialize(distancer = LevenshteinDistancer.new)
  @root = nil
  @distancer = distancer
end

Class Method Details

.import(stream) ⇒ Object



71
72
73
# File 'lib/bk.rb', line 71

def self.import(stream)
  YAML.load(stream.read)
end

Instance Method Details

#add(term) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/bk.rb', line 53

def add(term)
  if @root
    @root.add(term)
  else
    @root = Node.new(term, @distancer)
  end
end

#dot_graphObject



19
20
21
# File 'lib/bk/dot_graph.rb', line 19

def dot_graph
  ["digraph G {", @root.graph, "}"].join("\n")
end

#dumpObject



19
20
21
# File 'lib/bk/dump.rb', line 19

def dump
  @root ? @root.dump : []
end

#export(stream) ⇒ Object



67
68
69
# File 'lib/bk.rb', line 67

def export(stream)
  stream.write(YAML.dump(self))
end

#query(term, threshold) ⇒ Object



61
62
63
64
65
# File 'lib/bk.rb', line 61

def query(term, threshold)
  collected = {}
  @root.query(term, threshold, collected)
  return collected
end