3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/fuzzy_ruby.rb', line 3
def self.find(strings, input)
@weights = Array.new(strings.length, -1)
@strings = strings.dup
ret = Array.new
regexp = /#{build_regexp(input)}/
@strings.each_index do |i|
assign_weights(i, regexp)
end
@weights.each_index do |i|
if @weights[i] == -1
@weights.delete_at(i)
strings.delete_at(i)
end
end
@weights.map.with_index.sort_by(&:first).map(&:last).each do |index|
ret.push strings[index]
end
return ret
end
|