Skip to content

Instantly share code, notes, and snippets.

@erik-megarad
Created July 20, 2013 02:13
Show Gist options
  • Save erik-megarad/6043559 to your computer and use it in GitHub Desktop.
Save erik-megarad/6043559 to your computer and use it in GitHub Desktop.
require 'marky_markov'
require 'isaac'
DEBUG = ARGV[0]
configure do |c|
c.nick = DEBUG ? "marky_markov_t" : "marky_markov"
c.server = "irc.damballa"
c.port = 6667
end
on :connect do
if DEBUG
join "#coolertest"
msg "#coolertest", "Thank you for testing your shit!"
else
join "#sandbox", "#watercooler"
msg "#sandbox", "I LIVE!"
end
end
on :channel, /^marky_markov:? imitate (.*)$/ do |input|
msg channel, markov(input)
end
on :channel, /(.*)/ do |input|
write_log(nick, input)
end
on :channel, /^marky_markov:? refresh/ do
refresh()
end
helpers do
def refresh(*_)
quit "I will be reborn!"
FileUtils.cd File.dirname __FILE__
system 'git pull origin master'
exit # runsv should hopefully restart
end
def write_log(nick, line)
file = File.join(File.dirname(__FILE__), 'users', "#{nick}.log")
File.open(file, "a+") do |f|
f.write(line + "\n")
end
end
def markov(input)
file = File.join(File.dirname(__FILE__), 'users', "#{input}.log")
return "I don't know who that is!" unless File.exists?(file)
markov = MarkyMarkov::TemporaryDictionary.new
markov.parse_string remove_uris(File.read(file).split("\n").join("."))
return markov.generate_n_sentences 1
end
URI_REGEX = %r"((?:(?:[^ :/?#]+):)(?://(?:[^ /?#]*))(?:[^ ?#]*)(?:\?(?:[^ #]*))?(?:#(?:[^ ]*))?)"
def remove_uris(text)
text.force_encoding('US-ASCII').split(URI_REGEX).collect do |s|
unless s =~ URI_REGEX
s
end
end.join
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment