Class: LiterateRandomizer::Randomizer

Inherits:
Object
  • Object
show all
Defined in:
lib/literate_randomizer/randomizer.rb

Overview

The main class. Each instance has its own random number generator and can work against its own training source-material.

Constant Summary collapse

DEFAULT_PUNCTUATION_DISTRIBUTION =

The default punctuation distribution. Punctuation is pulled randomly from this array. It can contain any string.

%w{. . . . . . . . . . . . . . . . ? !}
PREPOSITION_REGEX =

LiterateRandomizer prefers to not end sentences with words that match the following regexp:

/^(had|the|to|or|and|a|in|that|it|if|of|is|was|for|on|as|an|your|our|my|per|until)$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Randomizer

Initialize a new instance. Each Markov randomizer instance can run against its own source_material.

Options:

  • :source_material => string OR

  • :source_material_file => filename

  • :punctuation_distribution => DEFAULT_PUNCTUATION_DISTRIBUTION punctiation is randomly selected from this array

Advanced options: (primiarilly for testing)

  • :randomizer => Random.new # must respond to #rand(limit)

  • :source_parser => SourceParser.new options

  • :model => MarkovModel.new :source_parser => source_parser



54
55
56
57
58
59
60
# File 'lib/literate_randomizer/randomizer.rb', line 54

def initialize(options={})
  @init_options = options
  @randomizer = randomizer || Random.new
  @punctuation_distribution = options[:punctuation_distribution] || DEFAULT_PUNCTUATION_DISTRIBUTION
  @source_parser = options[:source_parser] || SourceParser.new(options)
  @model = options[:model] || MarkovModel.new(:source_parser => source_parser)
end

Instance Attribute Details

#modelObject (readonly)

The random-generator model



26
27
28
# File 'lib/literate_randomizer/randomizer.rb', line 26

def model
  @model
end

#punctuation_distributionObject

To end setences, one of the strings in this array is selected at random (uniform-distribution)

Default: DEFAULT_PUNCTUATION_DISTRIBUTION



20
21
22
# File 'lib/literate_randomizer/randomizer.rb', line 20

def punctuation_distribution
  @punctuation_distribution
end

#randomizerObject

The source of all random values. Must implement: #rand(limit)

Default: Random.new()



15
16
17
# File 'lib/literate_randomizer/randomizer.rb', line 15

def randomizer
  @randomizer
end

#source_parserObject (readonly)

an instance of SourceParser attached to the source_material



23
24
25
# File 'lib/literate_randomizer/randomizer.rb', line 23

def source_parser
  @source_parser
end

Instance Method Details

#first_wordObject

return a random first word of a sentence



74
75
76
77
# File 'lib/literate_randomizer/randomizer.rb', line 74

def first_word
  @cached_first_word_keys ||= model.first_words.keys
  @cached_first_word_keys[rand(@cached_first_word_keys.length)]
end

#inspectObject

Returns a quick summary of the instance.



63
64
65
# File 'lib/literate_randomizer/randomizer.rb', line 63

def inspect
  "#<#{self.class}: #{model.words.length} words, #{model.markov_chains.length} word-chains, #{model.first_words.length} first_words>"
end

#paragraph(options = {}) ⇒ Object

return a random paragraph

Options:

  • :first_word => nil - the first word of the paragraph

  • :words => range or int - number of words in sentence

  • :sentences => range or int - number of sentences in paragraph

  • :punctuation => nil - punction to end the paragraph with (nil == randomly selected from punctuation_distribution)



119
120
121
122
123
124
125
126
127
128
# File 'lib/literate_randomizer/randomizer.rb', line 119

def paragraph(options={})
  count = Util.rand_count(options[:sentences] || (5..15),randomizer)

  count.times.collect do |i|
    op = options.clone
    op.delete :punctuation unless i==count-1
    op.delete :first_word unless i==0
    sentence op
  end.join(" ")
end

#paragraphs(options = {}) ⇒ Object

return random paragraphs

Options:

  • :first_word => nil - the first word of the paragraph

  • :words => range or int - number of words in sentence

  • :sentences => range or int - number of sentences in paragraph

  • :paragraphs => range or int - number of paragraphs in paragraph

  • :join => “nn” - join the paragraphs. if :join => false, returns an array of the paragraphs

  • :punctuation => nil - punction to end the paragraph with (nil == randomly selected from punctuation_distribution)



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/literate_randomizer/randomizer.rb', line 140

def paragraphs(options={})
  count = Util.rand_count(options[:paragraphs] || (3..5),randomizer)
  join_str = options[:join]

  res = count.times.collect do |i|
    op = options.clone
    op.delete :punctuation unless i==count-1
    op.delete :first_word unless i==0
    paragraph op
  end

  join_str!=false ? res.join(join_str || "\n\n") : res
end

#punctuationObject

return a random end-sentence string from punctuation_distribution



85
86
87
# File 'lib/literate_randomizer/randomizer.rb', line 85

def punctuation
  @punctuation_distribution[rand(@punctuation_distribution.length)]
end

#rand(limit = nil) ⇒ Object

return a random number generated by randomizer



80
81
82
# File 'lib/literate_randomizer/randomizer.rb', line 80

def rand(limit=nil)
  @randomizer.rand(limit)
end

#sentence(options = {}) ⇒ Object

return a random sentence

Options:

  • :first_word => nil - the start word

  • :words => range or int - number of words in sentence

  • :punctuation => nil - punction to end the sentence with (nil == randomly selected from punctuation_distribution)



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/literate_randomizer/randomizer.rb', line 96

def sentence(options={})
  word = options[:first_word] || self.first_word
  num_words_option = options[:words] || (3..15)
  count = Util.rand_count(num_words_option,randomizer)
  punctuation = options[:punctuation] || self.punctuation

  words = count.times.collect do
    word.tap {word = model.next_word(word,randomizer)}
  end.compact

  words = extend_trailing_preposition(Util.max(num_words_option), words)

  Util.capitalize words.compact.join(" ") + punctuation
end

#wordObject

return a random word



68
69
70
71
# File 'lib/literate_randomizer/randomizer.rb', line 68

def word
  @cached_word_keys ||= model.words.keys
  @cached_word_keys[rand(@cached_word_keys.length)]
end