word-salad 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,69 @@
1
+ = Word Salad
2
+
3
+ == DESCRIPTION:
4
+
5
+ Word Salad is a very simple Ruby library for generating random strings
6
+ of English words based on the Unix dictionary file. The gem does its
7
+ best to figure out where your particular dictionary file is, but on
8
+ some systems may need a little help.
9
+
10
+ == SYNOPSIS:
11
+
12
+ Generating random data is helpful for all kinds of testing. You could
13
+ just generate random gobble-dee-gook, but why not generate something
14
+ that looks a bit English-like? If for no other reason, you'll find great
15
+ enjoyment in all of the potential band-names that this gem generates.
16
+
17
+ WordSalad adds three methods to the <tt>Fixnum</tt> class to do things
18
+ like this:
19
+
20
+ require 'rubygems'
21
+ require 'word_salad'
22
+
23
+ 3.words ==> ['draw', 'ameliorate', 'bonanza']
24
+ 2.sentences ==> ['Shoot jonesing the make castle.', 'Blue murdered slight bastion.']
25
+ 2.paragraphs ==> ["Brachypterous gastropod pheretrer overeager toploftily denaturalization stokesite demented benzalhydrazine archaeologic. Haverer hypophonous lenticularly brickliner urocele paucipinnate pik unprintably perhalogen. Subglenoid bearish gesticulative staircase gallop vesuvianite pneumatically overyear conterminous dreamish. Nonalliterated galliwasp superconfirmation Comandra entoil millionth parcellize rarefaction Cynoidea. Podolian metamorphosable nativeness integriously protonematoid undoctor stochastically dissatisfactory unchastity.", "Increate unloquacious unsatisfiedly flareboard internuncio beguine equivocation snowshoe Rhynchonellacea. Parochially curliewurly vermix consistorial cond consciencelessness Anaxagorize recoct sempiternally Campanulatae. Scorpionida Castalides homoanisic semipenniform Novemberish assessor preterlethal acrotarsial knoller hartin. Procrastination boatwise canonize differentiate faunlike countermarriage obstinance dilatableness drumloid. Gerate squirr Silvanus Physostigma booting thyroarytenoid diminutival legpuller medisance radiobserver."]
26
+
27
+ == CUSTOMIZATION
28
+
29
+ WordSalad relies on the presence of a valid Unix dictionary file. It
30
+ will do it's best to figure out where one is, but doesn't account for
31
+ every platform. If you are one of those special people that has a
32
+ dictionary file in a place WordSalad can't find, you can tell it by
33
+ calling <tt>dictionary_path</tt> attribute setter method.
34
+
35
+ require 'rubygems'
36
+ require 'word_salad'
37
+
38
+ WordSalad.dictionary_path = "/my/special/dictionary/path"
39
+
40
+ == INSTALL:
41
+
42
+ To install Word Salad, simply:
43
+
44
+ sudo gem install word_salad
45
+
46
+ == LICENSE:
47
+
48
+ (The MIT License)
49
+
50
+ Copyright (c) 2008 Alex Vollmer
51
+
52
+ Permission is hereby granted, free of charge, to any person obtaining
53
+ a copy of this software and associated documentation files (the
54
+ 'Software'), to deal in the Software without restriction, including
55
+ without limitation the rights to use, copy, modify, merge, publish,
56
+ distribute, sublicense, and/or sell copies of the Software, and to
57
+ permit persons to whom the Software is furnished to do so, subject to
58
+ the following conditions:
59
+
60
+ The above copyright notice and this permission notice shall be
61
+ included in all copies or substantial portions of the Software.
62
+
63
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
64
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
65
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
66
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
67
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
68
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
69
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,27 @@
1
+ require "word_salad/core_ext"
2
+
3
+ class WordSalad
4
+
5
+ # The current dictionary path
6
+ def self.dictionary_path
7
+ @dictionary_path ||= ["/usr/share/dict/words",
8
+ "/usr/share/words"].select do |f|
9
+ FileTest.exists?(f)
10
+ end.first
11
+ end
12
+
13
+ # Override the default dictionary path to something special
14
+ def self.dictionary_path=(path)
15
+ @dictionary_path = path
16
+ @size = nil
17
+ end
18
+
19
+ def self.dictionary # :nodoc:
20
+ open(self.dictionary_path)
21
+ end
22
+
23
+ def self.size # :nodoc:
24
+ @size ||= File.size(self.dictionary)
25
+ end
26
+
27
+ end
@@ -0,0 +1,42 @@
1
+ class Fixnum
2
+
3
+ # Returns +num+ random words from the dictionary.
4
+ def words
5
+ dict = WordSalad.dictionary
6
+ (1..self).to_a.map do |x|
7
+ dict.seek(rand(WordSalad.size - 1000))
8
+ b = dict.readchar
9
+ while b != 10
10
+ b = dict.readchar
11
+ end
12
+
13
+ dict.readline.strip
14
+ end
15
+ end
16
+
17
+ alias :word :words
18
+
19
+ # Returns +num+ sentences of random words around +size+
20
+ # number of words.
21
+ def sentences(size=10)
22
+ variance = size / 5
23
+ (1..self).to_a.map do |x|
24
+ w = (size + (rand(variance) - variance / 2)).words
25
+ w[0].capitalize!
26
+ w.join(' ') + '.'
27
+ end
28
+ end
29
+
30
+ alias :sentence :sentences
31
+
32
+ # Returns +num+ paragraphs of around +psize+ sentences,
33
+ # each around +ssize+ number of words
34
+ def paragraphs(psize=5, ssize=10)
35
+ (1..self).to_a.map do |x|
36
+ psize.sentences.join(' ')
37
+ end
38
+ end
39
+
40
+ alias :paragraph :paragraphs
41
+ end
42
+
@@ -0,0 +1,98 @@
1
+ require "fileutils"
2
+ require "tempfile"
3
+ require "rubygems"
4
+ require "spec"
5
+
6
+ $:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
7
+ require "word_salad"
8
+
9
+ describe WordSalad do
10
+ describe "generating random words" do
11
+ it "should return an array of the right size" do
12
+ w = 5.words
13
+ w.should be_kind_of(Array)
14
+ w.size.should eql(5)
15
+ w.each do |word|
16
+ word.should be_kind_of(String)
17
+ end
18
+ end
19
+
20
+ it "should respond to the singular version too" do
21
+ w = 1.word
22
+ w.should be_kind_of(Array)
23
+ w.size.should == 1
24
+ w.first.should be_kind_of(String)
25
+ end
26
+ end
27
+
28
+ describe "generating random sentences" do
29
+ it "should return an array of sentences" do
30
+ s = 5.sentences
31
+ s.should be_kind_of(Array)
32
+ s.size.should == 5
33
+ s.each do |sent|
34
+ sent.should be_kind_of(String)
35
+ sent.should match(/^[A-Z].*\.$/)
36
+ sent.split(' ').size.should be_close(10, 2)
37
+ end
38
+ end
39
+
40
+ it "should respond to the singular version too" do
41
+ s = 1.sentence
42
+ s.should be_kind_of(Array)
43
+ s.size.should == 1
44
+ end
45
+ end
46
+
47
+ describe "generating random paragraphs" do
48
+ it "should return an array of paragraphs" do
49
+ p = 5.paragraphs
50
+ p.should be_kind_of(Array)
51
+ p.size.should == 5
52
+ p.each do |para|
53
+ para.should be_kind_of(String)
54
+ s = para.split('.')
55
+ s.size.should == 5
56
+ s.each do |sent|
57
+ sent.split(' ').size.should be_close(10, 2)
58
+ end
59
+ end
60
+ end
61
+
62
+ it "should respond to singular version too" do
63
+ p = 1.paragraph
64
+ p.should be_kind_of(Array)
65
+ p.size.should == 1
66
+ end
67
+ end
68
+
69
+ describe 'overriding the default dictionary path' do
70
+ before(:each) do
71
+ @tmpfile = Tempfile.new "word_salad"
72
+ 2000.times { @tmpfile << "alpha\n" }
73
+ @tmpfile.close
74
+ WordSalad.dictionary_path = @tmpfile.path
75
+ end
76
+
77
+ it 'should work for words' do
78
+ w = 5.words
79
+ w.each { |e| e.should == "alpha" }
80
+ end
81
+
82
+ it 'should work for sentences' do
83
+ s = 5.sentences
84
+ s.each do |sent|
85
+ sent.split(' ').each { |e| e.downcase.should match(/alpha/) }
86
+ end
87
+ end
88
+
89
+ it 'should work for paragraphs' do
90
+ p = 5.paragraphs
91
+ p.each do |para|
92
+ para.split('.').each do |sent|
93
+ sent.split(' ').each { |e| e.downcase.should match(/alpha/) }
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: word-salad
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex Vollmer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-07 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Word Salad is a very simple Ruby library for generating random strings of English words based on the Unix dictionary file.
26
+ email: alex.vollmer@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.txt
33
+ files:
34
+ - lib/word_salad.rb
35
+ - lib/word_salad/core_ext.rb
36
+ - spec/word_salad_spec.rb
37
+ - README.txt
38
+ has_rdoc: true
39
+ homepage: http://github.com/alexvollmer/word_salad
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.5
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Generate strings of random English text
66
+ test_files:
67
+ - spec/word_salad_spec.rb