smashwilson / hubot-markov

Hubot watches all, and builds a markov model from everything you say.
MIT License
61 stars 23 forks source link
coffeescript hubot markov-model

Hubot Markov Model

npm | Continuous Integration

Generates a markov model based on everything that your Hubot sees in your chat.

Installing

  1. Add hubot-markov to your package.json with npm install --save hubot-markov:
  "dependencies": {
    "hubot-markov": "~2.0.0"
  },
  1. Require the module in external-scripts.json:
["hubot-markov"]
  1. Run npm update and restart your Hubot.

Consult the upgrading guide for instructions on migrating from older major versions.

Commands

By default, saying anything at all in chat trains the model. The robot is always watching!

Hubot: markov will randomly generate text based on the current contents of its model.

Hubot: markov your mother is a will generate a random phrase seeded with the phrase you give it. This command might output "your mother is a classy lady", for example. Remember: Hubot is an innocent soul, and what he says only acts as a mirror for everything in your hearts.

Hubot: remarkov and Hubot: mmarkov are similar, but traverse node transitions in different directions: remarkov chains backwards from a given ending state, and mmarkov chains both forward and backward.

Configuration

The Hubot markov model can optionally be configured by setting environment variables:

To re-use a PostgreSQL connection with other parts of your Hubot, define a robot method called getDatabase that returns the connection object. This package uses pg-promise.

Custom models

Store and generate text from arbitrary sources and in more complex commands by using the programmatic API available at robot.markov. Call robot.markov.createModel during script initialization to configure a model, then use robot.markov.modelNamed to access the model instance in commands that train it or generate from it.

Example: Manual Model

module.exports = (robot) ->
  MODELNAME = 'manual'

  # Create or connect to a model with all default options
  robot.markov.createModel MODELNAME

  robot.respond /modeladd\s+(.+)/, (msg) ->
    robot.markov.modelNamed MODELNAME, (model) ->
      model.learn msg.match[1], ->
        msg.reply 'Input accepted.'

  robot.respond /modelgen(?:\s+(.+))/, (msg) ->
    robot.markov.modelNamed MODELNAME, (model) ->
      model.generate msg.match[1] or '', 50, (output) ->
        msg.reply output

Example: Letter-Based Model

module.exports = (robot) ->
  MODELNAME = 'letters'

  # Create or connect to a model with a custom pre- and post-processor
  robot.markov.createModel MODELNAME, {}, (model) ->
    model.processWith
      pre: (input) -> input.split('')
      post: (output) -> output.join('')

  robot.catchAll (msg) ->
    # Filter out "lol"
    return if /^\s*l(o+)l\s*/.test msg.text

    robot.markov.modelNamed MODELNAME, (model) ->
      model.learn msg.text

  robot.respond /lettergen(?:\s+(.+))/, (msg) ->
    robot.markov.modelNamed MODELNAME, (model) ->
      model.generate msg.match[1] or '', 100, (output) ->
        msg.reply output

The full API is available in the docs/ directory.