Class: Chainer

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

Overview

class

Constant Summary collapse

BEGINNING =
"__BEGIN__"
ENDING =
"__END__"
MAX_ATTEMPTS =
15

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dictionary) ⇒ Chainer

Returns a new instance of Chainer.



11
12
13
14
15
16
# File 'lib/markovite/chainer.rb', line 11

def initialize(dictionary)
  self.dictionary = dictionary
  @depth = dictionary.depth
  #takes in a dictionary object
  #constructs chain from dictionary object public interface
end

Instance Attribute Details

#depthObject

Returns the value of attribute depth.



5
6
7
# File 'lib/markovite/chainer.rb', line 5

def depth
  @depth
end

#dictionaryObject

Returns the value of attribute dictionary.



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

def dictionary
  @dictionary
end

Instance Method Details

#make_sentenceObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/markovite/chainer.rb', line 48

def make_sentence
  raise "No corpus in memory" if dictionary.nil?
  attempts = 0
  while attempts < MAX_ATTEMPTS
    sentence = generate_text
    if is_valid_sentence?(sentence)
      return sentence
    else
      attempts += 1
    end
  end
  nil
end

#make_sentence_of_length(how_long) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/markovite/chainer.rb', line 28

def make_sentence_of_length(how_long)
  begin
    make_sentence_with_block {|sentence| sentence.length <= how_long}
  rescue NoMethodError
    return nil
  end
end

#make_sentence_starts_with(phrase) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/markovite/chainer.rb', line 18

def make_sentence_starts_with(phrase)
  chunk = get_chunk(phrase)
  begin
    partial = generate_text(chunk)
  rescue ArgumentError
    return nil
  end
  "#{phrase} #{partial}"
end

#make_sentences(amount, condition = true) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/markovite/chainer.rb', line 36

def make_sentences(amount, condition=true)
  sentences = []
  amount.times do
    sentences << make_sentence
  end
  sentences.join(' ')
end