literate_randomizer 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +32 -3
- data/lib/literate_randomizer.rb +15 -6
- data/lib/literate_randomizer/markov.rb +47 -13
- data/lib/literate_randomizer/version.rb +1 -1
- data/spec/literate_randomizer_spec.rb +33 -16
- metadata +1 -1
data/README.md
CHANGED
@@ -23,29 +23,54 @@ Example:
|
|
23
23
|
require 'literate_randomizer'
|
24
24
|
|
25
25
|
lr = LiterateRandomizer.create
|
26
|
+
|
26
27
|
lr.word
|
27
28
|
# => "frivolous"
|
29
|
+
|
28
30
|
lr.sentance
|
29
31
|
# => "Muscular arms round opening of sorts while Lord John Roxton."
|
32
|
+
|
30
33
|
lr.paragraph
|
31
34
|
# => "Tapped by his feet and the sign of the woods partly! Promoters might find such a row some rough hulking creatures we get a! Prove to think if we killed him state visit http While. Trusted ourselves and leaving the temperature ranges from the. Formally declared that if he will want to a howling the. Attentive neutrality. Helped to place where we heard the wide slow-moving clay-tinted stream and his. Itself as yet I was one circle of leathery wings as will understand."
|
32
35
|
|
36
|
+
puts lr.paragraphs
|
37
|
+
|
38
|
+
The last line outputs:
|
39
|
+
|
40
|
+
> Bane of him away from the bushes waiting for it all rushed me like. Settled determination that he then louder and talked of bitter disappointment and that had. Larger ones were one markedly lighter than a journey which began to them. Leave that foolish and without. Parcel of baggage gave a sight while in vain. Peculiarities are monstrous kangaroos. Volume with them.
|
41
|
+
|
42
|
+
> Communication reaches the less than harmonious. Absorb us must do not remind this scientific truth? Issued directions which contained the Chestnuts three. Ally of humor moving. Over it was a drawer he waved his beard the evening. Purged that proud delicate profile of this point I am so I could have. Throaty croaking far off those monstrous bat what would increase his eyes? Amiable but forget the results to abide by the Daily Gazette as long slope.
|
43
|
+
|
44
|
+
> Shredded into each in the pterodactyl we were engaged two or distributing a work. Refund from my pocket and rippling beard and turning into. Contact the first found at a thousand miles and tore. Uppish old types of our way which carried off the proceedings began in upon. Usual scientific mind was inclined to be described? Leafy archway began to provide. Here's something which I wondered where I thought came upon the body. Types surviving and numerous papers including checks online. Ran past ages the wood and Summerlee burst between his? Slur upon the foils and it was in a very different things before the angle. Portions of an intrusive rascals who agree to agree.
|
45
|
+
|
33
46
|
When creating a randomizer, there are a few options. The source_material should be a large selection of english text. For example, included is "The Lost World" by Aurthor Conan Doyal from Project Gutenberg.
|
34
47
|
|
35
|
-
|
48
|
+
**create** options:
|
49
|
+
|
50
|
+
LiterateRandomizer.create(options={})
|
36
51
|
:source_material => string OR
|
37
52
|
:source_material_file => filename
|
38
53
|
:randomizer => Random.new(seed=0)
|
39
54
|
:punctuation_distribution => DEFAULT_PUNCTUATION_DISTRIBUTION - punctiation is randomly selected from this array
|
40
55
|
|
41
|
-
|
56
|
+
**paragraph** options:
|
42
57
|
|
43
|
-
LiterateRandomizer.
|
58
|
+
LiterateRandomizer.paragraph(options={})
|
44
59
|
:first_word => nil - the start word
|
45
60
|
:words => range or int - number of words in sentance
|
46
61
|
:sentances => range or int - number of sentances in paragraph
|
47
62
|
:punctuation => nil - punction to end the sentance with (nil == randomly selected from punctuation_distribution)
|
48
63
|
|
64
|
+
**paragraphs** options:
|
65
|
+
|
66
|
+
LiterateRandomizer.paragraphs(options={})
|
67
|
+
:first_word => nil - the first word of the paragraph
|
68
|
+
:words => range or int - number of words in sentance
|
69
|
+
:sentances => range or int - number of sentances in paragraph
|
70
|
+
:punctuation => nil - punction to end the paragraph with (nil == randomly selected from punctuation_distribution)
|
71
|
+
:paragraphs => range or int - number of paragraphs in paragraph
|
72
|
+
:join => "\n\n" - join the paragraphs. if :join => false, returns an array of the paragraphs
|
73
|
+
|
49
74
|
Advanced example:
|
50
75
|
|
51
76
|
lr.paragraph :sentances => 5, :words => 3..8, :first_word => "A", :punctuation => "!!!"
|
@@ -61,6 +86,10 @@ If you just want to use a single, global instance, you can initialize and access
|
|
61
86
|
LiterateRandomizer.global.sentance
|
62
87
|
# => "Muscular arms round opening of sorts while Lord John Roxton."
|
63
88
|
|
89
|
+
# or even simpler, all methods on LiterateRandomizer are forward to LiterateRandomizer.global:
|
90
|
+
LiterateRandomizer.paragraph(:sentances => 3, :words => 3)
|
91
|
+
# => "Drama which would. Wrong fashion which. Throw them there."
|
92
|
+
|
64
93
|
## Contributing
|
65
94
|
|
66
95
|
1. Fork it
|
data/lib/literate_randomizer.rb
CHANGED
@@ -4,12 +4,21 @@ end
|
|
4
4
|
|
5
5
|
module LiterateRandomizer
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
class << self
|
8
|
+
def create(options={})
|
9
|
+
MarkovChain.new options
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
def global(options={})
|
13
|
+
@global_instance ||= MarkovChain.new options
|
14
|
+
end
|
14
15
|
|
16
|
+
def method_missing(method, *arguments, &block)
|
17
|
+
global.send(method, *arguments, &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def respond_to?(method)
|
21
|
+
super || global.respond_to?(method)
|
22
|
+
end
|
23
|
+
end
|
15
24
|
end
|
@@ -4,7 +4,8 @@
|
|
4
4
|
|
5
5
|
module LiterateRandomizer
|
6
6
|
class MarkovChain
|
7
|
-
DEFAULT_PUNCTUATION_DISTRIBUTION = %w{. . . . . . . . ? !}
|
7
|
+
DEFAULT_PUNCTUATION_DISTRIBUTION = %w{. . . . . . . . . . . . . . . . ? !}
|
8
|
+
PREPOSITION_REGEX = /^(the|to|and|a|in|that|it|if|is|was|for|on|as|an)$/
|
8
9
|
attr_accessor :randomizer, :init_options, :punctuation_distribution
|
9
10
|
attr_reader :markov_words, :words, :first_words
|
10
11
|
|
@@ -79,6 +80,11 @@ class MarkovChain
|
|
79
80
|
populate_markov_sum
|
80
81
|
end
|
81
82
|
|
83
|
+
def max(r)
|
84
|
+
return r if r.kind_of? Integer
|
85
|
+
r.max
|
86
|
+
end
|
87
|
+
|
82
88
|
def rand_count(r)
|
83
89
|
return r if r.kind_of? Integer
|
84
90
|
rand(r.max-r.min)+r.min
|
@@ -97,12 +103,6 @@ class MarkovChain
|
|
97
103
|
populate
|
98
104
|
end
|
99
105
|
|
100
|
-
class << self
|
101
|
-
def global(options={})
|
102
|
-
@global_randomizer = MarkovChain.new options
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
106
|
def inspect
|
107
107
|
"#<#{self.class}: #{@words.length} words, #{@markov_words.length} word-chains, #{@first_words.length} first_words>"
|
108
108
|
end
|
@@ -112,10 +112,10 @@ class MarkovChain
|
|
112
112
|
sum = @markov_weighted_sum[word]
|
113
113
|
random = rand(sum)+1
|
114
114
|
partial_sum = 0
|
115
|
-
markov_words[word].find do |
|
115
|
+
(markov_words[word].find do |w, count|
|
116
116
|
partial_sum += count
|
117
|
-
partial_sum >= random
|
118
|
-
end.first
|
117
|
+
w!=word && partial_sum >= random
|
118
|
+
end||[]).first
|
119
119
|
end
|
120
120
|
|
121
121
|
def rand(limit=nil)
|
@@ -143,6 +143,13 @@ class MarkovChain
|
|
143
143
|
def punctuation
|
144
144
|
@punctuation_distribution[rand(@punctuation_distribution.length)]
|
145
145
|
end
|
146
|
+
|
147
|
+
def extend_trailing_preposition(max_words,words)
|
148
|
+
while words.length < max_words && words[-1] && words[-1][PREPOSITION_REGEX]
|
149
|
+
words << next_word(words[-1])
|
150
|
+
end
|
151
|
+
words
|
152
|
+
end
|
146
153
|
|
147
154
|
# return a random sentance
|
148
155
|
# options:
|
@@ -151,12 +158,17 @@ class MarkovChain
|
|
151
158
|
# * :punctuation => nil - punction to end the sentance with (nil == randomly selected from punctuation_distribution)
|
152
159
|
def sentance(options={})
|
153
160
|
word = options[:first_word] || self.markov_word
|
154
|
-
|
161
|
+
num_words_option = options[:words] || (3..15)
|
162
|
+
count = rand_count num_words_option
|
155
163
|
punctuation = options[:punctuation] || self.punctuation
|
156
164
|
|
157
|
-
|
165
|
+
words = count.times.collect do
|
158
166
|
word.tap {word = next_word(word)}
|
159
|
-
end.compact
|
167
|
+
end.compact
|
168
|
+
|
169
|
+
words = extend_trailing_preposition(max(num_words_option), words)
|
170
|
+
|
171
|
+
capitalize words.compact.join(" ") + punctuation
|
160
172
|
end
|
161
173
|
|
162
174
|
# return a random paragraph
|
@@ -175,5 +187,27 @@ class MarkovChain
|
|
175
187
|
sentance op
|
176
188
|
end.join(" ")
|
177
189
|
end
|
190
|
+
|
191
|
+
# return random paragraphs
|
192
|
+
# options:
|
193
|
+
# * :first_word => nil - the first word of the paragraph
|
194
|
+
# * :words => range or int - number of words in sentance
|
195
|
+
# * :sentances => range or int - number of sentances in paragraph
|
196
|
+
# * :paragraphs => range or int - number of paragraphs in paragraph
|
197
|
+
# * :join => "\n\n" - join the paragraphs. if :join => false, returns an array of the paragraphs
|
198
|
+
# * :punctuation => nil - punction to end the paragraph with (nil == randomly selected from punctuation_distribution)
|
199
|
+
def paragraphs(options={})
|
200
|
+
count = rand_count options[:paragraphs] || (3..5)
|
201
|
+
join_str = options[:join]
|
202
|
+
|
203
|
+
res = count.times.collect do |i|
|
204
|
+
op = options.clone
|
205
|
+
op.delete :punctuation unless i==count-1
|
206
|
+
op.delete :first_word unless i==0
|
207
|
+
paragraph op
|
208
|
+
end
|
209
|
+
|
210
|
+
join_str!=false ? res.join(join_str || "\n\n") : res
|
211
|
+
end
|
178
212
|
end
|
179
213
|
end
|
@@ -8,6 +8,10 @@ describe LiterateRandomizer do
|
|
8
8
|
$lr
|
9
9
|
end
|
10
10
|
|
11
|
+
before(:each) do
|
12
|
+
LiterateRandomizer.global.randomizer = Random.new(1)
|
13
|
+
end
|
14
|
+
|
11
15
|
it "should be possible to create a randomizer" do
|
12
16
|
lr = new_lr
|
13
17
|
lr.should_not == nil
|
@@ -31,44 +35,57 @@ describe LiterateRandomizer do
|
|
31
35
|
end
|
32
36
|
|
33
37
|
it "sentance should return a random sentance" do
|
34
|
-
new_lr.sentance.should == "Bad form of my own chances are a riding-whip
|
38
|
+
new_lr.sentance.should == "Bad form of my own chances are a riding-whip."
|
35
39
|
end
|
36
40
|
|
37
41
|
it "sentance length should work" do
|
38
|
-
new_lr.sentance(:words => 1).should == "Bad
|
39
|
-
new_lr.sentance(:words => 3).should == "Bad
|
40
|
-
new_lr.sentance(:words => 5).should == "Bad
|
41
|
-
new_lr.sentance(:words => 7).should == "Bad
|
42
|
-
new_lr.sentance(:words => 9).should == "Bad
|
43
|
-
new_lr.sentance(:words => 2..7).should == "Bad job for a final credit
|
42
|
+
new_lr.sentance(:words => 1).should == "Bad."
|
43
|
+
new_lr.sentance(:words => 3).should == "Bad money if."
|
44
|
+
new_lr.sentance(:words => 5).should == "Bad money if ever come."
|
45
|
+
new_lr.sentance(:words => 7).should == "Bad money if ever come outwards at."
|
46
|
+
new_lr.sentance(:words => 9).should == "Bad money if ever come outwards at the side."
|
47
|
+
new_lr.sentance(:words => 2..7).should == "Bad job for a final credit."
|
44
48
|
end
|
45
49
|
|
46
50
|
it "successive calls should vary" do
|
47
51
|
lr = new_lr
|
48
|
-
lr.sentance.should == "Bad form of my own chances are a riding-whip
|
49
|
-
lr.sentance.should == "Hit you
|
50
|
-
lr.sentance.should == "
|
52
|
+
lr.sentance.should == "Bad form of my own chances are a riding-whip."
|
53
|
+
lr.sentance.should == "Hit you chaps think of battle Our young fellah when in Streatham."
|
54
|
+
lr.sentance.should == "Upward curves which should be through the whole tribe."
|
51
55
|
end
|
52
56
|
|
53
57
|
it "paragraph should work" do
|
54
|
-
|
55
|
-
lr.paragraph.should == "Bad form of my own chances are a riding-whip! Hit you that book down below as his tattered sketch-book which held. Seated upon their journey up my sleeve and incalculable people start to-morrow! Telling you propose to this half-educated age of the bushes at last supreme! Placed over us. Rubbing his strong sunlight struck me and Fate with the effect of. Columns until he came at a. Elusive enemies while beneath the main river up in it because on. Fully justified in the big as the bank of that the. Variety of photographs said for the words!"
|
58
|
+
new_lr.paragraph.should == "Bad form of my own chances are a riding-whip. Hit you chaps think of battle Our young fellah when in Streatham. Upward curves which should be through the whole tribe. Mend it at Edinburgh rose and it in diameter. Placed behind him. Rubbing his elephant-gun and sloth which way up to Project Gutenberg is going. Columns until he came at a last. Elusive enemies while beneath the main river up in it because on a boiling. Burying its coloring that skull and that there was able. Eventful moment of my clothes were to visit."
|
56
59
|
end
|
57
60
|
|
58
61
|
it "first_word should work" do
|
59
|
-
new_lr.paragraph(:sentances => 5, :words=>3).should == "Bad
|
60
|
-
new_lr.paragraph(:sentances => 2..4, :words=>3).should == "Bad
|
62
|
+
new_lr.paragraph(:sentances => 5, :words=>3).should == "Bad money if. Discreetly vague way. Melee in that. Hopin that dreadful. Executive and hold."
|
63
|
+
new_lr.paragraph(:sentances => 2..4, :words=>3).should == "Bad money if. Discreetly vague way. Melee in that."
|
61
64
|
end
|
62
65
|
|
63
66
|
it "first_word should work" do
|
64
|
-
new_lr.paragraph(:first_word => "A",:sentances => 5, :words=>3).should == "A roaring rumbling. Instanced a
|
67
|
+
new_lr.paragraph(:first_word => "A",:sentances => 5, :words=>3).should == "A roaring rumbling. Instanced a journalist. Eight after to-morrow. Hopin that dreadful. Executive and hold."
|
65
68
|
end
|
66
69
|
|
67
70
|
it "punctuation should work" do
|
68
|
-
new_lr.paragraph(:punctuation => "!!!",:sentances => 5, :words=>3).should == "Bad
|
71
|
+
new_lr.paragraph(:punctuation => "!!!",:sentances => 5, :words=>3).should == "Bad money if. Discreetly vague way. Melee in that. Hopin that dreadful. Executive and hold!!!"
|
69
72
|
end
|
70
73
|
|
71
74
|
it "global_randomizer_should work" do
|
72
75
|
LiterateRandomizer.global.class.should == LiterateRandomizer::MarkovChain
|
73
76
|
end
|
77
|
+
|
78
|
+
it "global_randomizer_should forwarding should work" do
|
79
|
+
LiterateRandomizer.respond_to?(:paragraph).should == true
|
80
|
+
LiterateRandomizer.respond_to?(:fonsfoaihdsfa).should == false
|
81
|
+
LiterateRandomizer.word.should == "own"
|
82
|
+
LiterateRandomizer.sentance.should == "Beak filled in the side of Vertebrate Evolution and up into private."
|
83
|
+
LiterateRandomizer.paragraph.should == "GUTENBERG-tm concept of their rat-trap grip upon Challenger of the carrying of! Precipices of me! Telling you with great enterprise upon their own eventual goal and it in a liar. The complete your. Historical architecture elaborated slowly defined our heads there came upon their leathery. Their signs of. Elusive enemies while beneath the main river up in it because on a boiling."
|
84
|
+
end
|
85
|
+
|
86
|
+
it "global_randomizer_should forwarding should work" do
|
87
|
+
LiterateRandomizer.paragraphs(:words =>2, :sentances => 2).should == "Bad money. Instanced a.\n\nFLAIL OF. Melee in.\n\nHit you. Executive and.\n\nHopes and. Puffing red-faced."
|
88
|
+
LiterateRandomizer.paragraphs(:words =>2, :sentances => 2, :join=>"--").should == "Pick holes. Telling you.--Mend it. Considerate of!--Albany and! Fame or?--The weak. Prime mover."
|
89
|
+
LiterateRandomizer.paragraphs(:words =>2, :sentances => 2, :join=>false).should == ["Reporters down. Again the.", "Their position. Dressing down.", "Chandeliers in. Although every."]
|
90
|
+
end
|
74
91
|
end
|