Class: GraphedFuzzySearch::Node
- Inherits:
-
Struct
- Object
- Struct
- GraphedFuzzySearch::Node
- Defined in:
- lib/graphed_fuzzy_search.rb
Instance Attribute Summary collapse
-
#adjacents ⇒ Object
Returns the value of attribute adjacents.
-
#item ⇒ Object
Returns the value of attribute item.
-
#str ⇒ Object
Returns the value of attribute str.
Instance Method Summary collapse
- #[](k) ⇒ Object
- #connect(weight, node) ⇒ Object
- #head ⇒ Object
- #inspect ⇒ Object
- #length ⇒ Object
- #mine(weight, str) ⇒ Object
- #walk(enum) ⇒ Object
Instance Attribute Details
#adjacents ⇒ Object
Returns the value of attribute adjacents
87 88 89 |
# File 'lib/graphed_fuzzy_search.rb', line 87 def adjacents @adjacents end |
#item ⇒ Object
Returns the value of attribute item
87 88 89 |
# File 'lib/graphed_fuzzy_search.rb', line 87 def item @item end |
#str ⇒ Object
Returns the value of attribute str
87 88 89 |
# File 'lib/graphed_fuzzy_search.rb', line 87 def str @str end |
Instance Method Details
#[](k) ⇒ Object
99 100 101 102 103 |
# File 'lib/graphed_fuzzy_search.rb', line 99 def [](k) self.adjacents ||= [] _, adjacent = adjacents.find { |c, _| c == k } adjacent end |
#connect(weight, node) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/graphed_fuzzy_search.rb', line 123 def connect(weight, node) return nil if node.__id__ == self.__id__ self.adjacents ||= [] connection = self[node.str] return connection.node if connection adjacents << [node.head, Connection.new(weight, node)] node end |
#head ⇒ Object
91 92 93 |
# File 'lib/graphed_fuzzy_search.rb', line 91 def head str[0] end |
#inspect ⇒ Object
88 89 90 |
# File 'lib/graphed_fuzzy_search.rb', line 88 def inspect "#<#{self.class.name} @item=#{item.inspect} @str=#{str.inspect} adjacents=#{adjacents.map(&:first).join}>" end |
#length ⇒ Object
95 96 97 |
# File 'lib/graphed_fuzzy_search.rb', line 95 def length str.size end |
#mine(weight, str) ⇒ Object
117 118 119 120 121 |
# File 'lib/graphed_fuzzy_search.rb', line 117 def mine(weight, str) str.each_char.inject(self) do |r, i| r.connect(weight, Node.new(i, [], self.item)) end end |
#walk(enum) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/graphed_fuzzy_search.rb', line 105 def walk(enum) weight = 0 enum.inject(self) do |r, i| c = r[i] raise unless c weight += c.weight yield c.node c.node end weight end |