3 # Author: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
4 # New markov chain plugin
37 @hash.delete(el) if @hash[el] == 0
55 make_picker unless @valid_pick
57 @picker.each { |ch, el|
58 return el if pick < ch
67 # Word or nonword regexp:
68 # can be used to scan a string splitting it into
73 # mkv[i] holds the chains of order i
76 # Each chain is in the form
77 # [:array, :of, :symbols] => {
78 # :prev => ChanceHash,
81 # except for order 0, which is a simple ChanceHash
85 @mkv[0] = ChanceHash.new
87 @mkv[i] = Hash.new { |hash, key|
88 hash[key] = {:prev => ChanceHash.new, :next => ChanceHash.new}
96 s = sym.to_sym rescue nil
100 def add_before(array, prev)
101 raise "Not enough words in new data" if array.empty?
102 raise "Too many words in new data" if array.size > MAX_ORDER
104 h = @mkv[size][array.dup]
105 h[:prev].increase(prev)
108 def add_after(array, nxt)
109 raise "Not enough words in new data" if array.empty?
110 raise "Too many words in new data" if array.size > MAX_ORDER
112 h = @mkv[size][array.dup]
113 h[:next].increase(nxt)
117 raise "Too many words in new data" if array.size > MAX_ORDER + 1
118 add_before(array.butfirst, array.first)
119 add_after(array.butlast, array.last)
130 def simple_learn(text)
131 syms = text.scan(WNW).map { |w| w.intern }
135 syms.size.times { |i|
136 [MAX_ORDER, syms.size-i].min.times { |ord|
138 # puts "Learning #{v.inspect}"
145 def learn(text, o={})
146 opts = {:lowercase => true}.merge o
148 lc = opts[:lowercase]
152 simple_learn(text.downcase)
155 pp @mkv if defined? pp
160 mkv = MarkovChainer.new
162 mkv.learn("This is a test, a nice little test indeed.")