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.
/*--------------------------------------------------------------------------------*
Project: CrossRoad
Copyright (C)Nintendo All rights reserved.
These coded instructions, statements, and computer programs contain proprietary
information of Nintendo and/or its licensed developers and are protected by
national and international copyright laws. They may not be disclosed to third
parties or copied or duplicated in any form, in whole or in part, without the
prior written consent of Nintendo.
The content herein is highly confidential and should be handled accordingly.
*--------------------------------------------------------------------------------*/
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