diff options
author | Nathan Lasseter <nathan@4574.co.uk> | 2014-01-30 20:10:56 +0000 |
---|---|---|
committer | Nathan Lasseter <nathan@4574.co.uk> | 2014-01-30 20:10:56 +0000 |
commit | 69ee9398256a7e15e64aa73428e7291f6f50a11c (patch) | |
tree | e0f6d35a62c2d1abf2589815a3e7b09d47b32841 | |
parent | 46804facd4b74e6c5796b3427ccbb1a273528395 (diff) |
-rw-r--r-- | README.textile | 16 | ||||
-rw-r--r-- | markov-analyse.rb | 14 | ||||
-rw-r--r-- | markov-generate.rb | 8 | ||||
-rwxr-xr-x | markov-repl.rb | 14 |
4 files changed, 35 insertions, 17 deletions
diff --git a/README.textile b/README.textile index 815dcec..7bd16e0 100644 --- a/README.textile +++ b/README.textile @@ -2,9 +2,13 @@ h1. Ruby Markov Chain Generator h2. Usage -bc. $ ./markov-repl.rb ->> load corpus <corpusfile> <chunklength> ->> analyse ->> save <datafile> ->> load serial <datafile> ->> generate <length> +bc.. $ ./markov-repl.rb +>> load corpus <corpusfile> +>> analyse <chunklength> +>> load stats +>> generate <numwords> +>> save <serialisedfile> + +$ ./markov-repl.rb +>> load stats <serialisedfile> +>> generate <numwords> diff --git a/markov-analyse.rb b/markov-analyse.rb index 6c306ca..7263411 100644 --- a/markov-analyse.rb +++ b/markov-analyse.rb @@ -1,15 +1,15 @@ module Markov class Analyser - def initialize(inputfile, chunklength) - @chunklength = chunklength - @stats = {"__SETTINGS__" => {"__CHUNKLENGTH__" => @chunklength}} - + def initialize(inputfile) @words = File.readlines(inputfile) .map(&:split) .flatten end - def analyse + 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)] @@ -27,6 +27,10 @@ module Markov end end + def stats + return @stats + end + def save(outputfile) File.new(outputfile, "w").print(Marshal::dump(@stats)) end diff --git a/markov-generate.rb b/markov-generate.rb index 36d80a1..4213a58 100644 --- a/markov-generate.rb +++ b/markov-generate.rb @@ -1,8 +1,12 @@ module Markov class Generator - def initialize(inputfile) + def initialize(input, type=:var) @prng = Random.new(Time.now.to_i) - @stats = Marshal::load(File.open(inputfile).read) + if type == :var then + @stats = input + elsif type == :file then + @stats = Marshal::load(File.open(input).read) + end @chunklength = @stats["__SETTINGS__"]["__CHUNKLENGTH__"] end diff --git a/markov-repl.rb b/markov-repl.rb index 624539f..0d0ae63 100755 --- a/markov-repl.rb +++ b/markov-repl.rb @@ -12,12 +12,16 @@ def handle_input(inputs) when 'load' case input[1] when 'corpus' - $replstate[:analyser] = Markov::Analyser.new(input[2], input[3].to_i) - when 'serial' - $replstate[:generator] = Markov::Generator.new(input[2]) + $replstate[:analyser] = Markov::Analyser.new(input[2]) + when 'stats' + unless input[2].nil? then + $replstate[:generator] = Markov::Generator.new(input[2], :file) + else + $replstate[:generator] = Markov::Generator.new($replstate[:analyser].stats) + end end when 'analyse' - $replstate[:analyser].analyse + $replstate[:analyser].analyse(input[1].to_i) when 'save' $replstate[:analyser].save(input[1]) when 'generate' @@ -25,6 +29,8 @@ def handle_input(inputs) when 'quit' puts exit + else + puts "Error" end end |