Class: BK::Node
- Inherits:
-
Object
- Object
- BK::Node
- Includes:
- DotGraphable, Dumpable
- Defined in:
- lib/bk.rb,
lib/bk/dump.rb,
lib/bk/dot_graph.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#term ⇒ Object
readonly
Returns the value of attribute term.
Instance Method Summary collapse
- #add(term) ⇒ Object
- #distance(term) ⇒ Object
-
#initialize(term, distancer) ⇒ Node
constructor
A new instance of Node.
- #query(term, threshold, collected) ⇒ Object
Methods included from DotGraphable
Methods included from Dumpable
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
#children ⇒ Object (readonly)
Returns the value of attribute children.
16 17 18 |
# File 'lib/bk.rb', line 16 def children @children end |
#term ⇒ Object (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 |