Class: MarkovSentenceGenerator
- Inherits:
-
Object
- Object
- MarkovSentenceGenerator
- Defined in:
- lib/marky_markov/markov_sentence_generator.rb
Instance Method Summary collapse
-
#generate(wordcount) ⇒ String
Generates a sentence of (wordcount) length using the weighted_random function.
-
#generate_sentence(sentencecount) ⇒ String
Generates a (sentencecount) sentences using the weighted_random function.
-
#initialize(dictionary) ⇒ MarkovSentenceGenerator
constructor
:nodoc:.
- #punctuation?(word) ⇒ Boolean
-
#random_capitalized_word ⇒ Object
Generates a random capitalized word via picking a random key from the dictionary and recurring if the word is lowercase.
-
#random_word ⇒ String
Returns a random word via picking a random key from the dictionary.
-
#weighted_random(lastword) ⇒ Object
Returns a word based upon the likelihood of it appearing after the supplied word.
Constructor Details
#initialize(dictionary) ⇒ MarkovSentenceGenerator
:nodoc:
24 25 26 27 |
# File 'lib/marky_markov/markov_sentence_generator.rb', line 24 def initialize(dictionary) @dictionary = dictionary @depth = @dictionary.depth end |
Instance Method Details
#generate(wordcount) ⇒ String
Generates a sentence of (wordcount) length using the weighted_random function.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/marky_markov/markov_sentence_generator.rb', line 75 def generate(wordcount) if @dictionary.dictionary.empty? raise EmptyDictionaryError.new("The dictionary is empty! Parse a source file/string!") end sentence = [] sentence.concat(random_capitalized_word) (wordcount-1).times do word = weighted_random(sentence.last(@depth)) if punctuation?(word) sentence[-1] = sentence.last.dup << word sentence.concat(random_capitalized_word) elsif word.nil? sentence.concat(random_capitalized_word) else sentence << word end end sentence.pop(sentence.length-wordcount) sentence.join(' ') end |
#generate_sentence(sentencecount) ⇒ String
Generates a (sentencecount) sentences using the weighted_random function.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/marky_markov/markov_sentence_generator.rb', line 100 def generate_sentence(sentencecount) if @dictionary.dictionary.empty? raise EmptyDictionaryError.new("The dictionary is empty! Parse a source file/string!") end sentence = [] # Find out how many actual keys are in the dictionary. key_count = @dictionary.dictionary.keys.length # If less than 30 keys, use that plus five as your maximum sentence length. maximum_length = key_count < 30 ? key_count + 5 : 30 sentencecount.times do wordcount = 0 sentence.concat(random_capitalized_word) until (punctuation?(sentence.last[-1])) || wordcount > maximum_length wordcount += 1 word = weighted_random(sentence.last(@depth)) if punctuation?(word) sentence[-1] = sentence.last.dup << word else sentence << word end end end sentence.join(' ') end |
#punctuation?(word) ⇒ Boolean
67 68 69 |
# File 'lib/marky_markov/markov_sentence_generator.rb', line 67 def punctuation?(word) ( word =~ /[!?]/ || word == '.' ) end |
#random_capitalized_word ⇒ Object
Generates a random capitalized word via picking a random key from the dictionary and recurring if the word is lowercase.
(see #random_word)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/marky_markov/markov_sentence_generator.rb', line 44 def random_capitalized_word attempts = 0 # If you don't find a capitalized word after 15 attempts, just use # a lowercase word as there may be no capitals in the dicationary. until attempts > 15 attempts += 1 words = @dictionary.dictionary.keys random_choice = words[rand(words.length)] if random_choice[0] =~ /[A-Z]/ return random_choice end end random_word end |
#random_word ⇒ String
Returns a random word via picking a random key from the dictionary. In the case of the TwoWordDictionary, it returns two words to ensure that the sentence will have a valid two word string to pick the next word from. wordslength
35 36 37 38 |
# File 'lib/marky_markov/markov_sentence_generator.rb', line 35 def random_word words = @dictionary.dictionary.keys words[rand(words.length)] end |
#weighted_random(lastword) ⇒ Object
Returns a word based upon the likelihood of it appearing after the supplied word.
61 62 63 64 65 |
# File 'lib/marky_markov/markov_sentence_generator.rb', line 61 def weighted_random(lastword) # If word has no words in its dictionary (last word in source text file) # have it pick a random word to display instead. @dictionary.dictionary.fetch(lastword, NULL_OBJECT).sample end |