- This sample registers a detection keyword and runs an Aho-Corasick algorithm.
- The sample builds an automation that detects the key words "pen" and "this," and determines the position of the keywords within the string "this is a pen" using the Aho-Corasick algorithm.
- The Aho-Corasick algorithm can also be built offline using
ac.exe
. Please refer to the makefile and other files of the other samples.
using nlib_ns::succinct::AhoCorasickBuilder;
using nlib_ns::succinct::AhoCorasick;
bool MyMatchCallback(const char* first, const char* last, uint32_t nodeId, void*) {
char str[4096];
int i = 0;
while (first != last) str[i++] = *first++;
str[i] = '\0';
return true;
}
bool SampleMain(int, char**) {
AhoCorasickBuilder builder;
if (!builder.Init()) return false;
if (!builder.AddWord("pen")) return false;
if (!builder.AddWord("this")) return false;
AhoCorasick* ac = builder.Build();
if (!ac) return false;
ac->Match("this is a pen.", MyMatchCallback);
delete ac;
return true;
}
NLIB_MAINFUNC