markov-reloaded 0.0.1 → 0.0.2
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.
- data/lib/markov-reloaded.rb +13 -4
- metadata +2 -2
data/lib/markov-reloaded.rb
CHANGED
@@ -49,17 +49,22 @@ class Array
|
|
49
49
|
Markov.new(data, order)
|
50
50
|
end
|
51
51
|
end
|
52
|
+
#Main class for generating Markov Chains
|
52
53
|
class Markov
|
54
|
+
#Initialize the markov object from a hash
|
55
|
+
#data is the hash to generate by, order is the order of the hash
|
53
56
|
def initialize (data, order)
|
54
57
|
@data = data
|
55
58
|
@order = order
|
56
59
|
end
|
57
60
|
attr_accessor :data
|
61
|
+
#Analyze an array and add it to the current object's hash
|
58
62
|
def add(array)
|
59
|
-
self.+(array.toMarkov
|
63
|
+
self.+(array.toMarkov @order)
|
60
64
|
end
|
65
|
+
#Analyze array and create Markov object from it
|
61
66
|
def self.analyze(array, order)
|
62
|
-
data = Hash.new(
|
67
|
+
data = Hash.new(nil)
|
63
68
|
for i in 0..array.length-1-order
|
64
69
|
el = array[i..i+order-1]
|
65
70
|
if data.include? el
|
@@ -70,6 +75,7 @@ class Markov
|
|
70
75
|
end
|
71
76
|
Markov.new(data, order)
|
72
77
|
end
|
78
|
+
#Add the data of another markov object to the current one
|
73
79
|
def +(mark)
|
74
80
|
hash = mark.data
|
75
81
|
hash.each do |key, value|
|
@@ -80,13 +86,16 @@ class Markov
|
|
80
86
|
end
|
81
87
|
end
|
82
88
|
end
|
83
|
-
|
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
|
+
def pickElement(element, random=(Random.new(Time.now.nsec)))
|
84
91
|
arr = @data[element]
|
85
92
|
i = random.rand(arr.length)
|
86
93
|
arr[i]
|
87
94
|
end
|
88
95
|
attr_accessor :data
|
89
|
-
|
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.
|
98
|
+
def generate(maxlength, first= [], seed=(Time.now.nsec), terminator = ".")
|
90
99
|
r = Random.new seed
|
91
100
|
first = @data.keys.pickRand(r) if first == []
|
92
101
|
out = first.dup
|
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.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-23 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! "markov-reloaded provides a simple interface to create markov chains.\n\tthe
|
15
15
|
system is based on hashes, but provides flexible methods to generate the chains
|