Method: #ngrams

Defined in:
lib/zipf/stringutil.rb

#ngrams(s, n, fix = false) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/zipf/stringutil.rb', line 5

def ngrams(s, n, fix=false)
  a = tokenize s
  a.each_with_index { |tok, i|
    tok.strip!
    0.upto([n-1, a.size-i-1].min) { |m|
      yield a[i..i+m] if !fix||(fix&&a[i..i+m].size==n)
    }
  }
end