Module Raingrams::Helpers::Random
In: lib/raingrams/helpers/random.rb
Model TrigramModel BigramModel QuadgramModel PentagramModel HexagramModel Model TrigramModel BigramModel QuadgramModel PentagramModel HexagramModel RuntimeError PrefixFrequencyMissing Set NgramSet Array Ngram Token StopSentence StartSentence Unknown ProbabilityTable Tokens Commonality Random Similarity Frequency Probability Helpers OpenModel OpenVocabulary Raingrams dot/f_1.png

Methods

Public Instance methods

Returns a random gram from the model.

[Source]

# File lib/raingrams/helpers/random.rb, line 7
      def random_gram
        prefix = @prefixes.keys[rand(@prefixes.length)]

        return prefix[rand(prefix.length)]
      end

Returns a randomly generated sentence of grams using the given options.

[Source]

# File lib/raingrams/helpers/random.rb, line 31
      def random_gram_sentence(options={})
        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

Returns a random ngram from the model.

[Source]

# 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

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.

[Source]

# File lib/raingrams/helpers/random.rb, line 84
      def random_paragraph(options={})
        min_sentences = (options[:min_sentences] || 3)
        max_sentences = (options[:max_sentences] || 6)
        sentences = []

        (rand(max_sentences - min_sentences) + min_sentences).times do
          sentences << random_sentence(options)
        end

        return sentences.join(' ')
      end

Returns a randomly generated sentence of text using the given options.

[Source]

# File lib/raingrams/helpers/random.rb, line 57
      def random_sentence(options={})
        grams = random_gram_sentence(options)
        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

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.

[Source]

# File lib/raingrams/helpers/random.rb, line 109
      def random_text(options={})
        min_paragraphs = (options[:min_paragraphs] || 3)
        max_paragraphs = (options[:max_paragraphs] || 6)
        paragraphs = []

        (rand(max_paragraphs - min_paragraphs) + min_paragraphs).times do
          paragraphs << random_paragraph(options)
        end

        return paragraphs.join("\n\n")
      end

[Validate]