Class: Markov
- Inherits:
-
Object
- Object
- Markov
- Defined in:
- lib/markov-reloaded.rb
Overview
Main class for generating Markov Chains
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Class Method Summary collapse
-
.analyze(array, order) ⇒ Object
Analyze array and create Markov object from it.
Instance Method Summary collapse
-
#+(mark) ⇒ Object
Add the data of another markov object to the current one.
-
#add(array) ⇒ Object
Analyze an array and add it to the current object’s hash.
-
#generate(maxlength, first = [], seed = (Time.now.nsec), terminator = ".") ⇒ Object
terminate(defaults to “.”).
-
#initialize(data, order) ⇒ Markov
constructor
data is the hash to generate by, order is the order of the hash.
-
#pickElement(element, random = (Random.new(Time.now.nsec))) ⇒ Object
Given a list of elements with length order(a key for the hash), pick the next element based on the random number generator.
Constructor Details
#initialize(data, order) ⇒ Markov
data is the hash to generate by, order is the order of the hash
56 57 58 59 |
# File 'lib/markov-reloaded.rb', line 56 def initialize (data, order) @data = data @order = order end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
60 61 62 |
# File 'lib/markov-reloaded.rb', line 60 def data @data end |
Class Method Details
.analyze(array, order) ⇒ Object
Analyze array and create Markov object from it
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/markov-reloaded.rb', line 66 def self.analyze(array, order) data = Hash.new(nil) for i in 0..array.length-1-order el = array[i..i+order-1] if data.include? el data[el].push array[i+order]# unless i+order+1 == array.length else data[el] = [array[i+order]]# unless i+order+1 == array.length end end Markov.new(data, order) end |
Instance Method Details
#+(mark) ⇒ Object
Add the data of another markov object to the current one
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/markov-reloaded.rb', line 79 def +(mark) hash = mark.data hash.each do |key, value| if @data.include? key @data[key] += value else @data[key] = value end end end |
#add(array) ⇒ Object
Analyze an array and add it to the current object’s hash
62 63 64 |
# File 'lib/markov-reloaded.rb', line 62 def add(array) self.+(array.toMarkov @order) end |
#generate(maxlength, first = [], seed = (Time.now.nsec), terminator = ".") ⇒ Object
terminate(defaults to “.”). Generates and outputs an array corresponding to a markov chain.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/markov-reloaded.rb', line 105 def generate(maxlength, first= [], seed=(Time.now.nsec), terminator = ".") r = Random.new seed if first.class == Method first = first.call(@data, r.rand(10000)) else first = @data.keys.pickRand(r) if first == [] || first == "" end out = first.dup pick = out.dup for i in 0..(maxlength-@order) element = pickElement(pick, r) if out.class == Array out.push element elsif out.class == String out += element end if pick.class == Array pick = pick.drop 1 elsif pick.class == String pick = pick[1..pick.length-1] end if pick.class == Array pick.push element elsif pick.class == String pick += element end break if element == terminator end return out end |
#pickElement(element, random = (Random.new(Time.now.nsec))) ⇒ Object
Given a list of elements with length order(a key for the hash), pick the next element based on the random number generator
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/markov-reloaded.rb', line 90 def pickElement(element, random=(Random.new(Time.now.nsec))) arr = @data[element] if arr == nil print element return nil else i = random.rand(arr.length) arr[i] end end |