class BardBot::Dictionary
Public Class Methods
new(config)
click to toggle source
# File lib/bard_bot/dictionary.rb, line 3 def initialize(config) @prefix = config.prefix.to_i @max_length = config.max_length @file_path = File.join(config.character_dir, "#{config.character}.txt") @dictionary = Hash.new { |h, k| h[k] = [] } load_corpus! end
Public Instance Methods
generate_sentence()
click to toggle source
# File lib/bard_bot/dictionary.rb, line 20 def generate_sentence tuple = @dictionary.keys.sample sentence = tuple @max_length.times do sentence += ' ' + @dictionary[tuple].sample break if %w( ? ! . ).include?(sentence[-1]) tuple = sentence.split.last(2).join end sentence.downcase.capitalize end
load_corpus!()
click to toggle source
# File lib/bard_bot/dictionary.rb, line 11 def load_corpus! corpus = File.read(@file_path).split until corpus.length < (@prefix + 1) key = corpus.first(@prefix).join @dictionary[key] << corpus[@prefix] corpus.shift end end