nlib
succinct/simple/simple.cpp
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 node_id, void*) {
char str[4096];
int i = 0;
while (first != last) str[i++] = *first++;
str[i] = '\0';
nlib_printf("%" PRIu32 ", \"%s\"\n", node_id, str);
return true;
}
bool SampleMain(int, char**) {
AhoCorasickBuilder builder;
if (nlib_is_error(builder.Init())) return false;
if (nlib_is_error(builder.AddWord("pen"))) return false;
if (nlib_is_error(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