UNPKG

weighted-markov-generator

Version:

Quick and simple to use weighted markov text generator, seed with any text

74 lines (73 loc) 2.14 kB
"use strict"; // Weighted markov seed dictionary Object.defineProperty(exports, "__esModule", { value: true }); exports.SeedDictionary = void 0; class SeedBlob { constructor() { this.count = 0; this.countMap = new Map(); } addValue(val) { this.count++; if (this.countMap.has(val)) { this.countMap.set(val, this.countMap.get(val) + 1); } else { this.countMap.set(val, 1); } } getWeightedValue() { let rng = Math.random(); let currSum = 0; for (const [val, num] of this.countMap) { currSum += num; if ((currSum / this.count) > rng) { return val; } } } } class SeedDictionary { constructor(seedLength) { // Seed length refers to max seed length. this.seedLength = seedLength; this.dict = new Map(); } populateDictionary(text) { // Populate dictionary from 1 to seed length for (let csl = 1; csl <= this.seedLength; csl++) { let idx = 0; while (idx + csl < text.length) { let currKey = text.slice(idx, idx + csl); let currVal = text[idx + csl]; if (this.dict.has(currKey)) { let seedBlob = this.dict.get(currKey); seedBlob.addValue(currVal); } else { let seedBlob = new SeedBlob(); seedBlob.addValue(currVal); this.dict.set(currKey, seedBlob); } idx += 1; } } } getRandomSeed() { let keys = Array.from(this.dict.keys()); return keys[Math.floor(Math.random() * keys.length)]; } hasSeed(seed) { return this.dict.has(seed); } getValue(seed) { if (!this.dict.has(seed)) { throw new Error("No seed found!"); } return this.dict.get(seed).getWeightedValue(); } clearDictionary() { this.dict = new Map(); } } exports.SeedDictionary = SeedDictionary;