Class: Yada

Inherits:
Object
  • Object
show all
Defined in:
lib/yada.rb,
lib/yada/input.rb,
lib/yada/markov.rb,
lib/yada/version.rb

Defined Under Namespace

Classes: Input, Markov

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :ngram => 1,
  :tokenize => /[\w\-\/]+| ?[^\s]+/,
  :join => ' ',
  :punctuation => /[^\w\-\/]+/,
  :stop => /[\.\?!;]\s+/
}
VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Yada

Returns a new instance of Yada.



14
15
16
17
# File 'lib/yada.rb', line 14

def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge(options)
  @markov = Markov.new(@options[:ngram], @options[:tokenize], @options[:join])
end

Instance Method Details

#generate(n = 1) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/yada.rb', line 25

def generate(n = 1)
  start = [Markov::START] * @options[:ngram]
  (1..n).map do
    generate_tokens(start).reduce('') do |sentence, token|
      if token.match(@options[:punctuation])
        sentence + token
      else
        sentence + @options[:join] + token
      end
    end
  end
end

#train!(data) ⇒ Object



19
20
21
22
23
# File 'lib/yada.rb', line 19

def train!(data)
  input = Input.new(data, @options[:stop])
  @markov.train!(input)
  true
end