Class: BardBot::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/bard_bot/dictionary.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Dictionary

Returns a new instance of Dictionary.



3
4
5
6
7
8
9
# 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

Instance Method Details

#generate_sentenceObject



20
21
22
23
24
25
26
27
28
29
# 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!Object



11
12
13
14
15
16
17
18
# 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