Class: BK::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DotGraphable

#graph

Methods included from Dumpable

#dump

Constructor Details

#initialize(term, distancer) ⇒ Node

Returns a new instance of Node.



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

def initialize(term, distancer)
  @term = term
  @children = {}
  @distancer = distancer
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



16
17
18
# File 'lib/bk.rb', line 16

def children
  @children
end

#termObject (readonly)

Returns the value of attribute term.



16
17
18
# File 'lib/bk.rb', line 16

def term
  @term
end

Instance Method Details

#add(term) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/bk.rb', line 24

def add(term)
  score = distance(term)
  if child = children[score]
    child.add(term)
  else
    children[score] = Node.new(term, @distancer)
  end
end

#distance(term) ⇒ Object



42
43
44
# File 'lib/bk.rb', line 42

def distance(term)
  @distancer.call(term, self.term)
end

#query(term, threshold, collected) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/bk.rb', line 33

def query(term, threshold, collected)
  distance_at_node = distance(term)
  collected[self.term] = distance_at_node if distance_at_node <= threshold
  ((distance_at_node-threshold)..(threshold+distance_at_node)).each do |score|
    child = children[score]
    child.query(term, threshold, collected) if child
  end
end