aboutsummaryrefslogtreecommitdiff
path: root/markov-analyse.rb
blob: 726341112a6d1f006ff1b18250912a5a4d71368a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module Markov
	class Analyser
		def initialize(inputfile)
			@words = File.readlines(inputfile)
						 .map(&:split)
						 .flatten
		end

		def analyse(chunklength)
			@chunklength = chunklength
			@stats = {"__SETTINGS__" => {"__CHUNKLENGTH__" => @chunklength}}

			(0 .. (@words.length - @chunklength - @chunklength)).each do |i|
				k = @words[i...(i+@chunklength)]
				v = @words[(i+@chunklength)]
				if @stats.include?(k) then
					t = @stats[k]
					if t.include?(v) then
						t[v] += 1
					else
						t[v] = 1
					end
				else
					@stats[k] = Hash.new
					@stats[k][v] = 1
				end
			end
		end

		def stats
			return @stats
		end

		def save(outputfile)
			File.new(outputfile, "w").print(Marshal::dump(@stats))
		end
	end
end