-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlitbot.rb
67 lines (57 loc) · 1.62 KB
/
litbot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class LitBot
def eat_file(file_name)
file = File.open(file_name)
self.source_text = file.read
file.close
end
def speak_first_10_words
puts source_text.split(' ').first(10)
end
def digest_file
@source_text_words = source_text.split(' ')
@word_pairs_and_probabilities = {}
@source_text_words.each_with_index do |word, index|
hash_key = "#{word} #{@source_text_words[index + 1]}"
hash_value = @source_text_words[index + 2]
if @word_pairs_and_probabilities[hash_key]
@word_pairs_and_probabilities[hash_key] << hash_value
else
@word_pairs_and_probabilities[hash_key] = [hash_value]
end
end
end
def speak
output_text = generate_random_seed_pair
story = 0
while story < 35 do
word_pair = output_text.last(2).join(' ')
next_word = @word_pairs_and_probabilities[word_pair].sample unless @word_pairs_and_probabilities[word_pair].nil?
output_text << next_word
if next_word && (next_word.include?(".") || next_word.include?("?"))
story += 1
end
end
output_text.join(' ')
end
def generate_random_seed_pair
trial_pair = @word_pairs_and_probabilities.keys.sample
until trial_pair[0] == trial_pair[0].upcase do
trial_pair = @word_pairs_and_probabilities.keys.sample
end
trial_pair.split(' ')
end
def +(other_bot)
shiny_new_litbot = LitBot.new(source_text + other_bot.source_text)
end
#getter
def source_text
@source_text
end
#setter
def source_text=(text)
@source_text = text
end
def initialize(text="Any string")
self.source_text = text
end
end