aho-corasick.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. interface Node {
  2. /** @default 0 */
  3. depth?: number,
  4. key: string,
  5. /** @default false */
  6. word?: boolean,
  7. children: Record<string, Node>,
  8. fail?: Node,
  9. count: number
  10. }
  11. const createNode = (key: string, depth = 0): Node => ({
  12. depth,
  13. key,
  14. word: false,
  15. children: {},
  16. fail: undefined,
  17. count: 0
  18. });
  19. const createKeywordFilter = (keys: string[] | Set<string>) => {
  20. const root = createNode('root');
  21. const build = () => {
  22. const queue: Node[] = [];
  23. queue.push(root);
  24. let idx = 0;
  25. while (queue.length > idx) {
  26. const beginNode = queue[idx];
  27. const map = beginNode.children;
  28. // eslint-disable-next-line guard-for-in -- plain object
  29. for (const key in beginNode.children) {
  30. const node = map[key];
  31. let failNode = beginNode.fail;
  32. while (failNode && !failNode.children[key]) {
  33. failNode = failNode.fail;
  34. }
  35. if (node) {
  36. node.fail = failNode?.children[key] || root;
  37. queue.push(node);
  38. }
  39. }
  40. idx++;
  41. }
  42. };
  43. const put = (key: string, len: number) => {
  44. let node = root;
  45. const lastIdx = len - 1;
  46. node.count++;
  47. for (let idx = 0; idx < len; idx++) {
  48. const val = key[idx];
  49. const nextNode = node.children[val];
  50. if (nextNode) {
  51. nextNode.count++;
  52. node = nextNode;
  53. } else {
  54. const newNode = createNode(val, idx + 1);
  55. newNode.count = 1;
  56. node.children[val] = newNode;
  57. node = newNode;
  58. }
  59. if (lastIdx === idx && node.depth) {
  60. node.word = true;
  61. }
  62. }
  63. };
  64. keys.forEach(k => put(k, k.length));
  65. build();
  66. const search = (text: string) => {
  67. let node: Node | undefined = root;
  68. for (let i = 0, textLen = text.length; i < textLen; i++) {
  69. // const key = text.charAt(i);
  70. const key = text[i];
  71. while (node && !node.children[key]) {
  72. node = node.fail;
  73. }
  74. node = node?.children[key] || root;
  75. if (node.word) {
  76. return true;
  77. }
  78. }
  79. return false;
  80. };
  81. return {
  82. search
  83. };
  84. };
  85. export default createKeywordFilter;