lib/lita/handlers/markov.rb in lita-markov-0.0.1 vs lib/lita/handlers/markov.rb in lita-markov-1.0.0
- old
+ new
@@ -1,43 +1,48 @@
+# Forward definition of Markov handler class
+class Lita::Handlers::Markov < Lita::Handler; end
+
+require 'lita/handlers/markov/engine'
+
module Lita::Handlers
- class Markov < Lita::Handler
- Dictionary = MarkyMarkov::PersistentJSONDictionary
+ class Markov
+ attr_reader :engine
- REDIS_KEY_PREFIX = 'lita-markov:'
+ config :database_url
route(/.+/, :ingest, command: false)
route(/markov (.+)/, :generate, command: true, help: {
'markov USER' => 'Generate a markov chain from the given user.'
})
+ def initialize(robot)
+ super(robot)
+
+ @engine = Engine.new self
+ end
+
def ingest(chat)
# Don't ingest messages addressed to ourselves
return if chat.command?
message = chat.matches[0].strip
# Get the mention name (ie. 'dirk') of the user
- name = chat.user.mention_name
- dictionary = dictionary_for_user name
+ id = chat.user.id
- # Passing `false` to indicate it's a string and not a file name
- dictionary.parse_source message, false
-
- save_dictionary name, dictionary
+ @engine.ingest id, message
end
def generate(chat)
name = chat.matches[0][0].strip
+ id = Lita::User.fuzzy_find(name).id
- dictionary = dictionary_for_user name
- generator = MarkovSentenceGenerator.new dictionary
-
begin
- sentence = generator.generate_sentence 1
+ sentence = @engine.generate_sentence_for id
chat.reply sentence
- rescue EmptyDictionaryError
+ rescue Engine::EmptyDictionaryError
chat.reply "Looks like #{name} hasn't said anything!"
end
end
def save_dictionary(name, dictionary)