Sha256: bfb7befd4cce6bba5ac5ee1a813bc91e8d6c2d7b51fcf8c06bdf9ae9f8c23824

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

require 'marky_markov'

module Lita
  module Handlers
    class OnewheelSnowcrash < Handler
      route /^snowcrash$/i,
            :generate_random_sentence,
            command: true
      route /^snowcrash (\d+)$/i,
            :generate_by_number,
            command: true
      route /^snowcrashwords$/i,
            :generate_random_words,
            command: true
      route /^snowcrashwords (\d+)$/i,
            :generate_words_by_number,
            command: true

      def generate_random_sentence(response)
        response.reply return_sentence_chain(1)
      end

      def generate_by_number(response)
        response.reply return_sentence_chain(response.matches[0][0])
      end

      def generate_random_words(response)
        response.reply return_word_chain(Random::rand(20))
      end

      def generate_words_by_number(response)
        response.reply return_word_chain(response.matches[0][0])
      end

      def get_markov
        MarkyMarkov::Dictionary.new('dict/snowcrash')
      end

      def return_word_chain(number)
        Lita.logger.info "Generating #{number} words."
        markov = get_markov # Saves/opens dictionary.mmd
        words = markov.generate_n_words(number.to_i)
        Lita.logger.info words
        words
      end

      def return_sentence_chain(number)
        Lita.logger.info "Generating #{number} sentences."
        markov = get_markov # Saves/opens dictionary.mmd
        sentences = markov.generate_n_sentences(number.to_i)
        Lita.logger.info sentences
        sentences
      end

      Lita.register_handler(self)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lita-onewheel-snowcrash-1.0.1 lib/lita/handlers/onewheel_snowcrash.rb