Class: Ngram

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, options = { regex: / / }) ⇒ Ngram

Returns a new instance of Ngram.



4
5
6
7
# File 'lib/ruby_nlp/ngram.rb', line 4

def initialize(target, options = { regex: / / })
  @target = target
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



2
3
4
# File 'lib/ruby_nlp/ngram.rb', line 2

def options
  @options
end

Instance Method Details

#bigramsObject



17
18
19
# File 'lib/ruby_nlp/ngram.rb', line 17

def bigrams
  ngrams(2)
end

#ngrams(n) ⇒ Object



9
10
11
# File 'lib/ruby_nlp/ngram.rb', line 9

def ngrams(n)
  @target.split(@options[:regex]).each_cons(n).to_a
end

#trigramsObject



21
22
23
# File 'lib/ruby_nlp/ngram.rb', line 21

def trigrams
  ngrams(3)
end

#unigramsObject



13
14
15
# File 'lib/ruby_nlp/ngram.rb', line 13

def unigrams
  ngrams(1)
end