Class: String

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

Instance Method Summary collapse

Instance Method Details

#ngrams(size) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/zeiger.rb', line 13

def ngrams size
  padding = " " * (size - 1)
  txt = self
  ngrams = Hash.new { |h,k| h[k] = 0 }
  regex = Regexp.compile("." * size)

  result = []

  (txt.length - 2).times do |i|
    result << txt[i..(i + 2)]
  end

  result
end