literate_randomizer 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +10 -10
- data/lib/literate_randomizer/markov.rb +20 -19
- data/lib/literate_randomizer/version.rb +1 -1
- data/spec/literate_randomizer_spec.rb +25 -25
- metadata +1 -1
data/README.md
CHANGED
@@ -27,11 +27,11 @@ Example:
|
|
27
27
|
lr.word
|
28
28
|
# => "frivolous"
|
29
29
|
|
30
|
-
lr.
|
30
|
+
lr.sentence
|
31
31
|
# => "Muscular arms round opening of sorts while Lord John Roxton."
|
32
32
|
|
33
33
|
lr.paragraph
|
34
|
-
# => "
|
34
|
+
# => "Fulmination against the wandering that the woes of this. Particular package of the back to matchwood. File with hideous jaws of Southampton. Adventure and he. Skewered on to pledge."
|
35
35
|
|
36
36
|
puts lr.paragraphs
|
37
37
|
|
@@ -57,23 +57,23 @@ When creating a randomizer, there are a few options. The source_material should
|
|
57
57
|
|
58
58
|
LiterateRandomizer.paragraph(options={})
|
59
59
|
:first_word => nil - the start word
|
60
|
-
:words => range or int - number of words in
|
61
|
-
:
|
62
|
-
:punctuation => nil - punction to end the
|
60
|
+
:words => range or int - number of words in sentence
|
61
|
+
:sentences => range or int - number of sentences in paragraph
|
62
|
+
:punctuation => nil - punction to end the sentence with (nil == randomly selected from punctuation_distribution)
|
63
63
|
|
64
64
|
**paragraphs** options:
|
65
65
|
|
66
66
|
LiterateRandomizer.paragraphs(options={})
|
67
67
|
:first_word => nil - the first word of the paragraph
|
68
|
-
:words => range or int - number of words in
|
69
|
-
:
|
68
|
+
:words => range or int - number of words in sentence
|
69
|
+
:sentences => range or int - number of sentences in paragraph
|
70
70
|
:punctuation => nil - punction to end the paragraph with (nil == randomly selected from punctuation_distribution)
|
71
71
|
:paragraphs => range or int - number of paragraphs in paragraph
|
72
72
|
:join => "\n\n" - join the paragraphs. if :join => false, returns an array of the paragraphs
|
73
73
|
|
74
74
|
Advanced example:
|
75
75
|
|
76
|
-
lr.paragraph :
|
76
|
+
lr.paragraph :sentences => 5, :words => 3..8, :first_word => "A", :punctuation => "!!!"
|
77
77
|
# => "A dense mob of our. Gods on that Challenger. Invariably to safety though. Weaponless but it my! Some bandy-legged lurching creature!!!"
|
78
78
|
|
79
79
|
If you just want to use a single, global instance, you can initialize and access it this way:
|
@@ -83,11 +83,11 @@ If you just want to use a single, global instance, you can initialize and access
|
|
83
83
|
LiterateRandomizer.global(options={})
|
84
84
|
|
85
85
|
# after the first call, options are ignored and the existing randomizer is returned
|
86
|
-
LiterateRandomizer.global.
|
86
|
+
LiterateRandomizer.global.sentence
|
87
87
|
# => "Muscular arms round opening of sorts while Lord John Roxton."
|
88
88
|
|
89
89
|
# or even simpler, all methods on LiterateRandomizer are forward to LiterateRandomizer.global:
|
90
|
-
LiterateRandomizer.paragraph(:
|
90
|
+
LiterateRandomizer.paragraph(:sentences => 3, :words => 3)
|
91
91
|
# => "Drama which would. Wrong fashion which. Throw them there."
|
92
92
|
|
93
93
|
## Contributing
|
@@ -1,11 +1,12 @@
|
|
1
|
-
#
|
2
|
-
#
|
3
|
-
#
|
1
|
+
# Inspiration:
|
2
|
+
# http://openmonkey.com/blog/2008/10/23/using-markov-chains-to-provide-english-language-seed-data-for-your-rails-application/
|
3
|
+
# by Tim Riley
|
4
|
+
# by Shane Brinkman-Davis
|
4
5
|
|
5
6
|
module LiterateRandomizer
|
6
7
|
class MarkovChain
|
7
8
|
DEFAULT_PUNCTUATION_DISTRIBUTION = %w{. . . . . . . . . . . . . . . . ? !}
|
8
|
-
PREPOSITION_REGEX = /^(the|to|and|a|in|that|it|if|is|was|for|on|as|an)$/
|
9
|
+
PREPOSITION_REGEX = /^(had|the|to|or|and|a|in|that|it|if|of|is|was|for|on|as|an|your|our|my|per|until)$/
|
9
10
|
attr_accessor :randomizer, :init_options, :punctuation_distribution
|
10
11
|
attr_reader :markov_words, :words, :first_words
|
11
12
|
|
@@ -41,7 +42,7 @@ class MarkovChain
|
|
41
42
|
word.chars.first.upcase+word[1..-1]
|
42
43
|
end
|
43
44
|
|
44
|
-
def
|
45
|
+
def source_sentences
|
45
46
|
source_material.split(/([.?!"]\s|--| ')+/)
|
46
47
|
end
|
47
48
|
|
@@ -56,8 +57,8 @@ class MarkovChain
|
|
56
57
|
@markov_words = {}
|
57
58
|
@words = {}
|
58
59
|
@first_words = {}
|
59
|
-
|
60
|
-
word_list = scrub_word_list
|
60
|
+
source_sentences.each do |sentence|
|
61
|
+
word_list = scrub_word_list sentence
|
61
62
|
@first_words[word_list[0]] = true
|
62
63
|
word_list.each_with_index do |word, index|
|
63
64
|
@words[word] = true
|
@@ -128,13 +129,13 @@ class MarkovChain
|
|
128
129
|
@cached_word_keys[rand(@cached_word_keys.length)]
|
129
130
|
end
|
130
131
|
|
131
|
-
# return a random first word of a
|
132
|
+
# return a random first word of a sentence
|
132
133
|
def first_word
|
133
134
|
@cached_first_word_keys ||= first_words.keys
|
134
135
|
@cached_first_word_keys[rand(@cached_first_word_keys.length)]
|
135
136
|
end
|
136
137
|
|
137
|
-
# return a random first word of a
|
138
|
+
# return a random first word of a sentence
|
138
139
|
def markov_word
|
139
140
|
@cached_markov_word_keys ||= markov_words.keys
|
140
141
|
@cached_markov_word_keys[rand(@cached_markov_word_keys.length)]
|
@@ -151,12 +152,12 @@ class MarkovChain
|
|
151
152
|
words
|
152
153
|
end
|
153
154
|
|
154
|
-
# return a random
|
155
|
+
# return a random sentence
|
155
156
|
# options:
|
156
157
|
# * :first_word => nil - the start word
|
157
|
-
# * :words => range or int - number of words in
|
158
|
-
# * :punctuation => nil - punction to end the
|
159
|
-
def
|
158
|
+
# * :words => range or int - number of words in sentence
|
159
|
+
# * :punctuation => nil - punction to end the sentence with (nil == randomly selected from punctuation_distribution)
|
160
|
+
def sentence(options={})
|
160
161
|
word = options[:first_word] || self.markov_word
|
161
162
|
num_words_option = options[:words] || (3..15)
|
162
163
|
count = rand_count num_words_option
|
@@ -174,25 +175,25 @@ class MarkovChain
|
|
174
175
|
# return a random paragraph
|
175
176
|
# options:
|
176
177
|
# * :first_word => nil - the first word of the paragraph
|
177
|
-
# * :words => range or int - number of words in
|
178
|
-
# * :
|
178
|
+
# * :words => range or int - number of words in sentence
|
179
|
+
# * :sentences => range or int - number of sentences in paragraph
|
179
180
|
# * :punctuation => nil - punction to end the paragraph with (nil == randomly selected from punctuation_distribution)
|
180
181
|
def paragraph(options={})
|
181
|
-
count = rand_count options[:
|
182
|
+
count = rand_count options[:sentences] || (5..15)
|
182
183
|
|
183
184
|
count.times.collect do |i|
|
184
185
|
op = options.clone
|
185
186
|
op.delete :punctuation unless i==count-1
|
186
187
|
op.delete :first_word unless i==0
|
187
|
-
|
188
|
+
sentence op
|
188
189
|
end.join(" ")
|
189
190
|
end
|
190
191
|
|
191
192
|
# return random paragraphs
|
192
193
|
# options:
|
193
194
|
# * :first_word => nil - the first word of the paragraph
|
194
|
-
# * :words => range or int - number of words in
|
195
|
-
# * :
|
195
|
+
# * :words => range or int - number of words in sentence
|
196
|
+
# * :sentences => range or int - number of sentences in paragraph
|
196
197
|
# * :paragraphs => range or int - number of paragraphs in paragraph
|
197
198
|
# * :join => "\n\n" - join the paragraphs. if :join => false, returns an array of the paragraphs
|
198
199
|
# * :punctuation => nil - punction to end the paragraph with (nil == randomly selected from punctuation_distribution)
|
@@ -21,37 +21,37 @@ describe LiterateRandomizer do
|
|
21
21
|
new_lr.words.length.should == 9143
|
22
22
|
end
|
23
23
|
|
24
|
-
it "first_words.length should be the number words starting
|
24
|
+
it "first_words.length should be the number words starting sentences in the file" do
|
25
25
|
new_lr.first_words.length.should == 754
|
26
26
|
end
|
27
27
|
|
28
|
-
it "
|
29
|
-
new_lr.
|
30
|
-
new_lr.
|
28
|
+
it "source_sentences.length should be the number of sentences in the file" do
|
29
|
+
new_lr.source_sentences.length.should == 10699
|
30
|
+
new_lr.source_sentences.length.should > new_lr.first_word.length
|
31
31
|
end
|
32
32
|
|
33
33
|
it "word should return a random word" do
|
34
34
|
new_lr.word.should == "own"
|
35
35
|
end
|
36
36
|
|
37
|
-
it "
|
38
|
-
new_lr.
|
37
|
+
it "sentence should return a random sentence" do
|
38
|
+
new_lr.sentence.should == "Bad form of my own chances are a riding-whip."
|
39
39
|
end
|
40
40
|
|
41
|
-
it "
|
42
|
-
new_lr.
|
43
|
-
new_lr.
|
44
|
-
new_lr.
|
45
|
-
new_lr.
|
46
|
-
new_lr.
|
47
|
-
new_lr.
|
41
|
+
it "sentence length should work" do
|
42
|
+
new_lr.sentence(:words => 1).should == "Bad."
|
43
|
+
new_lr.sentence(:words => 3).should == "Bad money if."
|
44
|
+
new_lr.sentence(:words => 5).should == "Bad money if ever come."
|
45
|
+
new_lr.sentence(:words => 7).should == "Bad money if ever come outwards at."
|
46
|
+
new_lr.sentence(:words => 9).should == "Bad money if ever come outwards at the side."
|
47
|
+
new_lr.sentence(:words => 2..7).should == "Bad job for a final credit."
|
48
48
|
end
|
49
49
|
|
50
50
|
it "successive calls should vary" do
|
51
51
|
lr = new_lr
|
52
|
-
lr.
|
53
|
-
lr.
|
54
|
-
lr.
|
52
|
+
lr.sentence.should == "Bad form of my own chances are a riding-whip."
|
53
|
+
lr.sentence.should == "Hit you chaps think of battle Our young fellah when in Streatham."
|
54
|
+
lr.sentence.should == "Upward curves which should be through the whole tribe."
|
55
55
|
end
|
56
56
|
|
57
57
|
it "paragraph should work" do
|
@@ -59,16 +59,16 @@ describe LiterateRandomizer do
|
|
59
59
|
end
|
60
60
|
|
61
61
|
it "first_word should work" do
|
62
|
-
new_lr.paragraph(:
|
63
|
-
new_lr.paragraph(:
|
62
|
+
new_lr.paragraph(:sentences => 5, :words=>3).should == "Bad money if. Discreetly vague way. Melee in that. Hopin that dreadful. Executive and hold."
|
63
|
+
new_lr.paragraph(:sentences => 2..4, :words=>3).should == "Bad money if. Discreetly vague way. Melee in that."
|
64
64
|
end
|
65
65
|
|
66
66
|
it "first_word should work" do
|
67
|
-
new_lr.paragraph(:first_word => "A",:
|
67
|
+
new_lr.paragraph(:first_word => "A",:sentences => 5, :words=>3).should == "A roaring rumbling. Instanced a journalist. Eight after to-morrow. Hopin that dreadful. Executive and hold."
|
68
68
|
end
|
69
69
|
|
70
70
|
it "punctuation should work" do
|
71
|
-
new_lr.paragraph(:punctuation => "!!!",:
|
71
|
+
new_lr.paragraph(:punctuation => "!!!",:sentences => 5, :words=>3).should == "Bad money if. Discreetly vague way. Melee in that. Hopin that dreadful. Executive and hold!!!"
|
72
72
|
end
|
73
73
|
|
74
74
|
it "global_randomizer_should work" do
|
@@ -79,13 +79,13 @@ describe LiterateRandomizer do
|
|
79
79
|
LiterateRandomizer.respond_to?(:paragraph).should == true
|
80
80
|
LiterateRandomizer.respond_to?(:fonsfoaihdsfa).should == false
|
81
81
|
LiterateRandomizer.word.should == "own"
|
82
|
-
LiterateRandomizer.
|
83
|
-
LiterateRandomizer.paragraph.should == "GUTENBERG-tm concept of their rat-trap grip upon Challenger of the carrying of
|
82
|
+
LiterateRandomizer.sentence.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 selfishness! Telling you with great enterprise upon their own eventual goal and it in a liar. The complete your consent. Reporters down at a tangle of the huge flippers behind us in writing. Chandeliers in those huge wings of this agreement and Ipetu. Taken the gray eyes were general laws of what. Variety of photographs said for the words?"
|
84
84
|
end
|
85
85
|
|
86
86
|
it "global_randomizer_should forwarding should work" do
|
87
|
-
LiterateRandomizer.paragraphs(:words =>2, :
|
88
|
-
LiterateRandomizer.paragraphs(:words =>2, :
|
89
|
-
LiterateRandomizer.paragraphs(:words =>2, :
|
87
|
+
LiterateRandomizer.paragraphs(:words =>2, :sentences => 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, :sentences => 2, :join=>"--").should == "Pick holes. Telling you.--Mend it. Considerate of!--Albany and! Fame or?--The weak. Prime mover."
|
89
|
+
LiterateRandomizer.paragraphs(:words =>2, :sentences => 2, :join=>false).should == ["Reporters down. Again the.", "Their position. Dressing down.", "Chandeliers in. Although every."]
|
90
90
|
end
|
91
91
|
end
|