Class: Clarifier::NGrams

Inherits:
Object
  • Object
show all
Defined in:
lib/clarifier/n_grams.rb

Instance Method Summary collapse

Instance Method Details

#n_grams(input, size = 2) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/clarifier/n_grams.rb', line 4

def n_grams(input, size = 2)
  words = input.split(/\W+/)
  ngrams = []
  words.each_with_index do |word, i|
    upper_limit = i + size - 1
    ngrams << words[i..upper_limit].join(' ') unless upper_limit >= words.length
  end
  ngrams
end