random_data 1.3.1 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,4 +1,15 @@
1
- == 1.3.1 2008-06--06
1
+ == 1.5.0 2008-11-06
2
+ * 2 major enhancement:
3
+ * Totally cleaned up and reconfigured hoe using newgem 1.0's conventions
4
+ * Added Markov model text generator (courtesy of Hugh Sasse)
5
+
6
+ * 1 minor enhancement:
7
+ * Added roulette method to Array (courtesy of Hugh Sasse)
8
+
9
+ * 1 minor fix:
10
+ * Fixed newline bug in the name generator (courtesy of Harold Gimenez)
11
+
12
+ == 1.3.1 2008-06-06
2
13
 
3
14
  * 2 minor enhancements:
4
15
  * Added added more last and firstnames (courtesy of Hugh Sasse)
data/Manifest.txt CHANGED
@@ -1,10 +1,8 @@
1
1
  History.txt
2
2
  License.txt
3
3
  Manifest.txt
4
- README.txt
4
+ README.rdoc
5
5
  Rakefile
6
- config/hoe.rb
7
- config/requirements.rb
8
6
  lib/random_data.rb
9
7
  lib/random_data/array_randomizer.rb
10
8
  lib/random_data/booleans.rb
@@ -12,22 +10,15 @@ lib/random_data/contact_info.rb
12
10
  lib/random_data/dates.rb
13
11
  lib/random_data/grammar.rb
14
12
  lib/random_data/locations.rb
13
+ lib/random_data/markov.rb
15
14
  lib/random_data/names.rb
16
15
  lib/random_data/numbers.rb
17
16
  lib/random_data/text.rb
18
17
  lib/random_data/version.rb
19
- log/debug.log
18
+ script/console
20
19
  script/destroy
21
20
  script/generate
22
21
  script/txt2html
23
- setup.rb
24
- tasks/deployment.rake
25
- tasks/environment.rake
26
- tasks/website.rake
22
+ test/henry_v_prolog.txt
27
23
  test/test_helper.rb
28
24
  test/test_random_data.rb
29
- website/index.html
30
- website/index.txt
31
- website/javascripts/rounded_corners_lite.inc.js
32
- website/stylesheets/screen.css
33
- website/template.rhtml
@@ -1,4 +1,4 @@
1
- = Random
1
+ = random_data
2
2
 
3
3
  This gem provides a Random singleton class with a series of methods for generating random test data including names, mailing addresses, dates, phone numbers, e-mail addresses, and text. Instead of:
4
4
 
@@ -26,7 +26,7 @@ Random.date, Random.date_between
26
26
  Random.address_line_1, Random.address_line_2, Random.zipcode, Random.uk_post_code, Random.state, Random.state_full, Random.country, Random.city
27
27
 
28
28
  === Name Methods
29
- Random.firstname, Random.initial, Random.lastname
29
+ Random.firstname, Random.firstname_male, Random.firstname_female, Random.initial, Random.lastname
30
30
 
31
31
  === Text Methods
32
32
  Random.alphanumeric, Random.paragraphs
@@ -40,8 +40,14 @@ Random.number, Random.bit, Random.bits
40
40
  === Boolean Methods
41
41
  Random.boolean
42
42
 
43
+ === Markov Generator Methods
44
+ Random.MarkovGenerator.new
45
+ Random.MarkovGenerator.insert
46
+ Random.MarkovGenerator.generate
47
+
43
48
  === Array Extension
44
49
  Array.rand
50
+ Array.roulette
45
51
 
46
52
  === Choose From File Methods
47
53
  Random implements a method_missing helper that will choose a line at random from a file in your load path. See Random::method_missing for details.
data/Rakefile CHANGED
@@ -1,4 +1,32 @@
1
- require 'config/requirements'
2
- require 'config/hoe' # setup Hoe + all gem configuration
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/random_data'
3
3
 
4
- Dir['tasks/**/*.rake'].each { |rake| load rake }
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('random_data', RandomData::VERSION) do |p|
7
+ p.developer('Mike Subelsky', 'mike@subelsky.com')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.rubyforge_name = "random-data"
10
+ p.description = "A Ruby gem that provides a Random class with a series of methods for generating random test data including names, mailing addresses, dates, phone numbers, e-mail addresses, and text."
11
+ p.summary = "A Ruby gem that provides a Random class with a series of methods for generating random test data including names, mailing addresses, dates, phone numbers, e-mail addresses, and text."
12
+ p.email = 'random_data@mikeshop.net'
13
+ # p.remote_rdoc_dir = 'random-data'
14
+
15
+ # p.extra_deps = [
16
+ # ['activesupport','>= 2.0.2'],
17
+ # ]
18
+ p.extra_dev_deps = [
19
+ ['newgem', ">= #{::Newgem::VERSION}"]
20
+ ]
21
+
22
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
23
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
24
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
25
+ p.rsync_args = '-av --delete --ignore-errors'
26
+ end
27
+
28
+ require 'newgem/tasks' # load /tasks/*.rake
29
+ Dir['tasks/**/*.rake'].each { |t| load t }
30
+
31
+ # TODO - want other tests/tasks run by default? Add them to the list
32
+ # task :default => [:spec, :features]
data/lib/random_data.rb CHANGED
@@ -1,14 +1,16 @@
1
- $:.unshift File.dirname(__FILE__)
1
+ dir = "#{File.dirname(__FILE__)}/random_data"
2
2
 
3
- require 'random_data/array_randomizer'
4
- require 'random_data/booleans'
5
- require 'random_data/contact_info'
6
- require 'random_data/dates'
7
- require 'random_data/locations'
8
- require 'random_data/names'
9
- require 'random_data/numbers'
10
- require 'random_data/text'
11
- require 'random_data/grammar'
3
+ require "#{dir}/array_randomizer"
4
+ require "#{dir}/booleans"
5
+ require "#{dir}/contact_info"
6
+ require "#{dir}/dates"
7
+ require "#{dir}/locations"
8
+ require "#{dir}/names"
9
+ require "#{dir}/numbers"
10
+ require "#{dir}/text"
11
+ require "#{dir}/markov"
12
+ require "#{dir}/grammar"
13
+ require "#{dir}/version"
12
14
 
13
15
  class Random
14
16
  extend RandomData::Booleans
@@ -12,6 +12,50 @@ module RandomData
12
12
  return self[Kernel.rand(self.size)]
13
13
  end
14
14
 
15
+ # Takes an array of non-negative weights
16
+ # and returns the index selected by a
17
+ # roulette wheel weighted according to those
18
+ # weights.
19
+ # If a block is given then k is used to determine
20
+ # how many times the block is called. In this
21
+ # case nil is returned.
22
+ def roulette(k=1)
23
+ wheel = []
24
+ weight = 0
25
+ # Create the cumulative array.
26
+ self.each do |x|
27
+ raise "Illegal negative weight #{x}" if x < 0
28
+ wheel.push(weight += x)
29
+ end
30
+ # print "wheel is #{wheel.inspect}\n";
31
+ # print "weight is #{weight.inspect}\n";
32
+ raise "Array had all zero weights" if weight.zero?
33
+ wheel.push(weight + 1) #Add extra element
34
+ if block_given?
35
+ k.times do
36
+ r = Kernel.rand() # so we don't pick up that from array.
37
+ # print "r is #{r.inspect}\n";
38
+ roll = weight.to_f * r
39
+ # print "roll is #{roll.inspect}\n";
40
+ 0.upto(self.size - 1) do |i|
41
+ if wheel[i+1] > roll
42
+ yield i
43
+ break
44
+ end # if
45
+ end # upto
46
+ end # if block_given?
47
+ return nil
48
+ else
49
+ r = Kernel.rand() # so we don't pick up that from array.
50
+ # print "r is #{r.inspect}\n";
51
+ roll = weight.to_f * r
52
+ # print "roll is #{roll.inspect}\n";
53
+ 0.upto(self.size - 1) do |i|
54
+ return i if wheel[i+1] > roll
55
+ end
56
+ end
57
+ end
58
+
15
59
  end
16
60
 
17
61
  end
@@ -0,0 +1,77 @@
1
+ # Methods to create a markov chain from some input text.
2
+
3
+
4
+ module RandomData
5
+ class MarkovGenerator
6
+
7
+ def initialize(memory = 1)
8
+ @memory_size = memory
9
+ @table = Hash.new {|h,k| h[k] = {}}
10
+ @state = []
11
+ end
12
+
13
+
14
+ # given the next token of input add it to the
15
+ # table
16
+ def insert(result)
17
+ # puts "insert called with #{result}"
18
+ tabindex = Marshal.dump(@state)
19
+ if @table[tabindex].has_key?(result)
20
+ @table[tabindex][result] += 1
21
+ else
22
+ @table[tabindex][result] = 1
23
+ end
24
+ # puts "table #{@table.inspect}"
25
+ next_state(result)
26
+ end
27
+
28
+ def next_state(result)
29
+ @state.shift if @state.size >= @memory_size
30
+ @state.push(result)
31
+ # puts "@state is #{@state.inspect}"
32
+ end
33
+
34
+ def generate(n=1, clear_state=false)
35
+ @state = [] if clear_state
36
+ results = []
37
+ n.times do
38
+ retry_count = 0
39
+ begin
40
+ result_hash = @table[Marshal.dump(@state)]
41
+ the_keys,the_vals = [],[]
42
+ result_hash.each_pair do |k,v|
43
+ the_keys << k
44
+ the_vals << v
45
+ end
46
+ # get the weighted random value, by index.
47
+ i = the_vals.roulette
48
+ rescue
49
+ # puts "results:#{result_hash.inspect}";
50
+ # puts "keys:#{the_keys.inspect}";
51
+ # puts "vals:#{the_vals.inspect}";
52
+ # puts "state:#{@state.inspect}";
53
+ @state = []
54
+ retry_count += 1
55
+ if retry_count < 5
56
+ retry
57
+ else
58
+ # puts
59
+ # puts "table:#{@table.inspect}";
60
+ raise
61
+ end
62
+ end
63
+ result = the_keys[i]
64
+ # puts "index:#{i.inspect}";
65
+
66
+ next_state(result)
67
+ if block_given?
68
+ yield result
69
+ end
70
+ results << result
71
+ end
72
+ return results
73
+ end
74
+
75
+ end
76
+
77
+ end
@@ -9,10 +9,10 @@ module RandomData
9
9
  ('A'..'Z').to_a.rand
10
10
  end
11
11
 
12
- @@lastnames = %w(ABEL ANDERSON ANDREWS ANTHONY BAKER BROWN BURROWS CLARK CLARKE CLARKSON DAVIDSON DAVIES DAVIS \
13
- DENT EDWARDS GARCIA GRANT HALL HARRIS HARRISON JACKSON JEFFRIES JEFFERSON JOHNSON JONES \
14
- KIRBY KIRK LAKE LEE LEWIS MARTIN MARTINEZ MAJOR MILLER MOORE OATES PETERS PETERSON ROBERTSON \
15
- ROBINSON RODRIGUEZ SMITH SMYTHE STEVENS TAYLOR THATCHER THOMAS THOMPSON WALKER WASHINGTON WHITE \
12
+ @@lastnames = %w(ABEL ANDERSON ANDREWS ANTHONY BAKER BROWN BURROWS CLARK CLARKE CLARKSON DAVIDSON DAVIES DAVIS
13
+ DENT EDWARDS GARCIA GRANT HALL HARRIS HARRISON JACKSON JEFFRIES JEFFERSON JOHNSON JONES
14
+ KIRBY KIRK LAKE LEE LEWIS MARTIN MARTINEZ MAJOR MILLER MOORE OATES PETERS PETERSON ROBERTSON
15
+ ROBINSON RODRIGUEZ SMITH SMYTHE STEVENS TAYLOR THATCHER THOMAS THOMPSON WALKER WASHINGTON WHITE
16
16
  WILLIAMS WILSON YORKE)
17
17
 
18
18
  # Returns a random lastname
@@ -25,13 +25,13 @@ module RandomData
25
25
  @@lastnames.rand.capitalize
26
26
  end
27
27
 
28
- @@male_first_names = %w(ADAM ANTHONY ARTHUR BRIAN CHARLES CHRISTOPHER DANIEL DAVID DONALD EDGAR EDWARD EDWIN \
29
- GEORGE HAROLD HERBERT HUGH JAMES JASON JOHN JOSEPH KENNETH KEVIN MARCUS MARK MATTHEW \
28
+ @@male_first_names = %w(ADAM ANTHONY ARTHUR BRIAN CHARLES CHRISTOPHER DANIEL DAVID DONALD EDGAR EDWARD EDWIN
29
+ GEORGE HAROLD HERBERT HUGH JAMES JASON JOHN JOSEPH KENNETH KEVIN MARCUS MARK MATTHEW
30
30
  MICHAEL PAUL PHILIP RICHARD ROBERT ROGER RONALD SIMON STEVEN TERRY THOMAS WILLIAM)
31
31
 
32
- @@female_first_names = %w(ALISON ANN ANNA ANNE BARBARA BETTY BERYL CAROL CHARLOTTE CHERYL DEBORAH DIANA DONNA \
33
- DOROTHY ELIZABETH EVE FELICITY FIONA HELEN HELENA JENNIFER JESSICA JUDITH KAREN KIMBERLY \
34
- LAURA LINDA LISA LUCY MARGARET MARIA MARY MICHELLE NANCY PATRICIA POLLY ROBYN RUTH SANDRA \
32
+ @@female_first_names = %w(ALISON ANN ANNA ANNE BARBARA BETTY BERYL CAROL CHARLOTTE CHERYL DEBORAH DIANA DONNA
33
+ DOROTHY ELIZABETH EVE FELICITY FIONA HELEN HELENA JENNIFER JESSICA JUDITH KAREN KIMBERLY
34
+ LAURA LINDA LISA LUCY MARGARET MARIA MARY MICHELLE NANCY PATRICIA POLLY ROBYN RUTH SANDRA
35
35
  SARAH SHARON SUSAN TABITHA URSULA VICTORIA WENDY)
36
36
 
37
37
  @@first_names = @@male_first_names + @@female_first_names
@@ -1,9 +1,3 @@
1
1
  module RandomData #:nodoc:
2
- module VERSION #:nodoc:
3
- MAJOR = 1
4
- MINOR = 3
5
- TINY = 1
6
-
7
- STRING = [MAJOR, MINOR, TINY].join('.')
8
- end
2
+ VERSION = '1.5.0' #:nodoc:
9
3
  end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/random_data.rb'}"
9
+ puts "Loading random_data gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- APP_ROOT = File.join(File.dirname(__FILE__), '..')
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
3
 
4
4
  begin
5
5
  require 'rubigen'
@@ -10,5 +10,5 @@ end
10
10
  require 'rubigen/scripts/destroy'
11
11
 
12
12
  ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
- RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme]
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
14
  RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- APP_ROOT = File.join(File.dirname(__FILE__), '..')
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
3
 
4
4
  begin
5
5
  require 'rubigen'
@@ -10,5 +10,5 @@ end
10
10
  require 'rubigen/scripts/generate'
11
11
 
12
12
  ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
- RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme]
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
14
  RubiGen::Scripts::Generate.new.run(ARGV)
data/script/txt2html CHANGED
@@ -1,20 +1,17 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'rubygems'
4
- begin
5
- require 'newgem'
6
- rescue LoadError
7
- puts "\n\nGenerating the website requires the newgem RubyGem"
8
- puts "Install: gem install newgem\n\n"
9
- exit(1)
10
- end
3
+ load File.dirname(__FILE__) + "/../Rakefile"
4
+ require 'rubyforge'
11
5
  require 'redcloth'
12
6
  require 'syntax/convertors/html'
13
7
  require 'erb'
14
- require File.dirname(__FILE__) + '/../lib/random_data/version.rb'
15
8
 
16
- version = RandomData::VERSION::STRING
17
- download = 'http://rubyforge.org/projects/random_data'
9
+ download = "http://rubyforge.org/projects/#{$hoe.rubyforge_name}"
10
+ version = $hoe.version
11
+
12
+ def rubyforge_project_id
13
+ RubyForge.new.configure.autoconfig["group_ids"][$hoe.rubyforge_name]
14
+ end
18
15
 
19
16
  class Fixnum
20
17
  def ordinal
@@ -42,10 +39,9 @@ end
42
39
 
43
40
  if ARGV.length >= 1
44
41
  src, template = ARGV
45
- template ||= File.join(File.dirname(__FILE__), '/../website/template.rhtml')
46
-
42
+ template ||= File.join(File.dirname(__FILE__), '/../website/template.html.erb')
47
43
  else
48
- puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
44
+ puts("Usage: #{File.split($0).last} source.txt [template.html.erb] > output.html")
49
45
  exit!
50
46
  end
51
47
 
@@ -55,7 +51,8 @@ title = nil
55
51
  body = nil
56
52
  File.open(src) do |fsrc|
57
53
  title_text = fsrc.readline
58
- body_text = fsrc.read
54
+ body_text_template = fsrc.read
55
+ body_text = ERB.new(body_text_template).result(binding)
59
56
  syntax_items = []
60
57
  body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
61
58
  ident = syntax_items.length
@@ -0,0 +1,41 @@
1
+ PROLOGUE
2
+
3
+ Enter CHORUS
4
+
5
+ CHORUS.
6
+ O for a Muse of fire, that would ascend
7
+ The brightest heaven of invention,
8
+ A kingdom for a stage, princes to act,
9
+ And monarchs to behold the swelling scene!
10
+ Then should the warlike Harry, like himself,
11
+ Assume the port of Mars; and at his heels,
12
+ Leash'd in like hounds, should famine, sword, and fire,
13
+ Crouch for employment. But pardon, gentles all,
14
+ The flat unraised spirits that hath dar'd
15
+ On this unworthy scaffold to bring forth
16
+ So great an object. Can this cockpit hold
17
+ The vasty fields of France? Or may we cram
18
+ Within this wooden O the very casques
19
+ That did affright the air at Agincourt?
20
+ O, pardon! since a crooked figure may
21
+ Attest in little place a million;
22
+ And let us, ciphers to this great accompt,
23
+ On your imaginary forces work.
24
+ Suppose within the girdle of these walls
25
+ Are now confin'd two mighty monarchies,
26
+ Whose high upreared and abutting fronts
27
+ The perilous narrow ocean parts asunder.
28
+ Piece out our imperfections with your thoughts:
29
+ Into a thousand parts divide one man,
30
+ And make imaginary puissance;
31
+ Think, when we talk of horses, that you see them
32
+ Printing their proud hoofs i' th' receiving earth;
33
+ For 'tis your thoughts that now must deck our kings,
34
+ Carry them here and there, jumping o'er times,
35
+ Turning th' accomplishment of many years
36
+ Into an hour-glass; for the which supply,
37
+ Admit me Chorus to this history;
38
+ Who prologue-like, your humble patience pray
39
+ Gently to hear, kindly to judge, our play.
40
+
41
+