Class: GraphedFuzzySearch::Node

Inherits:
Struct
  • Object
show all
Defined in:
lib/graphed_fuzzy_search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adjacentsObject

Returns the value of attribute adjacents

Returns:

  • (Object)

    the current value of adjacents



87
88
89
# File 'lib/graphed_fuzzy_search.rb', line 87

def adjacents
  @adjacents
end

#itemObject

Returns the value of attribute item

Returns:

  • (Object)

    the current value of item



87
88
89
# File 'lib/graphed_fuzzy_search.rb', line 87

def item
  @item
end

#strObject

Returns the value of attribute str

Returns:

  • (Object)

    the current value of 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

#headObject



91
92
93
# File 'lib/graphed_fuzzy_search.rb', line 91

def head
  str[0]
end

#inspectObject



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

#lengthObject



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