Module: Eco::Data::FuzzyMatch::ArrayHelpers

Included in:
ClassMethods
Defined in:
lib/eco/data/fuzzy_match/array_helpers.rb

Instance Method Summary collapse

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).

Parameters:

  • values (Array)

    the input array with the values.

  • range (Integer, Range) (defaults to: 2..3)

    determine the lenght of the generated values.

Returns:

  • (Array<Array<Value>>)

    combinations of range length of values



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

Parameters:

  • values1 (Array)

    the input array with the values to have their facet against.

  • values2 (Array)

    the input array with the values to facet against.

Returns:

  • (Hash)

    where keys are values1 and value of each key all values2



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.

Parameters:

  • values (Array)

    the input array with the values.

  • range (Integer, Range) (defaults to: 2..3)

    determine the lenght of the generated values.

Returns:

  • (Array<Array<Value>>)

    combinations of range length of values.



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).

Parameters:

  • values (Array)

    the input array with the values.

  • range (Integer, Range) (defaults to: 2..3)

    determine the lenght of the generated values.

Returns:

  • (Array<Array<Value>>)

    permutations of range length of values



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