Class: Markovian::TextBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(corpus) ⇒ TextBuilder

Returns a new instance of TextBuilder.



8
9
10
# File 'lib/markovian/text_builder.rb', line 8

def initialize(corpus)
  @corpus = corpus
end

Instance Attribute Details

#corpusObject (readonly)

Returns the value of attribute corpus.



7
8
9
# File 'lib/markovian/text_builder.rb', line 7

def corpus
  @corpus
end

#seed_textObject (readonly)

Returns the value of attribute seed_text.



7
8
9
# File 'lib/markovian/text_builder.rb', line 7

def seed_text
  @seed_text
end

Instance Method Details

#construct(seed_text, length: 140, start_result_with_seed: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/markovian/text_builder.rb', line 12

def construct(seed_text, length: 140, start_result_with_seed: false)
  # TODO: if we don't hit a result for the first pair, move backward through the original text
  # until we get something
  seed_pair = identify_starter_text(seed_text)
  result_with_next_word(
    previous_pair: seed_pair,
    result: start_result_with_seed ? seed_text : nil,
    length: length
  )
end

#identify_starter_text(raw_text) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/markovian/text_builder.rb', line 23

def identify_starter_text(raw_text)
  seed_components = split_seed_text(raw_text)
  if seed_components.length >= 2
    seed_components[-2..-1]
  else
    # if we only have a one-word seed text, the previous word is nil
    [nil, seed_components.first]
  end
end