UNPKG

markov-typescript

Version:

A Markov Chain library written in Typescript.

381 lines (380 loc) 12.1 kB
<!doctype html> <html lang="en"> <head> <title>Code coverage report for src\markov-chain.ts</title> <meta charset="utf-8" /> <link rel="stylesheet" href="../prettify.css" /> <link rel="stylesheet" href="../base.css" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <style type='text/css'> .coverage-summary .sorter { background-image: url(../sort-arrow-sprite.png); } </style> </head> <body> <div class='wrapper'> <div class='pad1'> <h1> <a href="../index.html">all files</a> / <a href="index.html">src/</a> markov-chain.ts </h1> <div class='clearfix'> <div class='fl pad1y space-right2'> <span class="strong">93.33% </span> <span class="quiet">Statements</span> <span class='fraction'>56/60</span> </div> <div class='fl pad1y space-right2'> <span class="strong">81.25% </span> <span class="quiet">Branches</span> <span class='fraction'>13/16</span> </div> <div class='fl pad1y space-right2'> <span class="strong">75% </span> <span class="quiet">Functions</span> <span class='fraction'>9/12</span> </div> <div class='fl pad1y space-right2'> <span class="strong">96.15% </span> <span class="quiet">Lines</span> <span class='fraction'>50/52</span> </div> </div> </div> <div class='status-line high'></div> <pre><table class="coverage"> <tr><td class="line-count quiet">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106</td><td class="line-coverage quiet"><span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes">31×</span> <span class="cline-any cline-yes">31×</span> <span class="cline-any cline-yes">31×</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">31×</span> <span class="cline-any cline-yes">31×</span> <span class="cline-any cline-yes">31×</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes">10×</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes">10×</span> <span class="cline-any cline-yes">10×</span> <span class="cline-any cline-yes">10×</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">10×</span> <span class="cline-any cline-yes">100×</span> <span class="cline-any cline-yes">100×</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">90×</span> <span class="cline-any cline-yes">90×</span> <span class="cline-any cline-yes">90×</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">90×</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">90×</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">90×</span> <span class="cline-any cline-yes">90×</span> <span class="cline-any cline-yes">110×</span> <span class="cline-any cline-yes">110×</span> <span class="cline-any cline-yes">90×</span> <span class="cline-any cline-yes">90×</span> <span class="cline-any cline-yes">90×</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">20×</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">10×</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">import * as Collections from "typescript-collections"; &nbsp; import { ChainState } from "./chain-state"; import { FixedQueue } from "./fixed-queue"; import { MarkovChainItems } from "./markov-chain-items"; import { MarkovTerminalItems } from "./markov-terminal-items"; import * as utils from "./utils"; import { WeightedDictionary } from "./weighted-dictionary"; &nbsp; export class MarkovChain&lt;T&gt; { public readonly order: number; protected readonly items: MarkovChainItems&lt;T&gt; = new MarkovChainItems&lt;T&gt;() protected readonly terminals: MarkovTerminalItems&lt;T&gt; = new MarkovTerminalItems&lt;T&gt;(); &nbsp; protected readonly toStrFunction: (key: T) =&gt; string; &nbsp; /** * Initializes a new instance of MarkovChain&lt;T&gt;. * @param order The number of state transitions to track within the model. * @param toStrFunction Optional function used to convert keys to strings. */ constructor(<span class="missing-if-branch" title="if path not taken" >I</span>order: number = 2, toStrFunction?: (key: T) =&gt; string) { if (order &lt; 0) { throw new RangeError("Order must not be less than 0."); } this.order = order; this.toStrFunction = toStrFunction; } &nbsp; /** * Learns multiple sequences and their transitions. * @param items The list of sequences to learn. */ <span class="fstat-no" title="function not covered" > learnAll(items: T[][])</span>: void { <span class="cstat-no" title="statement not covered" > items.forEach(<span class="fstat-no" title="function not covered" >item </span>=&gt; <span class="cstat-no" title="statement not covered" >this.learn(item))</span>;</span> } &nbsp; /** * Learns a single sequence of elements. * @param items Lear */ learn(items: T[]): void { if (!items || items.length === 0) { return; } &nbsp; const previous: FixedQueue&lt;T&gt; = new FixedQueue&lt;T&gt;(this.order); items.forEach(item =&gt; { const key = new ChainState(previous); this.learnWithPrevious(key, item); previous.enqueue(item); }); &nbsp; const terminalKey = new ChainState(previous); this.terminals.incrementValue(terminalKey, 1); } &nbsp; private learnWithPrevious(previous: ChainState&lt;T&gt;, next: T): void { const weights = this.items.getOrCreateValue(previous, () =&gt; new WeightedDictionary&lt;T&gt;(this.toStrFunction)); weights.incrementValue(next, 1); } &nbsp; /** * Walks the model from the beginning. */ walk(): T[] { return this.walkWithPrevious([]); } &nbsp; /** * Walks the model, starting with the preceeding state. * @param previous The previous state with which to begin. */ walkWithPrevious(previous: T[]): T[] { const retVal: T[] = new Array(); const state: FixedQueue&lt;T&gt; = new FixedQueue&lt;T&gt;(this.order); previous.forEach(<span class="fstat-no" title="function not covered" >x </span>=&gt; <span class="cstat-no" title="statement not covered" >state.add(x))</span>; &nbsp; while (true) { const key = new ChainState(state); if (!this.items.containsKey(key)) { break; } &nbsp; const weights: WeightedDictionary&lt;T&gt; = this.items.getValue(key); let terminalWeight = 0; <span class="missing-if-branch" title="if path not taken" >I</span>if (this.terminals.containsKey(key)) { <span class="cstat-no" title="statement not covered" > terminalWeight = this.terminals.getValue(key);</span> } &nbsp; const value = utils.randomNumberBetween(1, weights.totalWeight + terminalWeight); &nbsp; // Represents a terminal. <span class="missing-if-branch" title="if path not taken" >I</span>if (value &gt; weights.totalWeight) { break; } &nbsp; // Get the value that corresponds to. let currentWeight = 0; weights.forEach((k, v) =&gt; { currentWeight += v; if (currentWeight &gt;= value) { retVal.push(k); state.enqueue(k); return false; } return true; }); } &nbsp; return retVal; } }; &nbsp;</pre></td></tr> </table></pre> <div class='push'></div><!-- for sticky footer --> </div><!-- /wrapper --> <div class='footer quiet pad2 space-top1 center small'> Code coverage generated by <a href="http://istanbul-js.org/" target="_blank">istanbul</a> at Sun Jul 02 2017 12:30:12 GMT+0100 (GMT Daylight Time) </div> </div> <script src="../prettify.js"></script> <script> window.onload = function () { if (typeof prettyPrint === 'function') { prettyPrint(); } }; </script> <script src="../sorter.js"></script> </body> </html>