Module: Eco::Data::FuzzyMatch::ArrayHelpers
- Included in:
- ClassMethods
- Defined in:
- lib/eco/data/fuzzy_match/array_helpers.rb
Instance Method Summary collapse
-
#combinations(values, range = 2..3) ⇒ Array<Array<Value>>
Keeps the start order of the
values
of the inputArray
values
. -
#facet(values1, values2) ⇒ Hash
Helper to praper facet structure.
-
#ngrams(values, range = 2..3) ⇒ Array<Array<Value>>
Keeps the start order of the
values
and consecutivevalues
together/consecutive. -
#permutations(values, range = 2..3) ⇒ Array<Array<Value>>
It includes
combinations
that break the initial order of theArray
.
Instance Method Details
#combinations(values, range = 2..3) ⇒ Array<Array<Value>>
Keeps the start order of the values
of the input Array
values
.
It does not keep consecutive values
together (it can jump/skip items).
33 34 35 36 37 38 39 |
# File 'lib/eco/data/fuzzy_match/array_helpers.rb', line 33 def combinations(values, range=2..3) if range.is_a?(Integer) values.combination(range).to_a else range.flat_map {|size| values.combination(size).to_a} end end |
#facet(values1, values2) ⇒ Hash
Helper to praper facet structure
64 65 66 67 68 69 70 |
# File 'lib/eco/data/fuzzy_match/array_helpers.rb', line 64 def facet(values1, values2) {}.tap do |out| next unless values1.is_a?(Enumerable) values1 = values1.is_a?(Hash) ? values1.values : values1.to_a values1.each {|val| out[val] = values2.dup} end end |
#ngrams(values, range = 2..3) ⇒ Array<Array<Value>>
Keeps the start order of the values
and consecutive values
together/consecutive.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/eco/data/fuzzy_match/array_helpers.rb', line 9 def ngrams(values, range=2..3) [].tap do |out| if range.is_a?(Integer) n = range values_count = values.length values.each_with_index do |word, i| min = i max = i + (n - 1) break if values_count <= max out << values[min..max].join(' ') end out.uniq! else range.each {|n| out.concat(ngrams(values, n))} out.uniq! end end end |
#permutations(values, range = 2..3) ⇒ Array<Array<Value>>
It includes combinations
that break the initial order of the Array
.
It does not keep consecutive values
together (it can jump/skip items).
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/eco/data/fuzzy_match/array_helpers.rb', line 46 def permutations(values, range=2..3) combinations(values, range).tap do |out| range = range.is_a?(Integer)? (range..range) : range out.dup.select do |item| range.include?(item.length) end.each do |comb| comb.permutation.to_a.tap do |perms| perms.each {|perm| out << perm} end end out.uniq! end end |