<commit_before>/*
 * Copyright (C) 2017  Anthony DeDominic <adedomin@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

var Irc = require('irc-framework'),
    colors = require('irc-colors'),
    output = require('./moose-res').output,
    config = require(process.env.CONFIG_PATH).irc,
    mooseParse = /^[.!]*moose(?:me)? (-[^ ]*)? ?(.*)/

module.exports = (moosedb, logger) => {
    if (!config || config.disabled) return

    var irc = new Irc.Client()

    irc.on('registered', () => {
        logger.debug('IRC CONN STARTED')
        if (config.nickserv_pass)
            irc.say('NickServ', `IDENTIFY ${config.nickserv_pass}`)

        config.channels.forEach(channel => irc.join(channel))
    })

    irc.matchMessage(/^[.!](bots|help)/, (event) => {
        event.reply(
            `NeoMoose [JavaScript|NodeJS] :: Make moose @ ${config.moose_url || 'https://moose.ghetty.space'} :: See .moose --help for usage`
        )
    })

    irc.matchMessage(/^[.!]*moose(me)?/, function(event) {
        var parse = event.message
            .match(mooseParse)
        if (!parse) parse = ['', '--random', '']

        var arg = parse[1],
            query = parse[2]

        if (arg == '--latest' ||
            arg == '-l' ||
            query == 'latest'
        ) {
            if (this.mooseLock)
                return irc.notice(event.nick, 'Please Wait for the Current Moose to finish')
            this.mooseLock = true
            moosedb.getLatest((err, moose) => {
                if (err) {
                    this.mooseLock = false
                    return event.reply('Unknown error getting latest moose')
                }
                output.output(moose, event, true, () => this.mooseLock = false)
            })
        }
        else if (arg == '--random' ||
            arg == '-r' ||
            query == 'random'
        ) {
            if (this.mooseLock)
                return irc.notice(event.nick, 'Please Wait for the Current Moose to finish')
            this.mooseLock = true
            moosedb.getRandom((err, moose) => {
                if (err) {
                    this.mooseLock = false
                    return event.reply('Unknown error getting latest moose')
                }
                output(moose, event, true, () => this.mooseLock = false)
            })
        }
        else if (arg == '--search' || arg == '-s') {
            if (!query)
                return event.reply('usage: --search query')
            moosedb.getGallery(query, 0, -1, (err, meese) => {
                if (err || !meese || !meese.length) {
                    return event.reply(`No moose found containing ${colors.bold(query)}`)
                }
                event.reply(
                    meese.map(moose => colors.bold(moose.name)).join(' - ')
                )
            })
        }
        else if (arg == '--') {
            if (this.mooseLock)
                return irc.notice(event.nick, 'Please Wait for the Current Moose to finish')
            this.mooseLock = true
            moosedb.getMoose(query, (err, moose) => {
                if (err) {
                    this.mooseLock = false
                    return event.reply(`No such moose ${colors.bold(query)}`)
                }
                output(moose, event, false, () => this.mooseLock = false)
            })
        }
        else if (arg == '--help' || arg == '-h' || !query) {
            event.reply('usage: [.!]*moose [--latest|--random|--search|--] moosename')
            event.reply(`Make moose @ ${config.moose_url || 'https://moose.ghetty.space'}`)
        }
        else if (arg) {
            event.reply(`invalid argument: ${arg}`)
            event.reply('usage: [.!]*moose [--latest|--random] moosename')
        }
        else {
            if (this.mooseLock)
                return irc.notice(event.nick, 'Please Wait for the Current Moose to finish')
            this.mooseLock = true
            moosedb.getMoose(query, (err, moose) => {
                if (err) {
                    this.mooseLock = false
                    return event.reply(`No such moose ${colors.bold(query)}`)
                }
                output(moose, event, false, () => this.mooseLock = false)
            })
        }
    })

    irc.on('join', event => {
        if (event.nick == config.nick)
            logger.debug(`IRC JOIN ${event.channel}`)
    })

    irc.on('part', event => {
        if (event.nick == config.nick)
            logger.info(`IRC PART ${event.channel}`)
    })

    irc.on('kick', event => {
        if (event.kicked == config.nick)
            logger.info(`IRC KICK ${event.channel}`, {
                err: `${event.nick} [${event.message}]`,
            })
    })

    irc.on('close', () => {
        logger.error('IRC CLOSE', { err: 'unable to reconnect' })
    })

    irc.on('socket close', () => {
        logger.error('IRC CLOSE', { err: 'flood kick' })
    })

    irc.connect({
        host: config.server,
        nick: config.nick,
        port: config.port,
        tls: config.tls,
    })
}
<commit_msg>added .source irc command<commit_after>/*
 * Copyright (C) 2017  Anthony DeDominic <adedomin@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

var Irc = require('irc-framework'),
    colors = require('irc-colors'),
    output = require('./moose-res').output,
    config = require(process.env.CONFIG_PATH).irc,
    mooseParse = /^[.!]*moose(?:me)? (-[^ ]*)? ?(.*)/

module.exports = (moosedb, logger) => {
    if (!config || config.disabled) return

    var irc = new Irc.Client()

    irc.on('registered', () => {
        logger.debug('IRC CONN STARTED')
        if (config.nickserv_pass)
            irc.say('NickServ', `IDENTIFY ${config.nickserv_pass}`)

        config.channels.forEach(channel => irc.join(channel))
    })

    irc.matchMessage(/^[.!](bots|help)/, (event) => {
        event.reply(
            `NeoMoose [JavaScript|NodeJS] :: Make moose @ ${config.moose_url || 'https://moose.ghetty.space'} :: See .moose --help for usage`
        )
    })

    irc.matchMessage(/^[.!]source/, (event) => {
        event.reply(
            'NeoMoose [JavaScript|NodeJS] :: Source https://github.com/adedomin/neomoose'
        )
    })

    irc.matchMessage(/^[.!]*moose(me)?/, function(event) {
        var parse = event.message
            .match(mooseParse)
        if (!parse) parse = ['', '--random', '']

        var arg = parse[1],
            query = parse[2]

        if (arg == '--latest' ||
            arg == '-l' ||
            query == 'latest'
        ) {
            if (this.mooseLock)
                return irc.notice(event.nick, 'Please Wait for the Current Moose to finish')
            this.mooseLock = true
            moosedb.getLatest((err, moose) => {
                if (err) {
                    this.mooseLock = false
                    return event.reply('Unknown error getting latest moose')
                }
                output.output(moose, event, true, () => this.mooseLock = false)
            })
        }
        else if (arg == '--random' ||
            arg == '-r' ||
            query == 'random'
        ) {
            if (this.mooseLock)
                return irc.notice(event.nick, 'Please Wait for the Current Moose to finish')
            this.mooseLock = true
            moosedb.getRandom((err, moose) => {
                if (err) {
                    this.mooseLock = false
                    return event.reply('Unknown error getting latest moose')
                }
                output(moose, event, true, () => this.mooseLock = false)
            })
        }
        else if (arg == '--search' || arg == '-s') {
            if (!query)
                return event.reply('usage: --search query')
            moosedb.getGallery(query, 0, -1, (err, meese) => {
                if (err || !meese || !meese.length) {
                    return event.reply(`No moose found containing ${colors.bold(query)}`)
                }
                event.reply(
                    meese.map(moose => colors.bold(moose.name)).join(' - ')
                )
            })
        }
        else if (arg == '--') {
            if (this.mooseLock)
                return irc.notice(event.nick, 'Please Wait for the Current Moose to finish')
            this.mooseLock = true
            moosedb.getMoose(query, (err, moose) => {
                if (err) {
                    this.mooseLock = false
                    return event.reply(`No such moose ${colors.bold(query)}`)
                }
                output(moose, event, false, () => this.mooseLock = false)
            })
        }
        else if (arg == '--help' || arg == '-h' || !query) {
            event.reply('usage: [.!]*moose [--latest|--random|--search|--] moosename')
            event.reply(`Make moose @ ${config.moose_url || 'https://moose.ghetty.space'}`)
        }
        else if (arg) {
            event.reply(`invalid argument: ${arg}`)
            event.reply('usage: [.!]*moose [--latest|--random] moosename')
        }
        else {
            if (this.mooseLock)
                return irc.notice(event.nick, 'Please Wait for the Current Moose to finish')
            this.mooseLock = true
            moosedb.getMoose(query, (err, moose) => {
                if (err) {
                    this.mooseLock = false
                    return event.reply(`No such moose ${colors.bold(query)}`)
                }
                output(moose, event, false, () => this.mooseLock = false)
            })
        }
    })

    irc.on('join', event => {
        if (event.nick == config.nick)
            logger.debug(`IRC JOIN ${event.channel}`)
    })

    irc.on('part', event => {
        if (event.nick == config.nick)
            logger.info(`IRC PART ${event.channel}`)
    })

    irc.on('kick', event => {
        if (event.kicked == config.nick)
            logger.info(`IRC KICK ${event.channel}`, {
                err: `${event.nick} [${event.message}]`,
            })
    })

    irc.on('close', () => {
        logger.error('IRC CLOSE', { err: 'unable to reconnect' })
    })

    irc.on('socket close', () => {
        logger.error('IRC CLOSE', { err: 'flood kick' })
    })

    irc.connect({
        host: config.server,
        nick: config.nick,
        port: config.port,
        tls: config.tls,
    })
}
<|endoftext|>


<commit_before>'use strict'

var path = require('path'),
    async = require('async'),
    _ = require('underscore')

// if you want to use mongoose with kin, pass your mongoose instance in
var Kin = function(mongoose) {
  // constructor
  this.mongoose = mongoose
  this._blueprints = {}
  this._postFunctions = {}
}

function generate(type, overrideProperties, callback) {
  var currentBlueprint = this.blueprint(type)

  if (!this.blueprint(type)){
    return callback(new Error('Invalid model in '+type+' blueprint: ' + modelType))
  }
  var modelType = this.blueprint(type)._model
  var Model
  if (modelType) {
    if (typeof modelType == 'string') {
      // mongoose instance must be passed in
      modelType = this.mongoose && this.mongoose.models[modelType]
      if (!modelType) {
        return callback(new Error('Invalid string model in '+type+' blueprint: ' + this.blueprint(type)._model))
      }
    }
    Model = this.generator(modelType)
  }
  var key
  var self = this
  this.linkProperties(currentBlueprint, overrideProperties, function(err, props) {
    var properties = _.clone(currentBlueprint)
    _.each(props, function(value, key) {
      properties[key] = value
    })
    var keys = _.keys(properties)
    async.forEachSeries(
      keys,
      function(key, callback) {
        var property = properties[key]
        if (typeof property == 'function') {
          Kin.get(properties, key, callback)
        } else {
          callback()
        }
      },
      function(err) {
        if (err) throw err
        var meta = {}
        _.each(properties, function(value, key) {
          if (/^_/.test(key) && key != '_id') {
            var keyWithNoUnderscore = key.substr(1)
            meta[keyWithNoUnderscore] = properties[key]
            delete properties[key]
          }
        })
        if (Model) {
          var model = new Model(properties)
          return callback.call(model, null, model, meta)
        } else {
          return callback.call(properties, null, properties, meta)
        }
      }
    )
  })
}
Kin.prototype._getGeneratorFunction = function(type) {
  var self = this
  var generatorFunction = function(overrideProperties, callback) {
    if (arguments.length == 1 && typeof overrideProperties == 'function') {
      callback = overrideProperties
      overrideProperties = {}
    }
    generate.call(self, type, overrideProperties, function(err, model, meta) {
      if (self._postFunctions[type] && self._postFunctions[type].length > 0) {
        var functionList = _.clone(self._postFunctions[type])
        functionList.unshift(function(next) {
          next(err, model, meta)
        })
        async.waterfall(functionList, function(err, model, meta) {
          callback.call(model, err, model, meta)
        })
      } else {
        callback.call(model, err, model, meta)
      }
    })
  }
  var returnFunction = function(overrideProperties, callback) {
    if (arguments.length == 1 && typeof overrideProperties == 'function') {
      callback = overrideProperties
      overrideProperties = {}
    }
    generatorFunction(overrideProperties, function(err, model, meta) {
      var functionList = _.clone(returnFunction._postFunctions)
      functionList.unshift(function(next) {
        next(err, model, meta)
      })
      async.waterfall(functionList, function(err, model, meta) {
        callback.call(model, err, model, meta)
      })
    })
  }
  returnFunction._postFunctions = []
  returnFunction.post = function(func) {
    returnFunction._postFunctions.push(func)
  }
  return returnFunction
}

Kin.prototype.generate = function(type, overrideProperties, callback) {
  // TODO Clean this mess up.
  var self = this
  // properties is optional
  if (arguments.length >= 2) {
    if (arguments.length == 2 && typeof overrideProperties == 'function') {
      callback = overrideProperties
      overrideProperties = {}
    }
    if (!type) {
      if (callback) return callback(new Error('No Type'))
    }
    return this._getGeneratorFunction(type)(overrideProperties, callback)
  } else if (arguments.length == 1) {
    return this._getGeneratorFunction(type, overrideProperties)
  }
}

Kin.prototype.blueprint = function(type, definition) {
  if (definition) {
    this._blueprints[type] = definition
  }

  return this._blueprints[type]
}

Kin.prototype.post = function(type, func) {
  this._postFunctions[type] = this._postFunctions[type] || []
  this._postFunctions[type].push(func)
}

Kin.evaluateProperty = function(context, property, count, callback) {
  var functions = []
  while(count--) {
    functions.push(property)
  }
  async.mapSeries(
    functions,
    function(item, callback) {
      item.call(context, function(err, result) {
        callback(err, result)
      })
    },
    function(err, results) {
      if (err) throw err
      callback(err, results)
    }
  )
}

Kin.prototype.evaluateProperty = function(property, count, callback) {
  Kin.evaluateProperty(this, property, count, callback)
}

Kin.prototype.linkProperties = function(blueprint, properties, callback) {
  var blueprintProperties = _.keys(blueprint)
  var resultProperties = _.clone(properties)
  var self = this
  async.reduce(
    blueprintProperties,
    resultProperties,
    function(memo, key, callback) {
      var item = blueprint[key]
      if (typeof item == 'function' && typeof memo[key] == 'number') {
        Kin.evaluateProperty(memo, item, memo[key], function(err, results) {
          memo[key] = results
          callback(null, memo)
        })
      } else {
        memo[key] = blueprint[key]
        if (properties[key] != undefined) {
          memo[key] = properties[key]
        }
        callback(null, memo)
      }
    },
    function(err, results) {
      if (err) throw err
      callback(null, results)
    }
  )
}

Kin.prototype.randomBetween = function(min, max) {
  if (max < min) return 0
  return Math.floor(Math.random() * (max - min + 1)) + min
}

Kin.generate = function() {
  throw new Error('Not implemented')
}

Kin.prototype.generator = function property(func) {
  func.isGenerator = true
  return func
}

Kin.get = function(object, property, callback) {
  var value = object[property]
  if (!value) return callback(new Error('Property not defined: ' + property))
  if (typeof value == "function" && !value.isGenerator) {
    value.call(object, function(err, gotValue) {
      object[property] = gotValue
      callback(err, gotValue)
    })
  } else {
    callback(null, value)
  }
}

module.exports = Kin
<commit_msg>Slightly more descriptive error messages<commit_after>'use strict'

var path = require('path'),
    async = require('async'),
    _ = require('underscore')

// if you want to use mongoose with kin, pass your mongoose instance in
var Kin = function(mongoose) {
  // constructor
  this.mongoose = mongoose
  this._blueprints = {}
  this._postFunctions = {}
}

function generate(type, overrideProperties, callback) {
  var currentBlueprint = this.blueprint(type)

  if (!this.blueprint(type)){
    return callback(new Error('Invalid model type in '+type+' blueprint: ' + modelType))
  }
  var modelType = this.blueprint(type)._model
  var Model
  if (modelType) {
    if (typeof modelType == 'string') {
      // mongoose instance must be passed in
      modelType = this.mongoose && this.mongoose.models[modelType]
      if (!modelType) {
        return callback(new Error('Invalid string model type in '+type+' blueprint: ' + this.blueprint(type)._model))
      }
    }
    Model = this.generator(modelType)
  }
  var key
  var self = this
  this.linkProperties(currentBlueprint, overrideProperties, function(err, props) {
    var properties = _.clone(currentBlueprint)
    _.each(props, function(value, key) {
      properties[key] = value
    })
    var keys = _.keys(properties)
    async.forEachSeries(
      keys,
      function(key, callback) {
        var property = properties[key]
        if (typeof property == 'function') {
          Kin.get(properties, key, callback)
        } else {
          callback()
        }
      },
      function(err) {
        if (err) throw err
        var meta = {}
        _.each(properties, function(value, key) {
          if (/^_/.test(key) && key != '_id') {
            var keyWithNoUnderscore = key.substr(1)
            meta[keyWithNoUnderscore] = properties[key]
            delete properties[key]
          }
        })
        if (Model) {
          var model = new Model(properties)
          return callback.call(model, null, model, meta)
        } else {
          return callback.call(properties, null, properties, meta)
        }
      }
    )
  })
}
Kin.prototype._getGeneratorFunction = function(type) {
  var self = this
  var generatorFunction = function(overrideProperties, callback) {
    if (arguments.length == 1 && typeof overrideProperties == 'function') {
      callback = overrideProperties
      overrideProperties = {}
    }
    generate.call(self, type, overrideProperties, function(err, model, meta) {
      if (self._postFunctions[type] && self._postFunctions[type].length > 0) {
        var functionList = _.clone(self._postFunctions[type])
        functionList.unshift(function(next) {
          next(err, model, meta)
        })
        async.waterfall(functionList, function(err, model, meta) {
          callback.call(model, err, model, meta)
        })
      } else {
        callback.call(model, err, model, meta)
      }
    })
  }
  var returnFunction = function(overrideProperties, callback) {
    if (arguments.length == 1 && typeof overrideProperties == 'function') {
      callback = overrideProperties
      overrideProperties = {}
    }
    generatorFunction(overrideProperties, function(err, model, meta) {
      var functionList = _.clone(returnFunction._postFunctions)
      functionList.unshift(function(next) {
        next(err, model, meta)
      })
      async.waterfall(functionList, function(err, model, meta) {
        callback.call(model, err, model, meta)
      })
    })
  }
  returnFunction._postFunctions = []
  returnFunction.post = function(func) {
    returnFunction._postFunctions.push(func)
  }
  return returnFunction
}

Kin.prototype.generate = function(type, overrideProperties, callback) {
  // TODO Clean this mess up.
  var self = this
  // properties is optional
  if (arguments.length >= 2) {
    if (arguments.length == 2 && typeof overrideProperties == 'function') {
      callback = overrideProperties
      overrideProperties = {}
    }
    if (!type) {
      if (callback) return callback(new Error('No Type'))
    }
    return this._getGeneratorFunction(type)(overrideProperties, callback)
  } else if (arguments.length == 1) {
    return this._getGeneratorFunction(type, overrideProperties)
  }
}

Kin.prototype.blueprint = function(type, definition) {
  if (definition) {
    this._blueprints[type] = definition
  }

  return this._blueprints[type]
}

Kin.prototype.post = function(type, func) {
  this._postFunctions[type] = this._postFunctions[type] || []
  this._postFunctions[type].push(func)
}

Kin.evaluateProperty = function(context, property, count, callback) {
  var functions = []
  while(count--) {
    functions.push(property)
  }
  async.mapSeries(
    functions,
    function(item, callback) {
      item.call(context, function(err, result) {
        callback(err, result)
      })
    },
    function(err, results) {
      if (err) throw err
      callback(err, results)
    }
  )
}

Kin.prototype.evaluateProperty = function(property, count, callback) {
  Kin.evaluateProperty(this, property, count, callback)
}

Kin.prototype.linkProperties = function(blueprint, properties, callback) {
  var blueprintProperties = _.keys(blueprint)
  var resultProperties = _.clone(properties)
  var self = this
  async.reduce(
    blueprintProperties,
    resultProperties,
    function(memo, key, callback) {
      var item = blueprint[key]
      if (typeof item == 'function' && typeof memo[key] == 'number') {
        Kin.evaluateProperty(memo, item, memo[key], function(err, results) {
          memo[key] = results
          callback(null, memo)
        })
      } else {
        memo[key] = blueprint[key]
        if (properties[key] != undefined) {
          memo[key] = properties[key]
        }
        callback(null, memo)
      }
    },
    function(err, results) {
      if (err) throw err
      callback(null, results)
    }
  )
}

Kin.prototype.randomBetween = function(min, max) {
  if (max < min) return 0
  return Math.floor(Math.random() * (max - min + 1)) + min
}

Kin.generate = function() {
  throw new Error('Not implemented')
}

Kin.prototype.generator = function property(func) {
  func.isGenerator = true
  return func
}

Kin.get = function(object, property, callback) {
  var value = object[property]
  if (!value) return callback(new Error('Property not defined: ' + property))
  if (typeof value == "function" && !value.isGenerator) {
    value.call(object, function(err, gotValue) {
      object[property] = gotValue
      callback(err, gotValue)
    })
  } else {
    callback(null, value)
  }
}

module.exports = Kin
<|endoftext|>


<commit_before>var stem = require('stem-porter');
require('./stopwords.js');

//
// Based on javascript implementation https://github.com/awaisathar/lda.js
// Original code based on http://www.arbylon.net/projects/LdaGibbsSampler.java
//
var process = function(sentences, numberOfTopics, numberOfTermsPerTopic, alphaValue, betaValue) {
	// The result will consist of topics and their included terms [[{"term":"word1", "probability":0.065}, {"term":"word2", "probability":0.047}, ... ], [{"term":"word1", "probability":0.085}, {"term":"word2", "probability":0.024}, ... ]].
	var result = [];
	// Index-encoded array of sentences, with each row containing the indices of the words in the vocabulary.
	var documents = new Array();
	// Hash of vocabulary words and the count of how many times each word has been seen.
	var f = {};
	// Vocabulary of unique words (porter stemmed).
	var vocab=new Array();
	// Vocabulary of unique words in their original form.
	var vocabOrig = {};

	for(var i=0;i<sentences.length;i++) {
		if (sentences[i]=="") continue;
		documents[i] = new Array();

		var words = sentences[i].split(/[\s,\"]+/);

		if(!words) continue;
		for(var wc=0;wc<words.length;wc++) {
			var w=words[wc].toLowerCase().replace(/[^a-z\'A-Z0-9 ]+/g, '');
			var wStemmed = stem(w);
			if (w=="" || !wStemmed || w.length==1 || stop_words.indexOf(w.replace("'", "")) > -1 || stop_words.indexOf(wStemmed) > -1 || w.indexOf("http")==0) continue;
			if (f[wStemmed]) {
				f[wStemmed]=f[wStemmed]+1;
			}
			else if(wStemmed) {
				f[wStemmed]=1;
				vocab.push(wStemmed);
				vocabOrig[wStemmed] = w;
			};

			documents[i].push(vocab.indexOf(wStemmed));
		}
	}

	var V = vocab.length;
	var M = documents.length;
	var K = parseInt(numberOfTopics);
	var alpha = alphaValue || 0.1;  // per-document distributions over topics
	var beta = betaValue || .01;  // per-topic distributions over words

	lda.configure(documents,V,10000, 2000, 100, 10);
	lda.gibbs(K, alpha, beta);

	var theta = lda.getTheta();
	var phi = lda.getPhi();

	var text = '';

	//topics
	var topTerms=numberOfTermsPerTopic;
	for (var k = 0; k < phi.length; k++) {
		var things = new Array();
		for (var w = 0; w < phi[k].length; w++) {
			 things.push(""+phi[k][w].toPrecision(2)+"_"+vocab[w] + "_" + vocabOrig[vocab[w]]);
		}
		things.sort().reverse();
		//console.log(things);
		if(topTerms>vocab.length) topTerms=vocab.length;

		//console.log('Topic ' + (k + 1));
		var row = [];

		for (var t = 0; t < topTerms; t++) {
			var topicTerm=things[t].split("_")[2];
			var prob=parseInt(things[t].split("_")[0]*100);
			if (prob<2) continue;

			//console.log('Top Term: ' + topicTerm + ' (' + prob + '%)');

			var term = {};
			term.term = topicTerm;
			term.probability = parseFloat(things[t].split("_")[0]);
			row.push(term);
		}

		result.push(row);
	}

	return result;
}

function makeArray(x) {
	var a = new Array();
	for (var i=0;i<x;i++)  {
		a[i]=0;
	}
	return a;
}

function make2DArray(x,y) {
	var a = new Array();
	for (var i=0;i<x;i++)  {
		a[i]=new Array();
		for (var j=0;j<y;j++)
			a[i][j]=0;
	}
	return a;
}

var lda = new function() {
	var documents,z,nw,nd,nwsum,ndsum,thetasum,phisum,V,K,alpha,beta;
    var THIN_INTERVAL = 20;
    var BURN_IN = 100;
    var ITERATIONS = 1000;
    var SAMPLE_LAG;
    var dispcol = 0;
	var numstats=0;
	this.configure = function (docs,v,iterations,burnIn,thinInterval,sampleLag) {
        this.ITERATIONS = iterations;
        this.BURN_IN = burnIn;
        this.THIN_INTERVAL = thinInterval;
        this.SAMPLE_LAG = sampleLag;
		this.documents = docs;
		this.V = v;
		this.dispcol=0;
		this.numstats=0;
    }
	this.initialState = function (K) {
        var i;
        var M = this.documents.length;
        this.nw = make2DArray(this.V,K);
        this.nd = make2DArray(M,K);
        this.nwsum = makeArray(K);
        this.ndsum = makeArray(M);
        this.z = new Array();	for (i=0;i<M;i++) this.z[i]=new Array();
        for (var m = 0; m < M; m++) {
            var N = this.documents[m].length;
            this.z[m] = new Array();
            for (var n = 0; n < N; n++) {
                var topic = parseInt(""+(Math.random() * K));
                this.z[m][n] = topic;
                this.nw[this.documents[m][n]][topic]++;
                this.nd[m][topic]++;
                this.nwsum[topic]++;
            }
            this.ndsum[m] = N;
        }
    }

	this.gibbs = function (K,alpha,beta) {
		var i;
        this.K = K;
        this.alpha = alpha;
        this.beta = beta;
        if (this.SAMPLE_LAG > 0) {
            this.thetasum = make2DArray(this.documents.length,this.K);
            this.phisum = make2DArray(this.K,this.V);
            this.numstats = 0;
        }
        this.initialState(K);
        //document.write("Sampling " + this.ITERATIONS
         //   + " iterations with burn-in of " + this.BURN_IN + " (B/S="
         //   + this.THIN_INTERVAL + ").
");
        for (i = 0; i < this.ITERATIONS; i++) {
			for (var m = 0; m < this.z.length; m++) {
                for (var n = 0; n < this.z[m].length; n++) {
			        var topic = this.sampleFullConditional(m, n);
					this.z[m][n] = topic;
                }
            }
            if ((i < this.BURN_IN) && (i % this.THIN_INTERVAL == 0)) {
				//document.write("B");
                this.dispcol++;
            }
            if ((i > this.BURN_IN) && (i % this.THIN_INTERVAL == 0)) {
                //document.write("S");
                this.dispcol++;
            }
            if ((i > this.BURN_IN) && (this.SAMPLE_LAG > 0) && (i % this.SAMPLE_LAG == 0)) {
                this.updateParams();
				//document.write("|");
                if (i % this.THIN_INTERVAL != 0)
                    this.dispcol++;
            }
            if (this.dispcol >= 100) {
				//document.write("*
");
                this.dispcol = 0;
            }
        }
    }

	this.sampleFullConditional = function(m,n) {
        var topic = this.z[m][n];
        this.nw[this.documents[m][n]][topic]--;
        this.nd[m][topic]--;
        this.nwsum[topic]--;
        this.ndsum[m]--;
        var p = makeArray(this.K);
        for (var k = 0; k < this.K; k++) {
            p[k] = (this.nw[this.documents[m][n]][k] + this.beta) / (this.nwsum[k] + this.V * this.beta)
                * (this.nd[m][k] + this.alpha) / (this.ndsum[m] + this.K * this.alpha);
        }
        for (var k = 1; k < p.length; k++) {
            p[k] += p[k - 1];
        }
        var u = Math.random() * p[this.K - 1];
        for (topic = 0; topic < p.length; topic++) {
            if (u < p[topic])
                break;
        }
        this.nw[this.documents[m][n]][topic]++;
        this.nd[m][topic]++;
        this.nwsum[topic]++;
        this.ndsum[m]++;
        return topic;
    }

	this.updateParams =function () {
        for (var m = 0; m < this.documents.length; m++) {
            for (var k = 0; k < this.K; k++) {
                this.thetasum[m][k] += (this.nd[m][k] + this.alpha) / (this.ndsum[m] + this.K * this.alpha);
            }
        }
        for (var k = 0; k < this.K; k++) {
            for (var w = 0; w < this.V; w++) {
                this.phisum[k][w] += (this.nw[w][k] + this.beta) / (this.nwsum[k] + this.V * this.beta);
            }
        }
        this.numstats++;
    }

	this.getTheta = function() {
        var theta = new Array(); for(var i=0;i<this.documents.length;i++) theta[i] = new Array();
        if (this.SAMPLE_LAG > 0) {
            for (var m = 0; m < this.documents.length; m++) {
                for (var k = 0; k < this.K; k++) {
                    theta[m][k] = this.thetasum[m][k] / this.numstats;
                }
            }
        } else {
            for (var m = 0; m < this.documents.length; m++) {
                for (var k = 0; k < this.K; k++) {
                    theta[m][k] = (this.nd[m][k] + this.alpha) / (this.ndsum[m] + this.K * this.alpha);
                }
            }
        }
        return theta;
    }

	this.getPhi = function () {
        var phi = new Array(); for(var i=0;i<this.K;i++) phi[i] = new Array();
        if (this.SAMPLE_LAG > 0) {
            for (var k = 0; k < this.K; k++) {
                for (var w = 0; w < this.V; w++) {
                    phi[k][w] = this.phisum[k][w] / this.numstats;
                }
            }
        } else {
            for (var k = 0; k < this.K; k++) {
                for (var w = 0; w < this.V; w++) {
                    phi[k][w] = (this.nw[w][k] + this.beta) / (this.nwsum[k] + this.V * this.beta);
                }
            }
        }
        return phi;
    }
}

module.exports = process;
<commit_msg>Support for Latin characters<commit_after>var stem = require('stem-porter');
require('./stopwords.js');

//
// Based on javascript implementation https://github.com/awaisathar/lda.js
// Original code based on http://www.arbylon.net/projects/LdaGibbsSampler.java
//
var process = function(sentences, numberOfTopics, numberOfTermsPerTopic, alphaValue, betaValue) {
	// The result will consist of topics and their included terms [[{"term":"word1", "probability":0.065}, {"term":"word2", "probability":0.047}, ... ], [{"term":"word1", "probability":0.085}, {"term":"word2", "probability":0.024}, ... ]].
	var result = [];
	// Index-encoded array of sentences, with each row containing the indices of the words in the vocabulary.
	var documents = new Array();
	// Hash of vocabulary words and the count of how many times each word has been seen.
	var f = {};
	// Vocabulary of unique words (porter stemmed).
	var vocab=new Array();
	// Vocabulary of unique words in their original form.
	var vocabOrig = {};

	for(var i=0;i<sentences.length;i++) {
		if (sentences[i]=="") continue;
		documents[i] = new Array();

		var words = sentences[i].split(/[\s,\"]+/);

		if(!words) continue;
		for(var wc=0;wc<words.length;wc++) {
			var w=words[wc].toLowerCase().replace(/[^a-z\'A-Z0-9\u00C0-\u00ff ]+/g, '');
			var wStemmed = stem(w);
			if (w=="" || !wStemmed || w.length==1 || stop_words.indexOf(w.replace("'", "")) > -1 || stop_words.indexOf(wStemmed) > -1 || w.indexOf("http")==0) continue;
			if (f[wStemmed]) {
				f[wStemmed]=f[wStemmed]+1;
			}
			else if(wStemmed) {
				f[wStemmed]=1;
				vocab.push(wStemmed);
				vocabOrig[wStemmed] = w;
			};

			documents[i].push(vocab.indexOf(wStemmed));
		}
	}

	var V = vocab.length;
	var M = documents.length;
	var K = parseInt(numberOfTopics);
	var alpha = alphaValue || 0.1;  // per-document distributions over topics
	var beta = betaValue || .01;  // per-topic distributions over words

	lda.configure(documents,V,10000, 2000, 100, 10);
	lda.gibbs(K, alpha, beta);

	var theta = lda.getTheta();
	var phi = lda.getPhi();

	var text = '';

	//topics
	var topTerms=numberOfTermsPerTopic;
	for (var k = 0; k < phi.length; k++) {
		var things = new Array();
		for (var w = 0; w < phi[k].length; w++) {
			 things.push(""+phi[k][w].toPrecision(2)+"_"+vocab[w] + "_" + vocabOrig[vocab[w]]);
		}
		things.sort().reverse();
		//console.log(things);
		if(topTerms>vocab.length) topTerms=vocab.length;

		//console.log('Topic ' + (k + 1));
		var row = [];

		for (var t = 0; t < topTerms; t++) {
			var topicTerm=things[t].split("_")[2];
			var prob=parseInt(things[t].split("_")[0]*100);
			if (prob<2) continue;

			//console.log('Top Term: ' + topicTerm + ' (' + prob + '%)');

			var term = {};
			term.term = topicTerm;
			term.probability = parseFloat(things[t].split("_")[0]);
			row.push(term);
		}

		result.push(row);
	}

	return result;
}

function makeArray(x) {
	var a = new Array();
	for (var i=0;i<x;i++)  {
		a[i]=0;
	}
	return a;
}

function make2DArray(x,y) {
	var a = new Array();
	for (var i=0;i<x;i++)  {
		a[i]=new Array();
		for (var j=0;j<y;j++)
			a[i][j]=0;
	}
	return a;
}

var lda = new function() {
	var documents,z,nw,nd,nwsum,ndsum,thetasum,phisum,V,K,alpha,beta;
    var THIN_INTERVAL = 20;
    var BURN_IN = 100;
    var ITERATIONS = 1000;
    var SAMPLE_LAG;
    var dispcol = 0;
	var numstats=0;
	this.configure = function (docs,v,iterations,burnIn,thinInterval,sampleLag) {
        this.ITERATIONS = iterations;
        this.BURN_IN = burnIn;
        this.THIN_INTERVAL = thinInterval;
        this.SAMPLE_LAG = sampleLag;
		this.documents = docs;
		this.V = v;
		this.dispcol=0;
		this.numstats=0;
    }
	this.initialState = function (K) {
        var i;
        var M = this.documents.length;
        this.nw = make2DArray(this.V,K);
        this.nd = make2DArray(M,K);
        this.nwsum = makeArray(K);
        this.ndsum = makeArray(M);
        this.z = new Array();	for (i=0;i<M;i++) this.z[i]=new Array();
        for (var m = 0; m < M; m++) {
            var N = this.documents[m].length;
            this.z[m] = new Array();
            for (var n = 0; n < N; n++) {
                var topic = parseInt(""+(Math.random() * K));
                this.z[m][n] = topic;
                this.nw[this.documents[m][n]][topic]++;
                this.nd[m][topic]++;
                this.nwsum[topic]++;
            }
            this.ndsum[m] = N;
        }
    }

	this.gibbs = function (K,alpha,beta) {
		var i;
        this.K = K;
        this.alpha = alpha;
        this.beta = beta;
        if (this.SAMPLE_LAG > 0) {
            this.thetasum = make2DArray(this.documents.length,this.K);
            this.phisum = make2DArray(this.K,this.V);
            this.numstats = 0;
        }
        this.initialState(K);
        //document.write("Sampling " + this.ITERATIONS
         //   + " iterations with burn-in of " + this.BURN_IN + " (B/S="
         //   + this.THIN_INTERVAL + ").
");
        for (i = 0; i < this.ITERATIONS; i++) {
			for (var m = 0; m < this.z.length; m++) {
                for (var n = 0; n < this.z[m].length; n++) {
			        var topic = this.sampleFullConditional(m, n);
					this.z[m][n] = topic;
                }
            }
            if ((i < this.BURN_IN) && (i % this.THIN_INTERVAL == 0)) {
				//document.write("B");
                this.dispcol++;
            }
            if ((i > this.BURN_IN) && (i % this.THIN_INTERVAL == 0)) {
                //document.write("S");
                this.dispcol++;
            }
            if ((i > this.BURN_IN) && (this.SAMPLE_LAG > 0) && (i % this.SAMPLE_LAG == 0)) {
                this.updateParams();
				//document.write("|");
                if (i % this.THIN_INTERVAL != 0)
                    this.dispcol++;
            }
            if (this.dispcol >= 100) {
				//document.write("*
");
                this.dispcol = 0;
            }
        }
    }

	this.sampleFullConditional = function(m,n) {
        var topic = this.z[m][n];
        this.nw[this.documents[m][n]][topic]--;
        this.nd[m][topic]--;
        this.nwsum[topic]--;
        this.ndsum[m]--;
        var p = makeArray(this.K);
        for (var k = 0; k < this.K; k++) {
            p[k] = (this.nw[this.documents[m][n]][k] + this.beta) / (this.nwsum[k] + this.V * this.beta)
                * (this.nd[m][k] + this.alpha) / (this.ndsum[m] + this.K * this.alpha);
        }
        for (var k = 1; k < p.length; k++) {
            p[k] += p[k - 1];
        }
        var u = Math.random() * p[this.K - 1];
        for (topic = 0; topic < p.length; topic++) {
            if (u < p[topic])
                break;
        }
        this.nw[this.documents[m][n]][topic]++;
        this.nd[m][topic]++;
        this.nwsum[topic]++;
        this.ndsum[m]++;
        return topic;
    }

	this.updateParams =function () {
        for (var m = 0; m < this.documents.length; m++) {
            for (var k = 0; k < this.K; k++) {
                this.thetasum[m][k] += (this.nd[m][k] + this.alpha) / (this.ndsum[m] + this.K * this.alpha);
            }
        }
        for (var k = 0; k < this.K; k++) {
            for (var w = 0; w < this.V; w++) {
                this.phisum[k][w] += (this.nw[w][k] + this.beta) / (this.nwsum[k] + this.V * this.beta);
            }
        }
        this.numstats++;
    }

	this.getTheta = function() {
        var theta = new Array(); for(var i=0;i<this.documents.length;i++) theta[i] = new Array();
        if (this.SAMPLE_LAG > 0) {
            for (var m = 0; m < this.documents.length; m++) {
                for (var k = 0; k < this.K; k++) {
                    theta[m][k] = this.thetasum[m][k] / this.numstats;
                }
            }
        } else {
            for (var m = 0; m < this.documents.length; m++) {
                for (var k = 0; k < this.K; k++) {
                    theta[m][k] = (this.nd[m][k] + this.alpha) / (this.ndsum[m] + this.K * this.alpha);
                }
            }
        }
        return theta;
    }

	this.getPhi = function () {
        var phi = new Array(); for(var i=0;i<this.K;i++) phi[i] = new Array();
        if (this.SAMPLE_LAG > 0) {
            for (var k = 0; k < this.K; k++) {
                for (var w = 0; w < this.V; w++) {
                    phi[k][w] = this.phisum[k][w] / this.numstats;
                }
            }
        } else {
            for (var k = 0; k < this.K; k++) {
                for (var w = 0; w < this.V; w++) {
                    phi[k][w] = (this.nw[w][k] + this.beta) / (this.nwsum[k] + this.V * this.beta);
                }
            }
        }
        return phi;
    }
}

module.exports = process;
<|endoftext|>


<commit_before>"use strict";

Object.defineProperty(exports, "__esModule", {
	value: true
});
exports.merge = undefined;
exports.isMap = isMap;
exports.default = exports.map = map;
exports.put = set;
exports.set = set;
exports.keys = keys;
exports.contains = contains;
exports.size = size;
exports.forEachEntry = forEachEntry;
exports.entry = entry;
exports.get = get;
exports.remove = remove;

var _ohamt = require("ohamt");

var ohamt = _interopRequireWildcard(_ohamt);

var _seq = require("./seq");

var _util = require("./util");

var _error = require("./error");

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

//import { eq } from "./op";

var OrderedMap = ohamt.empty.constructor;

OrderedMap.prototype.__is_Map = true;

OrderedMap.prototype._type = 6;

OrderedMap.prototype["@@transducer/init"] = function () {
	return ohamt.empty;
};

OrderedMap.prototype["@@transducer/step"] = function (m, kv) {
	return m.append(kv[0], kv[1]);
};

OrderedMap.prototype["@@transducer/result"] = function (m) {
	return m;
};

OrderedMap.prototype.call = function($,$k,$v) {
	const len = arguments.length;
	if(len == 1) return get(this,$k);
	if(len == 2) return set(this,$k,$v);
};

function isMap(maybe) {
	return !!(maybe && maybe.__is_Map);
}
module.exports.fromEntries = fromEntries;
module.exports.Map = OrderedMap;
function fromEntries(entries) {
	var m = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _create();

	m = m.beginMutation();
	var _iteratorNormalCompletion = true;
	var _didIteratorError = false;
	var _iteratorError = undefined;

	try {
		for (var _iterator = entries[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
			var kv = _step.value;
			m = m.set(kv[0], kv[1]);
		}
	} catch (err) {
		_didIteratorError = true;
		_iteratorError = err;
	} finally {
		try {
			if (!_iteratorNormalCompletion && _iterator.return) {
				_iterator.return();
			}
		} finally {
			if (_didIteratorError) {
				throw _iteratorError;
			}
		}
	}

	return m.endMutation();
}

var _create = function _create() {
	return ohamt.make({
		keyEq: function keyEq(x, y) {
			return x instanceof Object && "eq" in x ? x.eq(y) : x === y;
		}
	});
};

function map() {
	for (var _len = arguments.length, a = Array(_len), _key = 0; _key < _len; _key++) {
		a[_key] = arguments[_key];
	}

	var l = a.length;
	if (l === 0) {
		return (0, _seq.seq)(_create());
	}
	if (l == 1) {
		var s = a[0];
		if ((0, _seq.isSeq)(s)) return merge(s);
		if (isMap(s)) return s;
		if(Array.isArray(s)) return fromEntries(s);
		if ((0, _util.isObject)(s)) return fromEntries(Object.entries(s));
		// TODO VNode conversion + detect tuple
		//if ((0, _access.map)()(s)) return s.toMap();
		return (0, _error.error)("XXX", "Not a map or tuple");
	}
	// expect a sequence of maps or each argument to be a map
	return merge((0, _seq.seq)(a.map(function (x) {
		return (0, _seq.seq)(x);
	})).concatAll());
}

var merge = exports.merge = function merge($m) {
	if ($m === undefined) return (0, _error.error)("XPTY0004");
	// assume a sequence of vectors
	return $m.reduce(function (pre, cur) {
		// TODO force persistent cx
		//if ((0, _access.map)()(cur)) cur = cur.toMap();
		if (!isMap(cur)) {
			return (0, _error.error)("XPTY0004", "One of the items for map:merge is not a map.");
		}
		return fromEntries(cur, pre);
	}, _create());
};

function set($m, $k, $v) {
	return _seq.forEach((0, _seq.exactlyOne)($m),function (m) {
		return _seq.forEach((0, _seq.exactlyOne)($k),function (k) {
			return m.set(k, $v);
		});
	});
}

function keys($m) {
	return _seq.forEach(_seq.exactlyOne($m),m => {
		return m.keys();
	});
}

function contains($m, $k) {
	return _seq.forEach(_seq.exactlyOne($m),m => {
		return _seq.forEach(_seq.exactlyOne($k), k => {
			return  m.has(k);
		});
	});
}

function size($m) {
	return (0, _seq.exactlyOne)($m).map(function (m) {
		return m.count();
	});
}

function forEachEntry($m, $fn) {
	return _seq.forEach((0, _seq.exactlyOne)($fn),function (fn) {
		return _seq.forEach((0, _seq.exactlyOne)($m),function (m) {
			return _seq.forEach(_seq.from(m.entries()),function([k,v]) {
				return fn(k, v);
			});
		});
	});
}

function entry($k, $v) {
	// TODO template errors
	return _seq.forEach($k,function (k) {
		var m = _create();
		return m.set(k,$v);
	});
}

function get($m, $k) {
	return _seq.forEach((0, _seq.exactlyOne)($m),function (m) {
		return _seq.forEach((0, _seq.exactlyOne)($k),function (k) {
			const v = m.get(k);
			return _util.isUndef(v) ? _seq.seq() : v;
		});
	});
}

function remove($m, $k) {
	return _seq.forEach((0, _seq.exactlyOne)($m),function (m) {
		return _seq.forEach((0, _seq.exactlyOne)($k),function (k) {
			return m.delete(k);
		});
	});
}
<commit_msg>regen<commit_after>"use strict";

Object.defineProperty(exports, "__esModule", {
	value: true
});
exports.default = exports.put = exports.fromEntries = undefined;

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

//import { map } from "./access";

exports.isMap = isMap;
exports.map = map;
exports.merge = merge;
exports.set = set;
exports.keys = keys;
exports.contains = contains;
exports.size = size;
exports.forEachEntry = forEachEntry;
exports.entry = entry;
exports.get = get;
exports.remove = remove;

var _ohamt = require("ohamt");

var ohamt = _interopRequireWildcard(_ohamt);

var _seq = require("./seq");

var _util = require("./util");

var _error = require("./error");

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

//import { eq } from "./op";

var OrderedMap = ohamt.empty.constructor;

OrderedMap.prototype.__is_Map = true;

OrderedMap.prototype._type = 6;

OrderedMap.prototype["@@transducer/init"] = function () {
	return ohamt.empty;
};

OrderedMap.prototype["@@transducer/step"] = function (m, kv) {
	return m.append(kv[0], kv[1]);
};

OrderedMap.prototype["@@transducer/result"] = function (m) {
	return m;
};

OrderedMap.prototype.call = function ($, $k, $v) {
	var len = arguments.length;
	if (len == 2) return get(this, $k);
	if (len == 3) return set(this, $k, $v);
	return this;
};

function isMap(maybe) {
	return !!(maybe && maybe.__is_Map);
}

var fromEntries = exports.fromEntries = function fromEntries(entries) {
	var m = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _create();

	m = m.beginMutation();
	var _iteratorNormalCompletion = true;
	var _didIteratorError = false;
	var _iteratorError = undefined;

	try {
		for (var _iterator = entries[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
			var _ref = _step.value;

			var _ref2 = _slicedToArray(_ref, 2);

			var k = _ref2[0];
			var v = _ref2[1];
			m = m.set(k, v);
		}
	} catch (err) {
		_didIteratorError = true;
		_iteratorError = err;
	} finally {
		try {
			if (!_iteratorNormalCompletion && _iterator.return) {
				_iterator.return();
			}
		} finally {
			if (_didIteratorError) {
				throw _iteratorError;
			}
		}
	}

	return m.endMutation();
};

var _create = function _create() {
	return ohamt.make({
		keyEq: function keyEq(x, y) {
			return x instanceof Object && "eq" in x ? x.eq(y) : x === y;
		}
	});
};

function map() {
	for (var _len = arguments.length, a = Array(_len), _key = 0; _key < _len; _key++) {
		a[_key] = arguments[_key];
	}

	var l = a.length;
	if (l === 0) {
		return _create();
	}
	if (l == 1) {
		var s = a[0];
		if ((0, _seq.isSeq)(s)) return merge(s);
		if (isMap(s)) return s;
		if ((0, _util.isObject)(s)) return fromEntries(Object.entries(s));
		if (Array.isArray(s)) return fromEntries(s);
		// TODO VNode conversion + detect tuple
		//if ((0, _access.map)()(s)) return s.toMap();
		return (0, _error.error)("XXX", "Not a map or tuple");
	}
	// expect a sequence of maps or each argument to be a map
	return merge((0, _seq.from)(a.map(function (x) {
		return (0, _seq.isSeq)(x) ? x : (0, _seq.of)(x);
	})).concatAll());
}

function merge($m) {
	if ($m === undefined) return (0, _error.error)("XPTY0004");
	// assume a sequence of vectors
	return $m.reduce(function (pre, cur) {
		// TODO force persistent cx
		//if ((0, _access.map)()(cur)) cur = cur.toMap();
		if (!isMap(cur)) {
			return (0, _error.error)("XPTY0004", "One of the items for map:merge is not a map.");
		}
		return fromEntries(cur, pre);
	}, _create());
}

var _checked = function _checked($m, fn) {
	if ($m === undefined) return (0, _error.error)("XPTY0004");
	return (0, _seq.forEach)((0, _seq.exactlyOne)($m), function (m) {
		return !isMap(m) ? (0, _error.error)("XPTY0004", "The provided item is not a map.") : fn(m);
	});
};

function set($m, $k, $v) {
	return _checked($m, function (m) {
		return (0, _seq.forEach)((0, _seq.exactlyOne)($k), function (k) {
			return m.set(k, $v);
		});
	});
}

function keys($m) {
	return _checked($m, function (m) {
		return m.keys();
	});
}

function contains($m, $k) {
	return _checked($m, function (m) {
		return (0, _seq.forEach)((0, _seq.exactlyOne)($k), function (k) {
			return m.has(k);
		});
	});
}

function size($m) {
	return _checked($m, function (m) {
		return m.count();
	});
}

function forEachEntry($m, $fn) {
	return _checked($m, function (m) {
		return (0, _seq.forEach)((0, _seq.exactlyOne)($fn), function (fn) {
			return (0, _seq.forEach)((0, _seq.from)(m.entries()), function (_ref3) {
				var _ref4 = _slicedToArray(_ref3, 2),
				    k = _ref4[0],
				    v = _ref4[1];

				return fn(k, v);
			});
		});
	});
}

function entry($k, $v) {
	// TODO template errors
	return (0, _seq.forEach)($k, function (k) {
		return _create().set(k, $v);
	});
}

function get($m, $k) {
	return _checked($m, function (m) {
		return (0, _seq.forEach)((0, _seq.exactlyOne)($k), function (k) {
			return m.has(k) ? m.get(k) : (0, _seq.seq)();
		});
	});
}

function remove($m, $k) {
	return _checked($m, function (m) {
		return (0, _seq.forEach)((0, _seq.exactlyOne)($k), function (k) {
			return m.delete(k);
		});
	});
}

exports.put = set;
exports.default = map;
<|endoftext|>


<commit_before>/**
 * MWS request API which takes action, and paramaters for MWS request
 * For information on using, please see examples folder.
 *
 * @author Jakub Gabčo, Zdeněk Pečínka
 */
var xml = require('pixl-xml');
//var xml2js = require('xml2js');
var crypto = require('crypto');
var qs = require("querystring");
var https = require("https");

var _ = require('underscore');

/* Needed Api String */
var url = "mws.amazonservices.com";
var endpoint = {
    'Products': {
        'type': '/Products/',
        'version': '2011-10-01'
    },
    "Sellers": {
        type: "/Sellers/",
        version: "2011-07-01"
    },
    'Orders': {
        'type': /Orders/,
        'version': '2013-09-01'
    },
    'Feeds': {
        'type': '/',
        'version': ''
    },
    'Reports': {
        'type': '/Reports/',
        'version': '2009-01-01'
    }
};
module.exports = {

    /**
     * @description Create POST request to specific endpoint defined by type in paramaters.
     *
     * @param {Object} params Schema for all paramaters needed by POST request
     * @param {Function} cb If XML is true return raw xml, otherwise it converts to js object
     */

    createRequest: function (params, cb) {
        var tstamp = new Date().toISOString();
        tstamp = tstamp.replace(/\..+/, '') + 'Z';
        params["timestamp"] = tstamp;
        var paramObj = {
            'Action': params.action,
            'SellerId': params.config.SellerId,
            'SignatureVersion': '2',
            'SignatureMethod': 'HmacSHA256',
            'Timestamp': tstamp,
            'Version': endpoint[params.type].version,
            'AWSAccessKeyId': params.config.AWSAccessKeyId
        };

        // Combine core parameters with require parameters
        _.extend(paramObj, params.parameters);

        paramObj['Signature'] = this.generateSignature(params.config, paramObj, params.type);

        var body = qs.stringify(paramObj);

        var headers = {
            'Host': url,
            'User-Agent': 'mws-jsr/0.1.0 (Language=Javascript)',
            'Content-Type': 'text/xml'
        };

        var options = {
            host: url,
            port: 443,
            path: endpoint[params.type].type + endpoint[params.type].version + "?" + body,
            method: "POST",
            headers: headers
        };

        // Make the initial request and define callbacks
        var req = https.request(options, function (res) {
            var data = '';
            // Append each incoming chunk to data variable
            res.addListener('data', function (chunk) {
                data += chunk.toString();
            });
            // When response is complete, parse the XML and pass it to callback
            res.addListener('end', function () {
                //var parser = new xml2js.Parser();
                if (params.xml === false) {
                    var jsonData = xml.parse(data);
                    cb(null, jsonData);
                    //parser.parseString(data, function (err, result) {
                    //    cb(err, result);
                    //});
                }
                else {
                    cb(null, data);
                }
            });
        });
        req.write(body);
        req.end();
    },

    /**
     * @abstract Generates signature for request
     * @description Create HMAC sha256 of secret key and combine it with queryString. Then convert it to base64.
     * @param {Object} config
     * @param {Object} params
     * @param {String} ep Endpoint
     * @see http://docs.developer.amazonservices.com/en_CA/dev_guide/DG_ClientLibraries.html#DG_OwnClientLibrary__Signatures
     */
    generateSignature: function (config, params, ep) {
        hmac = crypto.createHmac("sha256", config.SecretKey);

        var keys = [];
        for (var key in params) {
            keys.push(key);
        }
        keys.sort();
        var sorted = {};
        for (var i = 0; i < keys.length; i++) {
            sorted[keys[i]] = params[keys[i]];
        }
        sorted.Timestamp = params.Timestamp.replace(/:/g, "%3A");
        joinedParameters = [];
        for (var key in sorted) {
            if (/.*Date.*/g.test(key)) {
                sorted[key] = sorted[key].replace(/:/g, "%3A");
            }
            joinedParameters.push(key + "=" + sorted[key]);
        }
        var strParams = joinedParameters.join('&');
        var queryRequest = ['POST', url, endpoint[ep].type + endpoint[ep].version, strParams].join('\n');

        var stringToSign = queryRequest;
        stringToSign = stringToSign.replace(/'/g, "%27");
        stringToSign = stringToSign.replace(/\*/g, "%2A");
        stringToSign = stringToSign.replace(/\(/g, "%28");
        stringToSign = stringToSign.replace(/\)/g, "%29");
        stringToSign = stringToSign.replace(/ /g, "%20");
        queryRequest = stringToSign;
        var result = hmac.update(queryRequest).digest("base64");

        return result;

    }

};
<commit_msg>Pull the URL to config to support global MWS endpoints<commit_after>/**
 * MWS request API which takes action, and paramaters for MWS request
 * For information on using, please see examples folder.
 *
 * @author Jakub Gabčo, Zdeněk Pečínka
 */
var xml = require('pixl-xml');
//var xml2js = require('xml2js');
var crypto = require('crypto');
var qs = require("querystring");
var https = require("https");

var _ = require('underscore');

/* Needed Api String */
var endpoint = {
    'Products': {
        'type': '/Products/',
        'version': '2011-10-01'
    },
    "Sellers": {
        type: "/Sellers/",
        version: "2011-07-01"
    },
    'Orders': {
        'type': /Orders/,
        'version': '2013-09-01'
    },
    'Feeds': {
        'type': '/',
        'version': ''
    },
    'Reports': {
        'type': '/Reports/',
        'version': '2009-01-01'
    }
};
module.exports = {

    /**
     * @description Create POST request to specific endpoint defined by type in paramaters.
     *
     * @param {Object} params Schema for all paramaters needed by POST request
     * @param {Function} cb If XML is true return raw xml, otherwise it converts to js object
     */

    createRequest: function (params, cb) {
        var tstamp = new Date().toISOString();
        tstamp = tstamp.replace(/\..+/, '') + 'Z';
        params["timestamp"] = tstamp;
        var paramObj = {
            'Action': params.action,
            'SellerId': params.config.SellerId,
            'SignatureVersion': '2',
            'SignatureMethod': 'HmacSHA256',
            'Timestamp': tstamp,
            'Version': endpoint[params.type].version,
            'AWSAccessKeyId': params.config.AWSAccessKeyId
        };

        // Combine core parameters with require parameters
        _.extend(paramObj, params.parameters);

        paramObj['Signature'] = this.generateSignature(params.config, paramObj, params.type);

        var body = qs.stringify(paramObj);

        var headers = {
            'Host': params.config["AmazonServicesURL"],
            'User-Agent': 'mws-jsr/0.1.0 (Language=Javascript)',
            'Content-Type': 'text/xml'
        };

        var options = {
            host: params.config["AmazonServicesURL"],
            port: 443,
            path: endpoint[params.type].type + endpoint[params.type].version + "?" + body,
            method: "POST",
            headers: headers
        };

        // Make the initial request and define callbacks
        var req = https.request(options, function (res) {
            var data = '';
            // Append each incoming chunk to data variable
            res.addListener('data', function (chunk) {
                data += chunk.toString();
            });
            // When response is complete, parse the XML and pass it to callback
            res.addListener('end', function () {
                //var parser = new xml2js.Parser();
                if (params.xml === false) {
                    var jsonData = xml.parse(data);
                    cb(null, jsonData);
                    //parser.parseString(data, function (err, result) {
                    //    cb(err, result);
                    //});
                }
                else {
                    cb(null, data);
                }
            });
        });
        req.write(body);
        req.end();
    },

    /**
     * @abstract Generates signature for request
     * @description Create HMAC sha256 of secret key and combine it with queryString. Then convert it to base64.
     * @param {Object} config
     * @param {Object} params
     * @param {String} ep Endpoint
     * @see http://docs.developer.amazonservices.com/en_CA/dev_guide/DG_ClientLibraries.html#DG_OwnClientLibrary__Signatures
     */
    generateSignature: function (config, params, ep) {
        hmac = crypto.createHmac("sha256", config.SecretKey);

        var keys = [];
        for (var key in params) {
            keys.push(key);
        }
        keys.sort();
        var sorted = {};
        for (var i = 0; i < keys.length; i++) {
            sorted[keys[i]] = params[keys[i]];
        }
        sorted.Timestamp = params.Timestamp.replace(/:/g, "%3A");
        joinedParameters = [];
        for (var key in sorted) {
            if (/.*Date.*/g.test(key)) {
                sorted[key] = sorted[key].replace(/:/g, "%3A");
            }
            joinedParameters.push(key + "=" + sorted[key]);
        }
        var strParams = joinedParameters.join('&');
        var queryRequest = ['POST', config["AmazonServicesURL"], endpoint[ep].type + endpoint[ep].version, strParams].join('\n');

        var stringToSign = queryRequest;
        stringToSign = stringToSign.replace(/'/g, "%27");
        stringToSign = stringToSign.replace(/\*/g, "%2A");
        stringToSign = stringToSign.replace(/\(/g, "%28");
        stringToSign = stringToSign.replace(/\)/g, "%29");
        stringToSign = stringToSign.replace(/ /g, "%20");
        queryRequest = stringToSign;
        var result = hmac.update(queryRequest).digest("base64");

        return result;

    }

};
<|endoftext|>


<commit_before>var nbv = (function() {
    "use strict";

    var d = document;
    var st = {}; // settings

    function render_ipynb(obj, target, settings) {
        if (!window.marked || !window.Prism) {
            console.error('expecting libraries marked.js and Prism.js to be present');
            return;
        }
        st = settings || {};

        st.nbformat = obj.nbformat;
        if (st.nbformat < 3) {
            console.error('Format of the notebook too old');
            return;
        }

        if (st.nbformat > 3)
            st.lang = obj.metadata.kernelspec.language;

        // wipe all inner elements of our target
        while (target.firstChild) {
            target.removeChild(target.firstChild);
        }
        var t = d.createElement('div');
        t.setAttribute('style', [
            'max-width: 960px',
            'border: 1px solid #ccc',
            'margin: 1em auto',
            'padding: 1.5em 1.5em 1.5em 7em',
            'background-color: white',
            'box-shadow: 0 0 10px #ccc'
            ].join(';'));
        target.appendChild(t);
        st.target = t;

        // v4 has cells directly in the object, v3 had a list of
        // worksheets, each with a list of cells
        var cells = obj.cells || function() {
            var ret = [];
            for (var j=0; j < obj.worksheets.length; j++) {
                ret = ret.concat(obj.worksheets[j].cells);
            }
            return ret;
        }();

        for (var j=0; j < cells.length; j++) {
            var tc = cells[j];

            var cell = d.createElement('div');
            cell.setAttribute('style', 'padding-bottom: .5em;');

            var dm = d.createElement('div'); // empty div as a fallback

            switch (tc.cell_type) {
                case 'code':
                    dm = handle_code(tc);
                    break;
                case 'markdown':
                    dm = handle_mdown(tc);
                    break;

                default:
                    console.error('Unsupported cell type: ' + tc.cell_type);
            }
            cell.appendChild(dm);
            t.appendChild(cell);
        }

        // style all dataframes
        // hint: https://davidwalsh.name/add-rules-stylesheets
        var sheet = (function() {
            var style = document.createElement('style');
            document.head.appendChild(style);
            return style.sheet;
        })();
        sheet.insertRule('table.dataframe { border-collapse: collapse; }')
        sheet.insertRule('table.dataframe, table.dataframe td, table.dataframe th { border: none; }')
        sheet.insertRule('table.dataframe th { font-weight: bold; }')
        sheet.insertRule('table.dataframe th, table.dataframe td { padding: .4em .5em; }')
        sheet.insertRule('table.dataframe tbody { border-top: 1px solid #4c4c4c; }')
        sheet.insertRule('table.dataframe tbody th { text-align: right; }')
        sheet.insertRule('table.dataframe tbody tr:nth-child(odd) { background-color: #f5f5f5; }')
    }

    function excount(cell, tin) {
        var cc = d.createElement('span');
        cc.setAttribute('style', [
            'display: block',
            'position: absolute',
            'top: ' + (tin ? '5px' : '0 !important'),
            'left: -7.5em',
            'width: 7em',
            'font-family: monospace',
            'text-align: right',
            'color: ' + (tin ? '#303fba' : '#de4815')
            ].join(';'));
        cc.textContent = (tin ? 'In': 'Out') + ' [' +
            ((!cell.execution_count && !cell.prompt_number) ? ' ' :
                (cell.execution_count || cell.prompt_number)) + ']:';
        return cc;
    }

    // receives cell, outputs DOM
    function handle_code(cell) {
        var el = d.createElement('div'); // container for it all
        el.style.position = 'relative'; // for excount positioning

        el.appendChild(excount(cell, true));

        var pre = d.createElement('pre');
        // because code may also be within markdown:
        pre.setAttribute('style', [
            'background: #f7f7f7',
            'border: 1px solid #cfcfcf',
            'padding: .4em',
            'margin-bottom: 0',
            'margin-top: 0',
            'border-radius: 2px',
            'min-height: .85em'
            ].join(';'));
        var code = d.createElement('code');

        // if (st.hasOwnProperty('lang'))
        code.setAttribute('class', 'language-' + st.lang || cell.language);

        // no need to join on '\n' - newlines are in the code already
        // .source for v4, .input for v3
        var raw_source = (cell.source || cell.input)
        code.textContent = typeof raw_source === 'string' ? raw_source : raw_source.join('');

        pre.appendChild(code);
        el.appendChild(pre);
        Prism.highlightElement(code);

        // outputs now
        var outp = d.createElement('div');
        outp.style.margin = '1em 0 .5em 0';

        for (var j=0; j < cell.outputs.length; j++) {
            var dm = d.createElement('div'); // wrapper
            dm.style.position = 'relative'; // for excount positioning
            var dt = cell.outputs[j];
            if (dt.output_type == 'execute_result')
                dm.appendChild(excount(dt, false));

            switch (dt.output_type) {
                case 'execute_result':
                    dm.appendChild(handle_cell_output(dt));
                    break;
                case 'stream':
                    dm.appendChild(handle_stream_output(dt));
                    break;
                case 'pyerr': // v3
                case 'error': // v4
                    dm.appendChild(handle_error_cell(dt));
                    break;
                case 'display_data':
                    if (st.nbformat > 3) {
                        dm.appendChild(handle_cell_output(dt));
                        break;
                    }
                    // if 3, fall through to pyout
                case 'pyout':
                    // legacy (v3)
                    dm.appendChild(handle_pyout(dt));
                    break;
                default:
                    console.error('Not supported output_type: ' +
                        cell.outputs[j].output_type);
            }

            outp.appendChild(dm);
        }
        el.appendChild(outp);

        return el;
    }

    function handle_cell_output(dt) {
        var el = d.createElement('div');
        el.style.minHeight = '1em';

        // individual outputs
        var fmts = Object.keys(dt.data);
        for (var j=0; j < fmts.length; j++) {
            var fmt = fmts[j];
            var dm = d.createElement('div');
            switch (fmt) {
                case 'text/plain':
                    // text/plain might be just a fallback here
                    // fixed #10
                    if (fmts.includes('text/html')) continue;

                    dm = d.createElement('pre');
                    dm.style.margin = 0;
                    dm.style.whiteSpace = 'pre-wrap';
                    dm.textContent = typeof dt.data[fmt] === 'string' ? dt.data[fmt] : dt.data[fmt].join('');
                    break;

                case 'text/html':
                    dm.innerHTML = dt.data[fmt].join('');

                    // we may have generated some HTML tables we need to style
                    var dfs = dm.getElementsByClassName('dataframe');
                    for (var k = 0; k < dfs.length; k++) {
                        if (dfs[k].classList.contains('dataframe')) {
                            continue; // we style dataframes separately
                        }
                        dfs[k].setAttribute('style', [
                            'border-collapse: collapse',
                            'text-align: left'
                            // 'margin-top: 1em'
                        ].join(';'));

                        // let's style individual cells as well
                        var cl = dfs[k].querySelectorAll('td, th');
                        for (var l=0; l < cl.length; l++) {
                            cl[l].style.padding = '3px';
                        }

                    }
                    break;

                case 'image/svg+xml':
                    dm.innerHTML = dt.data[fmt].join('');
                    break;

                default:
                    if (fmt.startsWith('image/')) {
                        dm = d.createElement('img');
                        dm.setAttribute('src', 'data:' + fmt + ';base64,' +
                            ((typeof dt.data[fmt]) == 'string' ? dt.data[fmt] : dt.data[fmt].join('')));
                        dm.setAttribute('style', 'max-width: 100%'); // avoid overflow
                        // use width and height attributes supplied in metadata
                        if (fmt in dt.metadata) {
                            var metadata = dt.metadata[fmt];
                            if ('width' in metadata) {
                                dm.setAttribute('width', metadata.width);
                            }
                            if ('height' in metadata) {
                                dm.setAttribute('height', metadata.height);
                            }
                        }
                        break;
                    }
                    console.error('unexpected format: ' + fmt);
            }
            el.appendChild(dm);

        }

        return el;
    }

    function handle_error_cell(dt) {
        var cn = d.createElement('pre');
        var txt = dt.traceback.join('\n');
        // stripping ANSI colours for now https://github.com/chalk/ansi-regex/blob/master/index.js#L3
        // could use a full library at some point https://github.com/drudru/ansi_up
        txt = txt.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
        cn.textContent = txt;

        return cn;
    }

    function handle_stream_output(dt) {
        // name in v4, stream in v3
        var outt = dt.name || dt.stream; // v4 || v3; contains 'stdout' or 'stderr'

        if (!dt.hasOwnProperty('text'))
            console.error('data for stream missing');

        var cn = d.createElement('pre');
        // stderr red background, stdout is plain white
        if (outt === 'stderr') {
            cn.setAttribute('style', 'background-color: #fdd; padding: .5em; white-space: pre-wrap');
        }
        cn.textContent = dt.text.join('');

        return cn;
    }

    function handle_mdown(cell) {
        var el = d.createElement('div');
        el.innerHTML = marked(cell.source.join(''));

        return el;
    }

    // handling legacy notebooks (v3)
    function handle_pyout(cell) {
        var el = d.createElement('div');

        if (cell.hasOwnProperty('prompt_number'))
            el.appendChild(excount(cell, false));

        // var ks = Object.keys(cell)
        // text, png, jpeg, html -- supported
        // svg, latex, javascript, json, pdf, metadata -- not yet
        Object.keys(cell).forEach(function(k) {
            if (['output_type', 'prompt_number'].indexOf(k) > -1)
                return;
            var p = d.createElement('span'); // if errs
            switch (k) {
                case 'text':
                    p = d.createElement('pre');
                    p.style.margin = 0;
                    p.textContent = cell[k].join('');
                    break;
                case 'html':
                    p = d.createElement('div');
                    // guessing here, haven't seen a v3 HTML element
                    p.innerHTML = cell[k].join('');
                    break;
                case 'png':
                case 'jpeg':
                    p = d.createElement('img');
                    p.setAttribute('src', 'data:' + k + ';base64,' + cell[k]);
                    p.setAttribute('style', 'max-width: 100%'); // avoid overflow

                    break;

                default:
                    console.error('unsupported pyout format: ' + k);
            }
            el.appendChild(p);
        });

        return el;
    }

    return {
        render: render_ipynb
    };

})();
<commit_msg>kernelspec need not be present<commit_after>var nbv = (function() {
    "use strict";

    var d = document;
    var st = {}; // settings

    function render_ipynb(obj, target, settings) {
        if (!window.marked || !window.Prism) {
            console.error('expecting libraries marked.js and Prism.js to be present');
            return;
        }
        st = settings || {};

        st.nbformat = obj.nbformat;
        if (st.nbformat < 3) {
            console.error('Format of the notebook too old');
            return;
        }

        if (st.nbformat > 3) {
            st.lang = (obj.metadata.kernelspec || {'language': null}).language;
        }

        // wipe all inner elements of our target
        while (target.firstChild) {
            target.removeChild(target.firstChild);
        }
        var t = d.createElement('div');
        t.setAttribute('style', [
            'max-width: 960px',
            'border: 1px solid #ccc',
            'margin: 1em auto',
            'padding: 1.5em 1.5em 1.5em 7em',
            'background-color: white',
            'box-shadow: 0 0 10px #ccc'
            ].join(';'));
        target.appendChild(t);
        st.target = t;

        // v4 has cells directly in the object, v3 had a list of
        // worksheets, each with a list of cells
        var cells = obj.cells || function() {
            var ret = [];
            for (var j=0; j < obj.worksheets.length; j++) {
                ret = ret.concat(obj.worksheets[j].cells);
            }
            return ret;
        }();

        for (var j=0; j < cells.length; j++) {
            var tc = cells[j];

            var cell = d.createElement('div');
            cell.setAttribute('style', 'padding-bottom: .5em;');

            var dm = d.createElement('div'); // empty div as a fallback

            switch (tc.cell_type) {
                case 'code':
                    dm = handle_code(tc);
                    break;
                case 'markdown':
                    dm = handle_mdown(tc);
                    break;

                default:
                    console.error('Unsupported cell type: ' + tc.cell_type);
            }
            cell.appendChild(dm);
            t.appendChild(cell);
        }

        // style all dataframes
        // hint: https://davidwalsh.name/add-rules-stylesheets
        var sheet = (function() {
            var style = document.createElement('style');
            document.head.appendChild(style);
            return style.sheet;
        })();
        sheet.insertRule('table.dataframe { border-collapse: collapse; }')
        sheet.insertRule('table.dataframe, table.dataframe td, table.dataframe th { border: none; }')
        sheet.insertRule('table.dataframe th { font-weight: bold; }')
        sheet.insertRule('table.dataframe th, table.dataframe td { padding: .4em .5em; }')
        sheet.insertRule('table.dataframe tbody { border-top: 1px solid #4c4c4c; }')
        sheet.insertRule('table.dataframe tbody th { text-align: right; }')
        sheet.insertRule('table.dataframe tbody tr:nth-child(odd) { background-color: #f5f5f5; }')
    }

    function excount(cell, tin) {
        var cc = d.createElement('span');
        cc.setAttribute('style', [
            'display: block',
            'position: absolute',
            'top: ' + (tin ? '5px' : '0 !important'),
            'left: -7.5em',
            'width: 7em',
            'font-family: monospace',
            'text-align: right',
            'color: ' + (tin ? '#303fba' : '#de4815')
            ].join(';'));
        cc.textContent = (tin ? 'In': 'Out') + ' [' +
            ((!cell.execution_count && !cell.prompt_number) ? ' ' :
                (cell.execution_count || cell.prompt_number)) + ']:';
        return cc;
    }

    // receives cell, outputs DOM
    function handle_code(cell) {
        var el = d.createElement('div'); // container for it all
        el.style.position = 'relative'; // for excount positioning

        el.appendChild(excount(cell, true));

        var pre = d.createElement('pre');
        // because code may also be within markdown:
        pre.setAttribute('style', [
            'background: #f7f7f7',
            'border: 1px solid #cfcfcf',
            'padding: .4em',
            'margin-bottom: 0',
            'margin-top: 0',
            'border-radius: 2px',
            'min-height: .85em'
            ].join(';'));
        var code = d.createElement('code');

        // if (st.hasOwnProperty('lang'))
        code.setAttribute('class', 'language-' + st.lang || cell.language);

        // no need to join on '\n' - newlines are in the code already
        // .source for v4, .input for v3
        var raw_source = (cell.source || cell.input)
        code.textContent = typeof raw_source === 'string' ? raw_source : raw_source.join('');

        pre.appendChild(code);
        el.appendChild(pre);
        Prism.highlightElement(code);

        // outputs now
        var outp = d.createElement('div');
        outp.style.margin = '1em 0 .5em 0';

        for (var j=0; j < cell.outputs.length; j++) {
            var dm = d.createElement('div'); // wrapper
            dm.style.position = 'relative'; // for excount positioning
            var dt = cell.outputs[j];
            if (dt.output_type == 'execute_result')
                dm.appendChild(excount(dt, false));

            switch (dt.output_type) {
                case 'execute_result':
                    dm.appendChild(handle_cell_output(dt));
                    break;
                case 'stream':
                    dm.appendChild(handle_stream_output(dt));
                    break;
                case 'pyerr': // v3
                case 'error': // v4
                    dm.appendChild(handle_error_cell(dt));
                    break;
                case 'display_data':
                    if (st.nbformat > 3) {
                        dm.appendChild(handle_cell_output(dt));
                        break;
                    }
                    // if 3, fall through to pyout
                case 'pyout':
                    // legacy (v3)
                    dm.appendChild(handle_pyout(dt));
                    break;
                default:
                    console.error('Not supported output_type: ' +
                        cell.outputs[j].output_type);
            }

            outp.appendChild(dm);
        }
        el.appendChild(outp);

        return el;
    }

    function handle_cell_output(dt) {
        var el = d.createElement('div');
        el.style.minHeight = '1em';

        // individual outputs
        var fmts = Object.keys(dt.data);
        for (var j=0; j < fmts.length; j++) {
            var fmt = fmts[j];
            var dm = d.createElement('div');
            switch (fmt) {
                case 'text/plain':
                    // text/plain might be just a fallback here
                    // fixed #10
                    if (fmts.includes('text/html')) continue;

                    dm = d.createElement('pre');
                    dm.style.margin = 0;
                    dm.style.whiteSpace = 'pre-wrap';
                    dm.textContent = typeof dt.data[fmt] === 'string' ? dt.data[fmt] : dt.data[fmt].join('');
                    break;

                case 'text/html':
                    dm.innerHTML = dt.data[fmt].join('');

                    // we may have generated some HTML tables we need to style
                    var dfs = dm.getElementsByClassName('dataframe');
                    for (var k = 0; k < dfs.length; k++) {
                        if (dfs[k].classList.contains('dataframe')) {
                            continue; // we style dataframes separately
                        }
                        dfs[k].setAttribute('style', [
                            'border-collapse: collapse',
                            'text-align: left'
                            // 'margin-top: 1em'
                        ].join(';'));

                        // let's style individual cells as well
                        var cl = dfs[k].querySelectorAll('td, th');
                        for (var l=0; l < cl.length; l++) {
                            cl[l].style.padding = '3px';
                        }

                    }
                    break;

                case 'image/svg+xml':
                    dm.innerHTML = dt.data[fmt].join('');
                    break;

                default:
                    if (fmt.startsWith('image/')) {
                        dm = d.createElement('img');
                        dm.setAttribute('src', 'data:' + fmt + ';base64,' +
                            ((typeof dt.data[fmt]) == 'string' ? dt.data[fmt] : dt.data[fmt].join('')));
                        dm.setAttribute('style', 'max-width: 100%'); // avoid overflow
                        // use width and height attributes supplied in metadata
                        if (fmt in dt.metadata) {
                            var metadata = dt.metadata[fmt];
                            if ('width' in metadata) {
                                dm.setAttribute('width', metadata.width);
                            }
                            if ('height' in metadata) {
                                dm.setAttribute('height', metadata.height);
                            }
                        }
                        break;
                    }
                    console.error('unexpected format: ' + fmt);
            }
            el.appendChild(dm);

        }

        return el;
    }

    function handle_error_cell(dt) {
        var cn = d.createElement('pre');
        var txt = dt.traceback.join('\n');
        // stripping ANSI colours for now https://github.com/chalk/ansi-regex/blob/master/index.js#L3
        // could use a full library at some point https://github.com/drudru/ansi_up
        txt = txt.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
        cn.textContent = txt;

        return cn;
    }

    function handle_stream_output(dt) {
        // name in v4, stream in v3
        var outt = dt.name || dt.stream; // v4 || v3; contains 'stdout' or 'stderr'

        if (!dt.hasOwnProperty('text'))
            console.error('data for stream missing');

        var cn = d.createElement('pre');
        // stderr red background, stdout is plain white
        if (outt === 'stderr') {
            cn.setAttribute('style', 'background-color: #fdd; padding: .5em; white-space: pre-wrap');
        }
        cn.textContent = dt.text.join('');

        return cn;
    }

    function handle_mdown(cell) {
        var el = d.createElement('div');
        el.innerHTML = marked(cell.source.join(''));

        return el;
    }

    // handling legacy notebooks (v3)
    function handle_pyout(cell) {
        var el = d.createElement('div');

        if (cell.hasOwnProperty('prompt_number'))
            el.appendChild(excount(cell, false));

        // var ks = Object.keys(cell)
        // text, png, jpeg, html -- supported
        // svg, latex, javascript, json, pdf, metadata -- not yet
        Object.keys(cell).forEach(function(k) {
            if (['output_type', 'prompt_number'].indexOf(k) > -1)
                return;
            var p = d.createElement('span'); // if errs
            switch (k) {
                case 'text':
                    p = d.createElement('pre');
                    p.style.margin = 0;
                    p.textContent = cell[k].join('');
                    break;
                case 'html':
                    p = d.createElement('div');
                    // guessing here, haven't seen a v3 HTML element
                    p.innerHTML = cell[k].join('');
                    break;
                case 'png':
                case 'jpeg':
                    p = d.createElement('img');
                    p.setAttribute('src', 'data:' + k + ';base64,' + cell[k]);
                    p.setAttribute('style', 'max-width: 100%'); // avoid overflow

                    break;

                default:
                    console.error('unsupported pyout format: ' + k);
            }
            el.appendChild(p);
        });

        return el;
    }

    return {
        render: render_ipynb
    };

})();
<|endoftext|>


<commit_before>'use strict';

// External modules
var Bluebird = require('bluebird');

// Local modules
var Support = require('./support');

module.exports = {
  bump: function (args) {
    return Bluebird
      .resolve(args)
      .tap(logWrap('Updating the changelog', Support.changelog.update))
      .tap(logWrap('Updating the package', Support.npm.updatePackage))
      .tap(logWrap('Pushing changes', Support.git.push))
      .tap(logWrap('Pushing tags', Support.git.pushTags));
  },

  release: function (args) {
    return this
      .bump(args)
      .tap(logWrap('Publishing package', Support.npm.publishPackage));
  }
};

function logWrap (s, fun) {
  return function () {
    var args = [].slice.apply(arguments);

    return Bluebird
      .resolve()
      .then(function () {
        console.log(s + ' ... ');
      })
      .then(function () {
        return fun.apply(null, args);
      })
      .then(function () {
        console.log(s + ' ... done.');
      })
      .catch(function (e) {
        console.log(s + ' ... failed:');
        console.log(e);
      });
  };
}
<commit_msg>Throw errors to stop the promise chain<commit_after>'use strict';

// External modules
var Bluebird = require('bluebird');

// Local modules
var Support = require('./support');

module.exports = {
  bump: function (args) {
    return Bluebird
      .resolve(args)
      .tap(logWrap('Updating the changelog', Support.changelog.update))
      .tap(logWrap('Updating the package', Support.npm.updatePackage))
      .tap(logWrap('Pushing changes', Support.git.push))
      .tap(logWrap('Pushing tags', Support.git.pushTags));
  },

  release: function (args) {
    return this
      .bump(args)
      .tap(logWrap('Publishing package', Support.npm.publishPackage));
  }
};

function logWrap (s, fun) {
  return function () {
    var args = [].slice.apply(arguments);

    return Bluebird
      .resolve()
      .then(function () {
        console.log(s + ' ... ');
      })
      .then(function () {
        return fun.apply(null, args);
      })
      .then(function () {
        console.log(s + ' ... done.');
      })
      .catch(function (e) {
        console.log(s + ' ... failed:');
        console.log(e);

        throw e;
      });
  };
}
<|endoftext|>


<commit_before>var git = require( '../' );

/**
 * @param {git.raw.Oid|null} rawOid
 */
var Oid = function(rawOid) {
  if(rawOid instanceof git.raw.Oid) {
    this.rawOid = rawOid;
  }
};

/**
 * @return {git.raw.Oid}
 */
Oid.prototype.getRawoid = function() {
  return this.rawOid;
};

exports.oid = Oid;
<commit_msg>Fixed typo in oid.js<commit_after>var git = require( '../' );

/**
 * @param {git.raw.Oid|null} rawOid
 */
var Oid = function(rawOid) {
  if(rawOid instanceof git.raw.Oid) {
    this.rawOid = rawOid;
  }
};

/**
 * @return {git.raw.Oid}
 */
Oid.prototype.getRawOid = function() {
  return this.rawOid;
};

exports.oid = Oid;
<|endoftext|>


<commit_before>'use strict';

var spawn = require('child_process').spawn;

/**
 * Runs given program with given cmds and calls back when finished
 * @name exports
 * @function
 * @param progName {String} name or path to program to run
 * @param cmds {Array[String]} arguments passed to the program
 * @param cb {Function} called when program is finished
 */
module.exports = function run(progName, cmds, cb) {
  var prog = spawn(progName, cmds, { stdio: 'inherit' })

  prog.on('close', function (code) {
    if (code !== 0) cb(new Error('prog ' + cmds.join(' ') + ' returned with code ' + code));
    cb();
  });
}
<commit_msg>getting rid of include warning<commit_after>'use strict';

var spawn = require('child_process').spawn;

/**
 * Runs given program with given cmds and calls back when finished
 * @name exports
 * @function
 * @param progName {String} name or path to program to run
 * @param cmds {Array[String]} arguments passed to the program
 * @param cb {Function} called when program is finished
 */
module.exports = function run(progName, cmds, cb) {
  var prog = spawn(progName, cmds, { stdio: 'inherit', env: { SASSPATH: '.' } })

  prog.on('close', function (code) {
    if (code !== 0) cb(new Error('prog ' + cmds.join(' ') + ' returned with code ' + code));
    cb();
  });
}
<|endoftext|>