malvestuto_random_data 1.5.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.
@@ -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
@@ -0,0 +1,73 @@
1
+ module RandomData
2
+
3
+ # Methods to create realistic-looking names
4
+ module Names
5
+
6
+ # Returns a random letter
7
+
8
+ def initial
9
+ ('A'..'Z').to_a.rand
10
+ end
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
16
+ WILLIAMS WILSON YORKE)
17
+
18
+ # Returns a random lastname
19
+ #
20
+ # >> Random.lastname
21
+ #
22
+ # "Harris"
23
+
24
+ def lastname
25
+ @@lastnames.rand.capitalize
26
+ end
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
30
+ MICHAEL PAUL PHILIP RICHARD ROBERT ROGER RONALD SIMON STEVEN TERRY THOMAS WILLIAM)
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
35
+ SARAH SHARON SUSAN TABITHA URSULA VICTORIA WENDY)
36
+
37
+ @@first_names = @@male_first_names + @@female_first_names
38
+
39
+
40
+ # Returns a random firstname
41
+ #
42
+ # >> Random.firstname
43
+ #
44
+ # "Sandra"
45
+
46
+ def firstname
47
+ @@first_names.rand.capitalize
48
+ end
49
+
50
+
51
+ # Returns a random male firstname
52
+ #
53
+ # >> Random.firstname_male
54
+ #
55
+ # "James"
56
+
57
+ def firstname_male
58
+ @@male_first_names.rand.capitalize
59
+ end
60
+
61
+
62
+ # Returns a random female firstname
63
+ #
64
+ # >> Random.firstname_female
65
+ #
66
+ # "Mary"
67
+
68
+ def firstname_female
69
+ @@female_first_names.rand.capitalize
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,31 @@
1
+ module RandomData
2
+ module Numbers
3
+ #n can be an Integer or a Range. If it is an Integer, it just returns a random
4
+ #number greater than or equal to 0 and less than n. If it is a Range, it
5
+ #returns a random number within the range
6
+ # Examples
7
+ #
8
+ # >> Random.number(5)
9
+ # => 4
10
+ # >> Random.number(5)
11
+ # => 2
12
+ # >> Random.number(5)
13
+ # => 1
14
+ def number(n)
15
+ n.is_a?(Range) ? n.to_a.rand : rand(n)
16
+ end
17
+
18
+ # return a random bit, 0 or 1.
19
+ def bit
20
+ rand(2)
21
+ end
22
+
23
+ # return an array of n random bits.
24
+ def bits(n)
25
+ x = []
26
+ n.times {x << bit}
27
+ x
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,49 @@
1
+ module RandomData
2
+
3
+ # Method to create Brazilian CPF number
4
+ module Papers
5
+ def cpf
6
+ numbers = Array.new(9) {rand(10)}
7
+ 2.times do
8
+ start = 2
9
+ digit = 0
10
+ for i in start..(numbers.size+1) do
11
+ digit += (numbers.reverse[i-start] * i)
12
+ end
13
+
14
+ digit = 11 - (digit % 11)
15
+ digit = 0 if digit >= 10
16
+
17
+ start += 1
18
+ numbers << digit
19
+ end
20
+ numbers.join
21
+ end
22
+
23
+ def cnpj
24
+ n = Array.new(8) {rand(10)} + [0, 0, 0, 1]
25
+ d1 = n[11]*2+n[10]*3+n[9]*4+n[8]*5+n[7]*6+n[6]*7+n[5]*8+n[4]*9+n[3]*2+n[2]*3+n[1]*4+n[0]*5
26
+ d1 = 11 - (d1 % 11)
27
+ d1 = 0 if d1 >= 10
28
+
29
+ n << d1
30
+
31
+ d2 = n[12]*2+n[11]*3+n[10]*4+n[9]*5+n[8]*6+n[7]*7+n[6]*8+n[5]*9+n[4]*2+n[3]*3+n[2]*4+n[1]*5+n[0]*6
32
+ d2 = 11 - (d2 % 11)
33
+ d2 = 0 if d2 >= 10
34
+
35
+ n << d2
36
+ n.join
37
+ end
38
+
39
+ def formated_cnpj
40
+ cnpj = cnpj()
41
+ "#{cnpj[0..1]}.#{cnpj[2..4]}.#{cnpj[5..7]}/#{cnpj[8..11]}-#{cnpj[12...14]}"
42
+ end
43
+
44
+ def formated_cpf
45
+ cpf = cpf()
46
+ "#{cpf[0..2]}.#{cpf[3..5]}.#{cpf[6..8]}-#{cpf[9..10]}"
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,70 @@
1
+ module RandomData
2
+
3
+ module Text
4
+
5
+ # Methods to create random strings and paragraphs.
6
+
7
+ # Returns a string of random upper- and lowercase alphanumeric characters. Accepts a size parameters, defaults to 16 characters.
8
+ #
9
+ # >> Random.alphanumeric
10
+ #
11
+ # "Ke2jdknPYAI8uCXj"
12
+ #
13
+ # >> Random.alphanumeric(5)
14
+ #
15
+ # "7sj7i"
16
+
17
+ def alphanumeric(size=16)
18
+ s = ""
19
+ size.times { s << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }
20
+ s
21
+ end
22
+
23
+ # TODO make these more coherent #:nodoc:
24
+
25
+ @@sentences = [
26
+ "Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua",
27
+ "Ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat",
28
+ "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur",
29
+ "Excepteur sint occaecat cupidatat non proident sunt in culpa qui officia deserunt mollit anim id est laborum",
30
+ "There's a voice that keeps on calling me",
31
+ "Down the road that's where I'll always be",
32
+ "Every stop I make I make a new friend; Can't stay for long just turn around and I'm gone again",
33
+ "Maybe tomorrow I'll want to settle down - until tomorrow I'll just keep moving on",
34
+ "Hong Kong Phooey number one super guy, Hong Kong Phooey quicker than the human eye",
35
+ "He's got style, a groovy style, and a car that just won't stop",
36
+ "Hey there where ya goin, not exactly knowin'",
37
+ "Who says you have to call just one place home?",
38
+ "He's going everywhere, B.J. McKay and his best friend Bear",
39
+ "He just keeps on movin' and ladies keep improvin'",
40
+ "Every day is better than the last, with new dreams and better scenes and best of all I don't pay property tax",
41
+ "Rolling down to Dallas - who is providin' my palace?",
42
+ "Off to New Orleans or who knows where",
43
+ "Soaring through all the galaxies in search of Earth flying in to the night",
44
+ "Ulysses, fighting evil and tyranny with all his power and with all of his might",
45
+ "No-one else can do the things you do, like a bolt of thunder from the blue",
46
+ "Always fighting all the evil forces bringing peace and justice to all",
47
+ "I've gotten burned over Cheryl Tiegs and blown up for Raquel Welch, but when I end up in the hay it's only hay, hey hey",
48
+ "I might jump an open drawbridge or Tarzan from a vine, beause I'm the unknown stuntman that makes Eastwood look so fine"]
49
+
50
+ # Returns a given number of paragraphs delimited by two newlines (defaults to two paragraphs), using a small pool of generic sentences.
51
+ # >> Random.paragraphs
52
+ #
53
+ # "I might jump an open drawbridge or Tarzan from a vine, beause I'm the unknown stuntman that makes Eastwood look so fine.\n\n \Always fighting all the evil forces bringing peace and justice to all. \n\n"
54
+
55
+ def paragraphs(num = 2)
56
+ text = ''
57
+
58
+ num.times do
59
+ (rand(5)+1).times do
60
+ text += @@sentences.rand + '. '
61
+ end
62
+ text += "\n\n"
63
+ end
64
+
65
+ return text
66
+
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module RandomData #:nodoc:
2
+ VERSION = '1.5.0' #:nodoc:
3
+ end
@@ -0,0 +1,65 @@
1
+ dir = "#{File.dirname(__FILE__)}/random_data"
2
+
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}/papers"
12
+ require "#{dir}/markov"
13
+ require "#{dir}/grammar"
14
+ require "#{dir}/version"
15
+
16
+ class Random
17
+ extend RandomData::Booleans
18
+ extend RandomData::ContactInfo
19
+ extend RandomData::Dates
20
+ extend RandomData::Grammar
21
+ extend RandomData::Locations
22
+ extend RandomData::Names
23
+ extend RandomData::Numbers
24
+ extend RandomData::Text
25
+ extend RandomData::Papers
26
+
27
+ # Looks for a file in the load path with the name methodname.dat, reads the lines from that file, then gives you a random line from that file.
28
+ # Raises an error if it can't find the file. For example, given a file named "horse.dat" in your load path:
29
+ # >> Random.horse
30
+ # => "Stallion"
31
+ # >> Random.horse
32
+ # => "Pony"
33
+ # >> Random.horse
34
+ # => "Mare"
35
+ # >> Random.horse
36
+ # => "Clydesdale"
37
+ # >> Random.horse
38
+ # => "Stallion"
39
+ # >> Random.horse
40
+ # => "Mare"
41
+
42
+ def self.method_missing(methodname)
43
+ thing = "#{methodname}.dat"
44
+ filename = find_path(thing)
45
+
46
+ if filename.nil?
47
+ super
48
+ else
49
+ array = []
50
+ File.open(filename, 'r') { |f| array = f.read.split(/[\r\n]+/) }
51
+ return array.rand
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def self.find_path(filename)
58
+ $:.each do |path|
59
+ new_path = File.join(path,filename)
60
+ return new_path if File.exist?(new_path)
61
+ end
62
+ return nil
63
+ end
64
+
65
+ 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 ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
data/script/txt2html ADDED
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ load File.dirname(__FILE__) + "/../Rakefile"
4
+ require 'rubyforge'
5
+ require 'redcloth'
6
+ require 'syntax/convertors/html'
7
+ require 'erb'
8
+
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
15
+
16
+ class Fixnum
17
+ def ordinal
18
+ # teens
19
+ return 'th' if (10..19).include?(self % 100)
20
+ # others
21
+ case self % 10
22
+ when 1: return 'st'
23
+ when 2: return 'nd'
24
+ when 3: return 'rd'
25
+ else return 'th'
26
+ end
27
+ end
28
+ end
29
+
30
+ class Time
31
+ def pretty
32
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
33
+ end
34
+ end
35
+
36
+ def convert_syntax(syntax, source)
37
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
38
+ end
39
+
40
+ if ARGV.length >= 1
41
+ src, template = ARGV
42
+ template ||= File.join(File.dirname(__FILE__), '/../website/template.html.erb')
43
+ else
44
+ puts("Usage: #{File.split($0).last} source.txt [template.html.erb] > output.html")
45
+ exit!
46
+ end
47
+
48
+ template = ERB.new(File.open(template).read)
49
+
50
+ title = nil
51
+ body = nil
52
+ File.open(src) do |fsrc|
53
+ title_text = fsrc.readline
54
+ body_text_template = fsrc.read
55
+ body_text = ERB.new(body_text_template).result(binding)
56
+ syntax_items = []
57
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
58
+ ident = syntax_items.length
59
+ element, syntax, source = $1, $2, $3
60
+ syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
61
+ "syntax-temp-#{ident}"
62
+ }
63
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
64
+ body = RedCloth.new(body_text).to_html
65
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
66
+ end
67
+ stat = File.stat(src)
68
+ created = stat.ctime
69
+ modified = stat.mtime
70
+
71
+ $stdout << template.result(binding)
@@ -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
+
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require 'mocha'
3
+ require File.dirname(__FILE__) + '/../lib/random_data'