markov-coil
Version:
Markov chain optimized for large bodies of text.
50 lines (49 loc) • 2.07 kB
TypeScript
export declare class MarkovNode {
children: Map<number, MarkovNode>;
weight: number;
}
interface MarkovVocab {
tokens: string[];
indexes: Map<string, number>;
}
export declare class MarkovCoil {
depth: number;
vocab: MarkovVocab;
root: MarkovNode;
/**
* Create new markov chain for token prediction.
* @param {string[]} tokens Tokens to generate chain with.
* @param {number} [depth=3] Depth of the chain. A depth of n will use n tokens in its search for a prediction (n-gram).
*/
constructor(tokens: string[], depth?: number);
private getIndex;
private getToken;
private addTokenToVocab;
private addSequenceToChain;
private createChain;
/**
* Predict next possible values for the given sequence of tokens.
* @param {string[]} context An array of tokens.
* @returns A mapping of possible tokens to their probability of occuring.
*/
predictions(context: string[]): Record<string, number>;
private weightedChoice;
/**
* Predict the next token given a starting sequence.
* @param {string[]} sequence The starting sequence of tokens.
* @param {boolean} [weighted=true] If true, will use weighted random choice from all possible predictions. If false, will use random choice.
* @returns {string} The predicted token.
*/
predict(sequence: string[], weighted?: boolean): string | null;
/**
* Predicts a sequence of tokens that could follow the starting sequence.
* @param {token[]} sequence The starting sequence of tokens.
* @param {number} length The length of the predicted sequence.
* @param {boolean} [weighted=true] If true, will use weighted random choice from all possible predictions. If false, will use random choice.
* @returns {string[]} A sequence of predicted tokens.
*/
predictSequence(sequence: string[], length: number, weighted?: boolean): string[];
/** Prints tabulated trie structure to console. Useful for debugging. */
prettyPrint(useVocab?: boolean): void;
}
export {};