aho-corasick.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 => {
  65. put(k, k.length);
  66. });
  67. build();
  68. const search = (text: string) => {
  69. let node: Node | undefined = root;
  70. for (let i = 0, textLen = text.length; i < textLen; i++) {
  71. // const key = text.charAt(i);
  72. const key = text[i];
  73. while (node && !node?.children[key]) {
  74. node = node?.fail;
  75. }
  76. node = node?.children[key] || root;
  77. if (node.word) {
  78. return true;
  79. }
  80. }
  81. return false;
  82. };
  83. return {
  84. search
  85. };
  86. };
  87. export default createKeywordFilter;