lorem-ipsum 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/bin/lorem-ipsum +16 -15
  2. data/lib/lorem-ipsum.rb +35 -19
  3. metadata +5 -7
@@ -7,7 +7,7 @@ require 'lorem-ipsum'
7
7
  require 'optparse'
8
8
 
9
9
  init_options = { :max_ngraph => 3 }
10
- gen_options = { :words => 100 }
10
+ gen_options = { :words => 100 , :paras => 1, :variance => 0 }
11
11
 
12
12
  opts = OptionParser.new do |opts|
13
13
  opts.banner = "Usage: #{$0} [options] data-files"
@@ -24,16 +24,26 @@ opts = OptionParser.new do |opts|
24
24
  int_val
25
25
  end
26
26
 
27
- opts.on("--max-ngraph MAX",
28
- "Maximum length of n-graphs to track. Default is 3") do |v|
27
+ opts.on("--max-ngraph MAX=3",
28
+ "Max length of n-graphs.") do |v|
29
29
  init_options[:max_ngraph] = check_int_arg(v, :min => 1)
30
30
  end
31
31
 
32
- opts.on("-w", "--words WORDS",
33
- "Number of words to generate. Default is 100") do |v|
32
+ opts.on("-w", "--words WORDS=100",
33
+ "Number of words to generate.") do |v|
34
34
  gen_options[:words] = check_int_arg(v, :min => 1)
35
35
  end
36
36
 
37
+ opts.on("-p", "--paragraphs PARAS=1",
38
+ "Number of paragraphs to generate.") do |v|
39
+ gen_options[:paras] = check_int_arg(v, :min=>1)
40
+ end
41
+
42
+ opts.on("-v", "--variance VAR=0",
43
+ "Paragraphs should be within this percent of word count") do |v|
44
+ gen_options[:variance] = check_int_arg(v, :min=>0, :max=>100)
45
+ end
46
+
37
47
  end
38
48
 
39
49
  begin
@@ -42,16 +52,7 @@ rescue
42
52
  opts.abort
43
53
  end
44
54
 
45
- # There has to be a more Ruby way to do this...
46
- if ARGV.empty?
47
- files = Dir.entries($DATA_DIR).reject do |f|
48
- f =~ /^\.\.?$/
49
- end.collect do |f|
50
- "#{$DATA_DIR}/#{f}"
51
- end
52
- else
53
- files = ARGV
54
- end
55
+ files = ARGV.empty? ? Dir.glob($DATA_DIR + "/*") : ARGV
55
56
 
56
57
  gen = LoremIpsum::Generator.new(files, init_options)
57
58
  puts gen.generate(gen_options).strip
@@ -35,27 +35,43 @@ class Generator
35
35
  end
36
36
  end
37
37
 
38
- def generate(options = { :words => 100 })
38
+ def generate(options)
39
+ options[:paras] ||= 1
40
+ options[:variance] ||= 0
41
+ options[:words] ||= 100
42
+
39
43
  str = ""
40
- if options[:words]
41
- to_next_sentence = rand(10) + 5
42
- start_of_sentence = true
43
- 1.upto(options[:words]) do |i|
44
- word = next_word
45
- if start_of_sentence
46
- word.capitalize!
47
- start_of_sentence = false
48
- end
44
+ 1.upto(options[:paras]) do |i|
45
+ str << next_paragraph(options[:words], options[:variance]) << "\n"
46
+ end
49
47
 
50
- to_next_sentence -= 1
51
- if to_next_sentence == 0 || i == options[:words]
52
- word.gsub!(/ /,'. ')
53
- to_next_sentence = rand(10) + 5
54
- start_of_sentence = true
55
- end
56
-
57
- str << word
58
- end
48
+ str
49
+ end
50
+
51
+ def next_paragraph(word_count, variance = 0)
52
+ str = ""
53
+
54
+ if variance != 0
55
+ range = (2 * variance * word_count / 100.0).to_i
56
+ word_count = word_count + rand(range) - range / 2
57
+ end
58
+
59
+ while word_count > 0
60
+ sentence_count = [rand(10) + 5, word_count].min
61
+ str << next_sentence(sentence_count)
62
+ word_count -= sentence_count
63
+ end
64
+
65
+ str << "\n"
66
+ end
67
+
68
+ def next_sentence(word_count)
69
+ str = ""
70
+ 1.upto(word_count) do |i|
71
+ word = next_word
72
+ word.capitalize! if i == 1
73
+ word.gsub!(/ /,'. ') if i == word_count
74
+ str << word
59
75
  end
60
76
 
61
77
  str
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lorem-ipsum
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matt Austin
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-24 00:00:00 -04:00
19
- default_executable:
18
+ date: 2011-06-26 00:00:00 Z
20
19
  dependencies: []
21
20
 
22
21
  description: lorem-ipsum is a simple, trainable dummy text generator.
@@ -31,7 +30,6 @@ files:
31
30
  - lib/lorem-ipsum.rb
32
31
  - data/lorem.txt
33
32
  - bin/lorem-ipsum
34
- has_rdoc: true
35
33
  homepage: http://github.com/maafy6/lorem-ipsum
36
34
  licenses:
37
35
  - MIT
@@ -61,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
59
  requirements: []
62
60
 
63
61
  rubyforge_project:
64
- rubygems_version: 1.6.2
62
+ rubygems_version: 1.7.2
65
63
  signing_key:
66
64
  specification_version: 3
67
65
  summary: A simple dummy text generator.