Skip to content

Instantly share code, notes, and snippets.

@markburns
Created September 13, 2012 19:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markburns/3716931 to your computer and use it in GitHub Desktop.
Save markburns/3716931 to your computer and use it in GitHub Desktop.
generate slightly more human friendly UUID
#!/usr/bin/env ruby
#usage file is optional, caches output in markov.yaml for subsequent runs
#./markov [<file>]
require './markov'
yaml = "markov.yaml"
m = Markov.new
m.load yaml unless file = ARGV[0]
if file
m.add Words.from_file file
m.save yaml
end
puts Words.from(m)
# Markov Chain Generator
# based on the Python version by Gary Burd: http://gary.burd.info/2003/11/markov-chain-generator.html
# Released into the public domain, please keep this notice intact
# (c) InVisible GmbH
# http://www.invisible.ch
# considerable reworking/refactoring by Mark Burns
require "YAML"
require './words'
class Markov
attr_accessor :data
def initialize(data = nil )
@data = data if data.class == Hash
@data ||= Hash.new
end
def add words
words.add_to @data
end
def save filename
File.open(filename, "w") do |f|
YAML.dump( @data, f )
end
end
def load filename
File.open(filename) do |f|
@data = YAML.load f
end
end
end
# Markov Chain Generator
# based on the Python version by Gary Burd: http://gary.burd.info/2003/11/markov-chain-generator.html
# Released into the public domain, please keep this notice intact
# (c) InVisible GmbH
# http://www.invisible.ch
# considerable reworking/refactoring/re-purposing by Mark Burns
class Words
SEPARATOR = "#-#-"
def to_s i=8
@words.join("")[0..i.to_i]
end
def initialize words
@words = words
end
def add_to markov_data
key = SEPARATOR
@words.each do |word|
(markov_data[key] ||= []) << word
key = self.class.new_key(key, word)
end
end
private
class << self
def from m, length = 100
markov_data = m.data
key = SEPARATOR
word = ""
result = 100.times.map do
word = markov_data[key].sample rescue nil
key = new_key( key, word )
word
end
new result
end
def from_string l
l.gsub(/[^a-z]/i,'').split //
end
def from_file( f )
new lines(f).map { |l| from_string l }.flatten
end
def new_key(key, word)
return SEPARATOR if word == "\n"
word or key
end
private
def lines f
content = File.read f
c = content.length
r = rand c
content[r-c .. r+c].downcase.split "\n"
end
end
end
@markburns
Copy link
Author

I downloaded 'The Complete Works of Shakespeare' from project Gothenburg

http://www.gutenberg.org/ebooks/100.txt.utf-8

and saved as 'pg100.txt' for a sample file for generating markov text.

@markburns
Copy link
Author

The markov gem only seems to do 2-grams so it could be improved by using larger n-grams

@stevegraham
Copy link

lol nice

@markburns
Copy link
Author

Cheers. Now uses completely different code and will generate slightly more natural seeming output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment