Class: Lita::Handlers::Markov
- Inherits:
-
Lita::Handler
- Object
- Lita::Handler
- Lita::Handlers::Markov
- Defined in:
- lib/lita/handlers/markov.rb,
lib/lita/handlers/markov/engine.rb,
lib/lita/handlers/markov.rb
Overview
Forward definition of Markov handler class
Defined Under Namespace
Classes: Engine
Class Method Summary collapse
-
.engine(instance) ⇒ Object
Share the engine instance between all instances of the bot.
Instance Method Summary collapse
- #backlog_form(request, response) ⇒ Object
- #engine ⇒ Object
- #generate(chat) ⇒ Object
- #ingest(chat) ⇒ Object
- #upload_backlog(request, response) ⇒ Object
Class Method Details
.engine(instance) ⇒ Object
Share the engine instance between all instances of the bot
23 24 25 |
# File 'lib/lita/handlers/markov.rb', line 23 def self.engine(instance) @engine ||= Engine.new(instance.config.database_url) end |
Instance Method Details
#backlog_form(request, response) ⇒ Object
60 61 62 |
# File 'lib/lita/handlers/markov.rb', line 60 def backlog_form(request, response) render_backlog_form response end |
#engine ⇒ Object
26 27 28 |
# File 'lib/lita/handlers/markov.rb', line 26 def engine self.class.engine(self) end |
#generate(chat) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/lita/handlers/markov.rb', line 42 def generate(chat) name = simplify_name chat.matches[0][0] user = Lita::User.fuzzy_find name if user.nil? chat.reply "Couldn't find the user #{name}. :(" return end begin sentence = engine.generate_sentence_for user.id chat.reply sentence rescue Engine::EmptyDictionaryError chat.reply "Looks like #{name} hasn't said anything!" end end |
#ingest(chat) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/lita/handlers/markov.rb', line 30 def ingest(chat) # Don't ingest messages addressed to ourselves return if chat.command? = chat.matches[0].strip # Get the mention name (ie. 'dirk') of the user id = chat.user.id engine.ingest id, end |
#upload_backlog(request, response) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/lita/handlers/markov.rb', line 64 def upload_backlog(request, response) t0 = Time.now response.headers['Content-Type'] = 'text/plain' multipart = Rack::Multipart.parse_multipart request.env tempfile = multipart.values.first[:tempfile] begin = Oj.load File.read(tempfile.path).strip, :mode => :strict rescue Oj::ParseError => error response.write error. return end .select! { |m| m['type'] == 'message' } users = {} find_user = proc do |id| users[id] ||= Lita::User.fuzzy_find id end = /<(\w|[!|@])+>/ count = 0 .each do || count += 1 begin text = ['text'.freeze] next unless text user = find_user.call ['user'.freeze] unless user response.write "User not found for message ##{count}: #{message['user']}\n" next end = text.gsub , ''.freeze engine.ingest user.id, if count % 1000 == 0 response.write "Processed #{count} messages\n" end rescue => error response.write "Error writing message ##{count}: #{error.inspect}\n" end end response.write "Processed #{count} total messages in #{Time.now - t0} seconds\n" end |