3 # Author: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
4 # New markov chain plugin
37 @hash.delete(el) if @hash[el] == 0
59 return @hash.keys.first
61 make_picker unless @valid_pick
63 @picker.each { |ch, el|
64 return el if pick < ch
74 # Word or nonword regexp:
75 # can be used to scan a string splitting it into
80 # mkv[i] holds the chains of order i
83 # Each chain is in the form
84 # [:array, :of, :symbols] => {
85 # :prev => ChanceHash,
88 # except for order 0, which is a simple ChanceHash
92 @mkv[0] = ChanceHash.new
94 @mkv[i] = Hash.new { |hash, key|
95 hash[key] = {:prev => ChanceHash.new, :next => ChanceHash.new}
103 s = sym.to_sym rescue nil
107 def add_before(array, prev)
108 raise "Not enough words in new data" if array.empty?
109 raise "Too many words in new data" if array.size > MAX_ORDER
111 h = @mkv[size][array.dup]
112 h[:prev].increase(prev)
115 def add_after(array, nxt)
116 raise "Not enough words in new data" if array.empty?
117 raise "Too many words in new data" if array.size > MAX_ORDER
119 h = @mkv[size][array.dup]
120 h[:next].increase(nxt)
124 raise "Too many words in new data" if array.size > MAX_ORDER + 1
125 add_before(array.butfirst, array.first)
126 add_after(array.butlast, array.last)
137 def simple_learn(text)
138 syms = text.scan(WNW).map { |w| w.intern }
142 syms.size.times { |i|
143 [MAX_ORDER, syms.size-i].min.times { |ord|
145 # puts "Learning #{v.inspect}"
152 def learn(text, o={})
153 opts = {:lowercase => true}.merge o
155 lc = opts[:lowercase]
159 simple_learn(text.downcase)
162 pp @mkv if defined? pp
167 mkv = MarkovChainer.new
169 mkv.learn("This is a test, a nice little test indeed.")