From 79a5cf84254c2aeed244861197fdc5fe3282a940 Mon Sep 17 00:00:00 2001 From: Nathan Lasseter Date: Tue, 14 Jan 2014 17:55:47 +0000 Subject: First Commit --- .gitignore | 2 ++ README.TEXTILE | 6 ++++++ markov-run.rb | 35 +++++++++++++++++++++++++++++++++++ markov-serialise.rb | 27 +++++++++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 .gitignore create mode 100644 README.TEXTILE create mode 100755 markov-run.rb create mode 100755 markov-serialise.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81f4387 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.txt +*.ser diff --git a/README.TEXTILE b/README.TEXTILE new file mode 100644 index 0000000..110494a --- /dev/null +++ b/README.TEXTILE @@ -0,0 +1,6 @@ +h1. Ruby Markov Chain Generator + +h2. Usage + +# Run markov-serialise.rb +# Run markov-run.rb diff --git a/markov-run.rb b/markov-run.rb new file mode 100755 index 0000000..ef81315 --- /dev/null +++ b/markov-run.rb @@ -0,0 +1,35 @@ +#!/usr/bin/env ruby + +PRNG = Random.new(Time.now.to_i) + +stats = Marshal::load(File.open(ARGV[0]).read) + +def cfd(hash) + tot = 0 + hash.each_pair {|k, v| + hash[k] = tot += v + } +end + +def sel(hash) + max = hash.values.max + r = PRNG.rand(max*100000) % max + hash.each_pair {|k, v| + return k if r < v + } +end + +current = stats.keys.select{|k|k.first.match(/^[A-Z]/)}.sample + +print "#{current.join(" ")} " + +def achunk(chunk, hash) + sel(cfd(hash[chunk])) +end + +(ARGV[1].to_i - 1).times do + current = achunk(current, stats) + print "#{current.join(" ")} " +end + +puts diff --git a/markov-serialise.rb b/markov-serialise.rb new file mode 100755 index 0000000..00e459e --- /dev/null +++ b/markov-serialise.rb @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby + +CHUNK = ARGV[1].to_i + +stats = Hash.new + +words = File.readlines(ARGV[0]) + .map(&:split) + .flatten + +(0 .. (words.length - CHUNK - CHUNK)).each do |i| + k = words[i...(i+CHUNK)] + v = words[(i+CHUNK)...(i+CHUNK+CHUNK)] + 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 + +File.new(ARGV[2], "w").print(Marshal::dump(stats)) -- cgit v1.2.1