nlib
succinct/simple/simple.cpp
検出用キーワードを登録してAC法を実行するサンプルです。
"pen", "this"というキーワードを検出することができるオートマトンを構築して"this is a pen."という文中のキーワードの位置をAC法により検出します。
AC法のオートマトンはac.exeを利用してオフラインで構築することもできます。 他のサンプルのMakefile等を参照してください。
/*---------------------------------------------------------------------------*
Project: CrossRoad
Copyright (C)2012-2016 Nintendo. All rights reserved.
These coded instructions, statements, and computer programs contain
proprietary information of Nintendo of America Inc. and/or Nintendo
Company Ltd., and are protected by Federal copyright law. 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.
*---------------------------------------------------------------------------*/
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