class Guillaume::Line

Public Class Methods

new(seed = nil) click to toggle source
# File lib/guillaume/line.rb, line 2
def initialize(seed = nil)
  @line_parts = seed.split(/ /)
end

Public Instance Methods

build(ngrams) click to toggle source
# File lib/guillaume/line.rb, line 6
def build(ngrams)
  begin
    @line_parts << next_word(ngrams)
  end until @line_parts.last.nil?
  @line_parts.join(" ").strip
end
choose_word(matches) click to toggle source
# File lib/guillaume/line.rb, line 22
def choose_word(matches)
  possible_choices(matches).sample
end
next_word(ngrams, options = { look_behind: 1 }) click to toggle source
# File lib/guillaume/line.rb, line 13
def next_word(ngrams, options = { look_behind: 1 })
  search_words = @line_parts[-options[:look_behind]..-1]
  choose_word(ngram_matches(ngrams, search_words))
end
ngram_matches(ngrams, search_words) click to toggle source
# File lib/guillaume/line.rb, line 18
def ngram_matches(ngrams, search_words)
  ngrams.select { |gram| gram[0..search_words.count-1] == search_words }
end
possible_choices(matches) click to toggle source
# File lib/guillaume/line.rb, line 26
def possible_choices(matches)
  matches.map { |res| res.last }.sort
end