Class: MarkovTextGenerator
- Inherits:
-
Object
- Object
- MarkovTextGenerator
- Defined in:
- lib/randomizer/markov_text_generator.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(text = DEFAULT_SOURCE_TEXT) ⇒ MarkovTextGenerator
constructor
A new instance of MarkovTextGenerator.
- #random(sentence_count = 10) ⇒ Object
- #remove_gutenberg_text(text) ⇒ Object
Constructor Details
#initialize(text = DEFAULT_SOURCE_TEXT) ⇒ MarkovTextGenerator
Returns a new instance of MarkovTextGenerator.
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 |
# File 'lib/randomizer/markov_text_generator.rb', line 15 def initialize(text = DEFAULT_SOURCE_TEXT) text = remove_gutenberg_text(text) words = text.split @end_sentence = [] @dict = {} prev1 = '' prev2 = '' for word in words if prev1 != '' && prev2 != '' key = [prev2, prev1] if @dict.key?(key) @dict[key] << word else @dict[key] = [word] @end_sentence << key if prev1[-1, 1] == '.' end end prev2 = prev1 prev1 = word end raise 'Sorry, there are no sentences in the text.' if @end_sentence.empty? true end |
Class Method Details
.instance ⇒ Object
11 12 13 |
# File 'lib/randomizer/markov_text_generator.rb', line 11 def self.instance @@instance ||= MarkovTextGenerator.new end |
Instance Method Details
#random(sentence_count = 10) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/randomizer/markov_text_generator.rb', line 51 def random(sentence_count = 10) results = [] key = nil while true if @dict.key?(key) word = @dict[key].random results << word key = [key.last, word] if @end_sentence.include?(key) results << '' sentence_count -= 1 break if sentence_count <= 0 end else key = @end_sentence.random end end results.join(' ') end |
#remove_gutenberg_text(text) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/randomizer/markov_text_generator.rb', line 42 def remove_gutenberg_text(text) text = text.sub(/^.*[*]{3} START OF THIS PROJECT GUTENBERG .*? [*]{3}/m, '') text = text.sub(/End of the Project Gutenberg.*$/im, '') text = text.sub(/End of Project Gutenberg.*$/im, '') text = text.gsub(/(\n|\r)[A-Z '"]+(\n|\r)/m, '') text = text.gsub(/_/, '') text = text.gsub(/\[Illustration.*?\]/, '') end |