Class: NGram

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

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ NGram

Returns a new instance of NGram.



6
7
8
9
10
# File 'lib/ngram.rb', line 6

def initialize(opts={})
    @size = opts[:size]||2
    @word_separator = opts[:word_separator]||" "
    @padchar = opts[:padchar]||"_"
end

Instance Attribute Details

#padcharObject

Returns the value of attribute padchar.



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

def padchar
  @padchar
end

#sizeObject

Returns the value of attribute size.



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

def size
  @size
end

#word_separatorObject

Returns the value of attribute word_separator.



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

def word_separator
  @word_separator
end

Instance Method Details

#parse(phrase) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/ngram.rb', line 12

def parse(phrase)
    words = phrase.split(@separator)
    if words.length == 1
        process(phrase)
    else
        words.map { |w| process(w) }
    end
end