class Guillaume::Poem

Attributes

corpora[RW]
lines[RW]
poem[RW]

Public Class Methods

new(corpora, options = { seed: nil, max_stanzas: 10 }) click to toggle source
# File lib/guillaume/poem.rb, line 4
def initialize(corpora, options = { seed: nil, max_stanzas: 10 })
  @corpora = corpora
  @first_seed = options[:seed].nil? ? random_seed(@corpora.unigrams) : options[:seed]
  @max_stanzas = options[:max_stanzas]
  @lines = 0
  @stanzas = 0
  @poem = write
end

Public Instance Methods

formatted() click to toggle source
# File lib/guillaume/poem.rb, line 48
def formatted
  @formatted ||= @poem.join("\n")
end
random_seed(ngrams) click to toggle source
# File lib/guillaume/poem.rb, line 13
def random_seed(ngrams)
  ngrams.select { |word| word.first[0] =~ /[A-Z]/ }.sample.first
end
stanza(lines_memo = []) click to toggle source

TODO: which_gram TODO: thesaurus stuff for topic staying/straying TODO: explicit num_lines option

# File lib/guillaume/poem.rb, line 33
def stanza(lines_memo = [])
  #which_gram = :bigrams

  if rand(100) <= lines_memo.count * 10 # never more than 10 lines this way
    return lines_memo << ""
  else
    line = Guillaume::Line.new(random_seed(@corpora.unigrams)).build(@corpora.bigrams)
    if line.length > 80
      line = Guillaume::Poetics.enjamb 40, line # 40% chance to break a long line
    end
    lines_memo << line
    stanza(lines_memo)
  end
end
write(lines_memo = []) click to toggle source
# File lib/guillaume/poem.rb, line 17
def write(lines_memo = [])
  if rand(@max_stanzas) < @stanzas
    return lines_memo
  else
    $LOGGER.info("Writing stanza #{@stanzas + 1}...")
    lines_memo += stanza
    @stanzas += 1
    write(lines_memo)
  end
end