markov-reloaded 0.1.0 → 0.2.0

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 (2) hide show
  1. data/lib/markov-reloaded.rb +16 -7
  2. metadata +4 -2
@@ -89,15 +89,26 @@ class Markov
89
89
  #Given a list of elements with length order(a key for the hash), pick the next element based on the random number generator
90
90
  def pickElement(element, random=(Random.new(Time.now.nsec)))
91
91
  arr = @data[element]
92
- i = random.rand(arr.length)
93
- arr[i]
92
+ if arr == nil
93
+ print element
94
+ return nil
95
+ else
96
+ i = random.rand(arr.length)
97
+ arr[i]
98
+ end
94
99
  end
95
100
  attr_accessor :data
96
- #Given a maximum length, a list of elements with length order at which to begin the generation(nil wil be output at every point if the list of elements does not exist in the analyzed array)
97
- #a seed for the random number generator and an element at which to terminate(defaults to "."). Generates and outputs an array corresponding to a markov chain.
101
+ #Given a maximum length, and a point at which to begin the generation(this can be either the empty list, in which case the generation begins at a random point,
102
+ # a list of elements with lengt=order, in which case it begins with them or a Method object accepting two arguments, which will be passed the Hash of data that makes up this Markov object
103
+ # and a random seed, which must then return a usable list of elements at which the generation will then start) and a seed for the random number generator and an element at which to
104
+ #terminate(defaults to "."). Generates and outputs an array corresponding to a markov chain.
98
105
  def generate(maxlength, first= [], seed=(Time.now.nsec), terminator = ".")
99
106
  r = Random.new seed
100
- first = @data.keys.pickRand(r) if first == []
107
+ if first.class == Method
108
+ first = first.call(@data, r.rand(10000))
109
+ else
110
+ first = @data.keys.pickRand(r) if first == [] || first == ""
111
+ end
101
112
  out = first.dup
102
113
  pick = out.dup
103
114
  for i in 0..(maxlength-@order)
@@ -122,5 +133,3 @@ class Markov
122
133
  return out
123
134
  end
124
135
  end
125
- mark = Markov.analyze("James.Smith.John.Carl.Joseph.Robert.Matthew.Magnus.Eric.Gregory.", 1)
126
- print mark.generate(50, [], Time.now.nsec, ".")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markov-reloaded
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -15,7 +15,9 @@ description: ! "markov-reloaded provides a simple interface to create markov cha
15
15
  system is based on hashes, but provides flexible methods to generate the chains
16
16
  based on any array, and to generate suitable arrays from strings.\n\tIn addition,
17
17
  version 0.1.0 extends functionality, letting one input strings instead of arrays
18
- directly into the analyze method(treating each character\n\t\tas an element"
18
+ directly into the analyze method(treating each character\n\tas an element)\n\tAs
19
+ of version 0.2.0, the generate method gives the user the ability to supply a custom
20
+ Method object to decide the starting point"
19
21
  email: ayegill@gmail.com
20
22
  executables: []
21
23
  extensions: []