Module: Raingrams::Helpers::Random
- Included in:
- Model
- Defined in:
- lib/raingrams/helpers/random.rb
Instance Method Summary collapse
-
#random_gram ⇒ Object
Returns a random gram from the model.
-
#random_gram_sentence(options = {}) ⇒ Object
Returns a randomly generated sentence of grams using the given options.
-
#random_ngram ⇒ Object
Returns a random ngram from the model.
-
#random_paragraph(options = {}) ⇒ Object
Returns a randomly generated paragraph of text using the given options.
-
#random_sentence(options = {}) ⇒ Object
Returns a randomly generated sentence of text using the given options.
-
#random_text(options = {}) ⇒ Object
Returns randomly generated text using the given options.
Instance Method Details
#random_gram ⇒ Object
Returns a random gram from the model.
7 8 9 10 11 |
# File 'lib/raingrams/helpers/random.rb', line 7 def random_gram prefix = @prefixes.keys[rand(@prefixes.length)] return prefix[rand(prefix.length)] end |
#random_gram_sentence(options = {}) ⇒ Object
Returns a randomly generated sentence of grams using the given options.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/raingrams/helpers/random.rb', line 31 def random_gram_sentence(={}) grams = [] last_ngram = @starting_ngram loop do next_ngrams = ngrams_prefixed_by(last_ngram.postfix).to_a last_ngram = next_ngrams[rand(next_ngrams.length)] if last_ngram.nil? return [] else last_gram = last_ngram.last break if last_gram == Tokens.stop grams << last_gram end end return grams end |
#random_ngram ⇒ Object
Returns a random ngram from the model.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/raingrams/helpers/random.rb', line 16 def random_ngram prefix_index = rand(@prefixes.length) prefix = @prefixes.keys[prefix_index] table = @prefixes.values[prefix_index] gram_index = rand(table.grams.length) return (prefix + table.grams[gram_index]) end |
#random_paragraph(options = {}) ⇒ Object
Returns a randomly generated paragraph of text using the given options.
options may contain the following keys:
:min_sentences
-
Minimum number of sentences in the paragraph. Defaults to 3.
:max_sentences
-
Maximum number of sentences in the paragraph. Defaults to 6.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/raingrams/helpers/random.rb', line 84 def random_paragraph(={}) min_sentences = ([:min_sentences] || 3) max_sentences = ([:max_sentences] || 6) sentences = [] (rand(max_sentences - min_sentences) + min_sentences).times do sentences << random_sentence() end return sentences.join(' ') end |
#random_sentence(options = {}) ⇒ Object
Returns a randomly generated sentence of text using the given options.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/raingrams/helpers/random.rb', line 57 def random_sentence(={}) grams = random_gram_sentence() sentence = grams.delete_if { |gram| gram == Tokens.start || gram == Tokens.stop }.join(' ') if @ignore_case sentence.capitalize! end if @ignore_punctuation sentence << '.' end return sentence end |
#random_text(options = {}) ⇒ Object
Returns randomly generated text using the given options.
options may contain the following keys:
:min_sentences
-
Minimum number of sentences in the paragraph. Defaults to 3.
:max_sentences
-
Maximum number of sentences in the paragraph. Defaults to 6.
:min_paragraphs
-
Minimum number of paragraphs in the text. Defaults to 3.
:max_paragraphs
-
Maximum number of paragraphs in the text. Defaults to 5.
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/raingrams/helpers/random.rb', line 109 def random_text(={}) min_paragraphs = ([:min_paragraphs] || 3) max_paragraphs = ([:max_paragraphs] || 6) paragraphs = [] (rand(max_paragraphs - min_paragraphs) + min_paragraphs).times do paragraphs << random_paragraph() end return paragraphs.join("\n\n") end |