madlibs 1.0.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -30
- data/lib/madlibs/dictionary.rb +19 -0
- data/lib/madlibs/madlib.rb +16 -51
- data/lib/madlibs/template.rb +34 -0
- data/lib/madlibs/version.rb +1 -1
- data/madlibs.gemspec +1 -1
- data/specs/dictionary_spec.rb +62 -0
- data/specs/madlib_spec.rb +44 -60
- data/specs/template_spec.rb +68 -0
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50e1725db43dffc6104a7a07115b23f7f61cdefa
|
4
|
+
data.tar.gz: 684394e817d9405bcaf0d05b46716e4962a56f34
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ced0191503cb29e9dd03b06c978b4b2807cd4c264ac40d671e8a3263b96329d4cd14b95d329ff8ffefde9deeffc98be29259c277205b2711cb73d572e174a363
|
7
|
+
data.tar.gz: 6cfe7a0a5d96f166bfbdb3b190895d1e66cf9e41d9a8e4b3c0d2330adf6a48bcb6b672bd060ba082712c3124c1d9e10576dd5b6cffba176ce1626778ce6416cc
|
data/README.md
CHANGED
@@ -1,8 +1,5 @@
|
|
1
1
|
# Madlibs
|
2
2
|
|
3
|
-
Sometimes you just need to make something silly on a whim. That's exactly
|
4
|
-
what this gem is and does.
|
5
|
-
|
6
3
|
This gem simply takes a template, does some randomizing, applies a dictionary,
|
7
4
|
and produces a string. Depending on the dictionary, it can be quite humorous.
|
8
5
|
|
@@ -86,33 +83,30 @@ interchangeable words.
|
|
86
83
|
```rb
|
87
84
|
require 'madlibs'
|
88
85
|
|
89
|
-
# Create a new Madlib object, passing a template
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
"
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
]
|
114
|
-
|
115
|
-
})
|
86
|
+
# Create a new Madlib object, passing a template and a dictionary. You can also
|
87
|
+
# pass nothing and assign them after the fact with `.dictionary` and `.template`
|
88
|
+
madlib = Madlibs::Madlib.new(
|
89
|
+
'I will <verb> (the|another)( <adjective>)? <noun>',
|
90
|
+
{
|
91
|
+
"nouns" => [
|
92
|
+
"proxy",
|
93
|
+
"cache",
|
94
|
+
"architecture",
|
95
|
+
"fragmentation",
|
96
|
+
],
|
97
|
+
"verbs" => [
|
98
|
+
"refactor",
|
99
|
+
"implement",
|
100
|
+
"replicate",
|
101
|
+
"debug",
|
102
|
+
],
|
103
|
+
"adjectives" => [
|
104
|
+
"distributed",
|
105
|
+
"imperative",
|
106
|
+
"virtualized",
|
107
|
+
]
|
108
|
+
}
|
109
|
+
)
|
116
110
|
|
117
111
|
# Generate your string(s)
|
118
112
|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Madlibs
|
2
|
+
class Dictionary
|
3
|
+
|
4
|
+
def initialize hash
|
5
|
+
# deep clone to prevent side-effects
|
6
|
+
@hash = Marshal.load(Marshal.dump(hash))
|
7
|
+
@hash_clone = Marshal.load(Marshal.dump(hash))
|
8
|
+
end
|
9
|
+
|
10
|
+
def retrieve type
|
11
|
+
return '' if @hash[type].nil? or @hash[type].empty?
|
12
|
+
@hash[type].delete @hash[type].sample
|
13
|
+
end
|
14
|
+
|
15
|
+
def reload
|
16
|
+
@hash = Marshal.load(Marshal.dump(@hash_clone))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/madlibs/madlib.rb
CHANGED
@@ -1,65 +1,30 @@
|
|
1
1
|
require 'active_support/inflector'
|
2
|
+
require 'madlibs/template'
|
3
|
+
require 'madlibs/dictionary'
|
2
4
|
|
3
5
|
module Madlibs
|
4
6
|
class Madlib
|
7
|
+
attr_accessor :template
|
8
|
+
attr_reader :dictionary
|
5
9
|
|
6
|
-
def initialize template,
|
7
|
-
|
8
|
-
|
9
|
-
@dictionary = dictionary.clone
|
10
|
-
@original_dictionary = dictionary.clone
|
11
|
-
end
|
10
|
+
def initialize template = '', dictionary_hash = {}
|
11
|
+
self.template = template
|
12
|
+
self.dictionary = dictionary_hash
|
12
13
|
end
|
13
14
|
|
14
|
-
def
|
15
|
-
@
|
16
|
-
@dictionary = dictionary.clone
|
17
|
-
end
|
18
|
-
|
19
|
-
def reload
|
20
|
-
@dictionary = @original_dictionary.clone
|
21
|
-
end
|
22
|
-
|
23
|
-
def retrieve type
|
24
|
-
types = type.pluralize
|
25
|
-
@dictionary[types].delete @dictionary[types].sample
|
26
|
-
end
|
27
|
-
|
28
|
-
def extract_keys template
|
29
|
-
template.scan(/<([^>]*?)>/).flatten
|
30
|
-
end
|
31
|
-
|
32
|
-
def extract_word_lists template
|
33
|
-
template.scan(/\([|a-zA-Z]*\)/)
|
15
|
+
def dictionary=(hash)
|
16
|
+
@dictionary = Dictionary.new hash
|
34
17
|
end
|
35
18
|
|
36
19
|
def generate
|
37
|
-
reload
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
product.gsub! words, words.gsub(/[()]/, '').split('|').sample
|
43
|
-
end
|
44
|
-
|
45
|
-
loop do
|
46
|
-
optionals = product.scan(/\([^\(]*?\)(?=\?)/)
|
47
|
-
break unless optionals.any?
|
48
|
-
optionals.each do |o|
|
49
|
-
if [true, false].sample
|
50
|
-
used_string = o.gsub(/[()]/, '')
|
51
|
-
product.sub! "#{o}?", used_string
|
52
|
-
else
|
53
|
-
product.sub! "#{o}?", ''
|
20
|
+
@dictionary.reload
|
21
|
+
Template.process_word_lists(
|
22
|
+
Template.process_optionals(
|
23
|
+
Template.keys(@template).reduce(@template) do |temp, key|
|
24
|
+
Template.process_key temp, key, @dictionary.retrieve(key.pluralize)
|
54
25
|
end
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
extract_keys(product).each do |key|
|
59
|
-
product.sub! "<#{key}>", retrieve(key)
|
60
|
-
end
|
61
|
-
|
62
|
-
product
|
26
|
+
)
|
27
|
+
)
|
63
28
|
end
|
64
29
|
end
|
65
30
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Madlibs
|
2
|
+
module Template
|
3
|
+
|
4
|
+
def self.keys template
|
5
|
+
template.scan(/<([^>]*?)>/).map do |key|
|
6
|
+
key.first
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.process_key template, key, value
|
11
|
+
template.sub "<#{key}>", value
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.process_word_lists template
|
15
|
+
template.scan(/\([|a-zA-Z]*\)/).reduce(template) do |temp, words|
|
16
|
+
temp.sub words, words.gsub(/[()]/, '').split('|').sample
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.process_optionals template
|
21
|
+
loop do
|
22
|
+
optionals = template.scan(/\([^\(]*?\)(?=\?)/)
|
23
|
+
break if optionals.empty?
|
24
|
+
template = optionals.reduce(template) do |temp, opt|
|
25
|
+
temp.sub(
|
26
|
+
"#{opt}?",
|
27
|
+
if [true, false].sample then opt.gsub(/[()]/, '') else '' end
|
28
|
+
)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
template
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/madlibs/version.rb
CHANGED
data/madlibs.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
spec.email = ["me@jondavidjohn.com"]
|
12
12
|
spec.description = %q{Because sometimes, you just need to make something stupid.}
|
13
13
|
spec.summary = %q{Create generated text with a combination of coditional templating and a provided dictionary}
|
14
|
-
spec.homepage = ""
|
14
|
+
spec.homepage = "https://github.com/jondavidjohn/madlibs"
|
15
15
|
spec.license = "Apache v2"
|
16
16
|
|
17
17
|
spec.files = `git ls-files`.split($/)
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'minitest/spec'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'madlibs'
|
4
|
+
|
5
|
+
describe Madlibs::Dictionary do
|
6
|
+
describe '#retrieve' do
|
7
|
+
it 'should retrieve a random dictionary term in a given collection' do
|
8
|
+
hash = {
|
9
|
+
'things' => [
|
10
|
+
'thing_one',
|
11
|
+
'thing_two',
|
12
|
+
],
|
13
|
+
'items' => [
|
14
|
+
'item_one',
|
15
|
+
'item_two',
|
16
|
+
'item_three',
|
17
|
+
]
|
18
|
+
}
|
19
|
+
|
20
|
+
dictionary = Madlibs::Dictionary.new(hash)
|
21
|
+
hash['things'].must_include dictionary.retrieve 'things'
|
22
|
+
hash['items'].must_include dictionary.retrieve 'items'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should retrieve random dictionary items only once' do
|
26
|
+
dictionary = Madlibs::Dictionary.new({
|
27
|
+
'items' => [
|
28
|
+
'item_one',
|
29
|
+
'item_two',
|
30
|
+
'item_three',
|
31
|
+
]
|
32
|
+
})
|
33
|
+
|
34
|
+
retrieved = []
|
35
|
+
retrieved.push dictionary.retrieve 'items'
|
36
|
+
retrieved.push dictionary.retrieve 'items'
|
37
|
+
retrieved.push dictionary.retrieve 'items'
|
38
|
+
|
39
|
+
retrieved.size.must_be_same_as retrieved.uniq.size
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should retrieve empty string from empty dictionary collection' do
|
43
|
+
dictionary = Madlibs::Dictionary.new({ 'items' => [ ] })
|
44
|
+
dictionary.retrieve('items').must_equal ''
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should not mutate the provided hash'do
|
48
|
+
hash = {
|
49
|
+
'items' => [
|
50
|
+
'item_one',
|
51
|
+
'item_two',
|
52
|
+
'item_three',
|
53
|
+
]
|
54
|
+
}
|
55
|
+
|
56
|
+
dictionary = Madlibs::Dictionary.new(hash)
|
57
|
+
dictionary.retrieve 'items'
|
58
|
+
dictionary.retrieve 'items'
|
59
|
+
hash['items'].size.must_equal 3
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/specs/madlib_spec.rb
CHANGED
@@ -3,66 +3,50 @@ require 'minitest/autorun'
|
|
3
3
|
require 'madlibs'
|
4
4
|
|
5
5
|
describe Madlibs::Madlib do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
6
|
+
describe 'generate' do
|
7
|
+
it 'should work' do
|
8
|
+
madlib = Madlibs::Madlib.new(
|
9
|
+
'(wow, )?such <noun>,( very <adjective>,)? so <verb>',
|
10
|
+
{
|
11
|
+
'nouns' => [
|
12
|
+
'desk',
|
13
|
+
'lamp',
|
14
|
+
],
|
15
|
+
'adjectives' => [
|
16
|
+
'neat',
|
17
|
+
'fun',
|
18
|
+
],
|
19
|
+
'verbs' => [
|
20
|
+
'commit',
|
21
|
+
'scare',
|
22
|
+
]
|
23
|
+
}
|
24
|
+
)
|
25
|
+
|
26
|
+
[ 'such lamp, very neat, so commit',
|
27
|
+
'wow, such lamp, very fun, so commit',
|
28
|
+
'wow, such desk, very neat, so scare',
|
29
|
+
'such desk, so scare',
|
30
|
+
'wow, such lamp, so scare',
|
31
|
+
'wow, such desk, very neat, so commit',
|
32
|
+
'wow, such desk, very fun, so commit',
|
33
|
+
'such desk, very neat, so commit',
|
34
|
+
'wow, such desk, very fun, so scare',
|
35
|
+
'such lamp, very fun, so commit',
|
36
|
+
'wow, such desk, so commit',
|
37
|
+
'such lamp, so commit',
|
38
|
+
'such desk, so commit',
|
39
|
+
'such lamp, very fun, so scare',
|
40
|
+
'wow, such desk, so scare',
|
41
|
+
'wow, such lamp, so commit',
|
42
|
+
'such lamp, very neat, so scare',
|
43
|
+
'wow, such lamp, very fun, so scare',
|
44
|
+
'such lamp, so scare',
|
45
|
+
'wow, such lamp, very neat, so scare',
|
46
|
+
'such desk, very fun, so scare',
|
47
|
+
'such desk, very neat, so scare',
|
48
|
+
'wow, such lamp, very neat, so commit',
|
49
|
+
'such desk, very fun, so commit' ].must_include madlib.generate
|
46
50
|
end
|
47
51
|
end
|
48
|
-
|
49
|
-
it "should accurately extract (word|lists) with other templating entities around" do
|
50
|
-
keys = @mad.extract_word_lists('<simple> (some|words) (<keys>)? (to|extract)')
|
51
|
-
keys.size.must_equal 2
|
52
|
-
assert_block "correct keys extracted" do
|
53
|
-
keys.include? '(some|words)' and keys.include? '(to|extract)'
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should retrieve random dictionary items only once" do
|
58
|
-
retrieved = []
|
59
|
-
retrieved.push @mad.retrieve 'item'
|
60
|
-
retrieved.push @mad.retrieve 'item'
|
61
|
-
retrieved.push @mad.retrieve 'item'
|
62
|
-
|
63
|
-
retrieved.size.must_be_same_as retrieved.uniq.size
|
64
|
-
|
65
|
-
@mad.retrieve('item').must_be_nil
|
66
|
-
end
|
67
|
-
|
68
52
|
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'minitest/spec'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'madlibs'
|
4
|
+
|
5
|
+
describe Madlibs::Template do
|
6
|
+
describe '#keys' do
|
7
|
+
it 'should extract simple keys' do
|
8
|
+
keys = Madlibs::Template.keys '<simple> <keys>'
|
9
|
+
keys.size.must_equal 2
|
10
|
+
keys.must_include 'simple'
|
11
|
+
keys.must_include 'keys'
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should extract keys from more complex templates' do
|
15
|
+
keys = Madlibs::Template.keys '<simple> (some|words) (<keys>)?'
|
16
|
+
keys.size.must_equal 2
|
17
|
+
keys.must_include 'simple'
|
18
|
+
keys.must_include 'keys'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#process_key' do
|
23
|
+
it 'should put a value in place of a key' do
|
24
|
+
result = Madlibs::Template.process_key '<simple> <keys>', 'simple', 'hello'
|
25
|
+
result.must_equal 'hello <keys>'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#process_word_lists' do
|
30
|
+
it 'should choose random words from word lists included in a template' do
|
31
|
+
result = Madlibs::Template.process_word_lists '(simple) (simple|word|lists)'
|
32
|
+
[ 'simple simple',
|
33
|
+
'simple word',
|
34
|
+
'simple lists' ].must_include result
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should choose random words from word lists included in a complex template' do
|
38
|
+
result = Madlibs::Template.process_word_lists '<simple> (some|words) (<keys>)? (to|extract)'
|
39
|
+
[ '<simple> some (<keys>)? to',
|
40
|
+
'<simple> some (<keys>)? extract',
|
41
|
+
'<simple> words (<keys>)? to',
|
42
|
+
'<simple> words (<keys>)? extract' ].must_include result
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#process_optionals' do
|
47
|
+
it 'should choose to randomly include optional elements' do
|
48
|
+
result = Madlibs::Template.process_optionals 'this is( really)?( very)? silly'
|
49
|
+
[ 'this is really very silly',
|
50
|
+
'this is very silly',
|
51
|
+
'this is really silly',
|
52
|
+
'this is silly' ].must_include result
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should choose to randomly include optional elements in complex templates' do
|
56
|
+
result = Madlibs::Template.process_optionals '<simple> (some|words)( <keys>)? (to|extract)'
|
57
|
+
[ '<simple> (some|words) <keys> (to|extract)',
|
58
|
+
'<simple> (some|words) (to|extract)' ].must_include result
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should support the nesting of optionals' do
|
62
|
+
result = Madlibs::Template.process_optionals 'this is( very( freaking)?)? cool'
|
63
|
+
[ 'this is cool',
|
64
|
+
'this is very cool',
|
65
|
+
'this is very freaking cool' ].must_include result
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: madlibs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan D. Johnson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -65,11 +65,15 @@ files:
|
|
65
65
|
- README.md
|
66
66
|
- Rakefile
|
67
67
|
- lib/madlibs.rb
|
68
|
+
- lib/madlibs/dictionary.rb
|
68
69
|
- lib/madlibs/madlib.rb
|
70
|
+
- lib/madlibs/template.rb
|
69
71
|
- lib/madlibs/version.rb
|
70
72
|
- madlibs.gemspec
|
73
|
+
- specs/dictionary_spec.rb
|
71
74
|
- specs/madlib_spec.rb
|
72
|
-
|
75
|
+
- specs/template_spec.rb
|
76
|
+
homepage: https://github.com/jondavidjohn/madlibs
|
73
77
|
licenses:
|
74
78
|
- Apache v2
|
75
79
|
metadata: {}
|