word-match-helper
Version:
Aho-Corasick based word-matching class. Match & filter words.
32 lines (31 loc) • 887 B
TypeScript
export default class AhoCorasick {
private root;
currentState: number;
wordset: Set<string>;
constructor(config: {
targets: string[];
});
addWord(wordlist: string[]): void;
private initACStateTree;
/**
* @param root tireTree root
*
* how to get each node's backNode:
* 1. if node is root, backnode points to itself
* 2. if parents are root, backnode points to root
* 3. find childNode of parentNode's backNode, if the character value are same,
* then that childNode is this node's backNode
* 4. (loop) childNode of parentNode's backNode doesn't fit current node,
* continue to find parentNode's parentNode
*/
private setBackNode;
/**
* @param text search text
*
*/
search(text: string): {
pos: number;
word: string;
}[];
private getWord;
}