Class: FastFuzzy::Ngram

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_fuzzy/ngram.rb

Class Method Summary collapse

Class Method Details

.generate(str_or_ary, n = 2) ⇒ Array

generate the ngram for either the caracter grams of a string or the items grams of an array example: generate(“abcd”) => [“ab”, “bc”, “cd”], generate([:a, :b, :c, :d]) => [[:a, :b], [:b, :c], [:c, :d]]

Parameters:

  • str_or_ary (String|Array)

    input string or array of anything

Returns:

  • (Array)

    array or ngrams



9
10
11
12
13
14
15
16
# File 'lib/fast_fuzzy/ngram.rb', line 9

def self.generate(str_or_ary, n = 2)
  if (s = str_or_ary.size) <= n
    return [] if s.zero?
    return [str_or_ary]
  end

  (0..str_or_ary.size - n).map{|i| str_or_ary[i, n]}
end