UNPKG

markov-typescript

Version:

A Markov Chain library written in Typescript.

109 lines 4.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var chain_state_1 = require("./chain-state"); var fixed_queue_1 = require("./fixed-queue"); var markov_chain_items_1 = require("./markov-chain-items"); var markov_terminal_items_1 = require("./markov-terminal-items"); var utils = require("./utils"); var weighted_dictionary_1 = require("./weighted-dictionary"); var MarkovChain = (function () { /** * Initializes a new instance of MarkovChain<T>. * @param order The number of state transitions to track within the model. * @param toStrFunction Optional function used to convert keys to strings. */ function MarkovChain(order, toStrFunction) { if (order === void 0) { order = 2; } this.items = new markov_chain_items_1.MarkovChainItems(); this.terminals = new markov_terminal_items_1.MarkovTerminalItems(); if (order < 0) { throw new RangeError("Order must not be less than 0."); } this.order = order; this.toStrFunction = toStrFunction; } /** * Learns multiple sequences and their transitions. * @param items The list of sequences to learn. */ MarkovChain.prototype.learnAll = function (items) { var _this = this; items.forEach(function (item) { return _this.learn(item); }); }; /** * Learns a single sequence of elements. * @param items Lear */ MarkovChain.prototype.learn = function (items) { var _this = this; if (!items || items.length === 0) { return; } var previous = new fixed_queue_1.FixedQueue(this.order); items.forEach(function (item) { var key = new chain_state_1.ChainState(previous); _this.learnWithPrevious(key, item); previous.enqueue(item); }); var terminalKey = new chain_state_1.ChainState(previous); this.terminals.incrementValue(terminalKey, 1); }; MarkovChain.prototype.learnWithPrevious = function (previous, next) { var _this = this; var weights = this.items.getOrCreateValue(previous, function () { return new weighted_dictionary_1.WeightedDictionary(_this.toStrFunction); }); weights.incrementValue(next, 1); }; /** * Walks the model from the beginning. */ MarkovChain.prototype.walk = function () { return this.walkWithPrevious([]); }; /** * Walks the model, starting with the preceeding state. * @param previous The previous state with which to begin. */ MarkovChain.prototype.walkWithPrevious = function (previous) { var retVal = new Array(); var state = new fixed_queue_1.FixedQueue(this.order); previous.forEach(function (x) { return state.add(x); }); var _loop_1 = function () { var key = new chain_state_1.ChainState(state); if (!this_1.items.containsKey(key)) { return "break"; } var weights = this_1.items.getValue(key); var terminalWeight = 0; if (this_1.terminals.containsKey(key)) { terminalWeight = this_1.terminals.getValue(key); } var value = utils.randomNumberBetween(1, weights.totalWeight + terminalWeight); // Represents a terminal. if (value > weights.totalWeight) { return "break"; } // Get the value that corresponds to. var currentWeight = 0; weights.forEach(function (k, v) { currentWeight += v; if (currentWeight >= value) { retVal.push(k); state.enqueue(k); return false; } return true; }); }; var this_1 = this; while (true) { var state_1 = _loop_1(); if (state_1 === "break") break; } return retVal; }; return MarkovChain; }()); exports.MarkovChain = MarkovChain; ; //# sourceMappingURL=markov-chain.js.map