void loadPattern(const string &filename) { FILE* in = tryOpen(filename, "r"); for (;getLine(in);) { vector<string> tokens = splitBy(line, ','); string pattern = tolower(tokens[0]); int occurrence; fromString(tokens[1], occurrence); patterns[pattern] = occurrence; prob[pattern] = occurrence; quote[pattern] = parenthesis[pattern] = dash[pattern] = capital[pattern] = total[pattern] = 0; size_t tokensN = splitBy(pattern, ' ').size(); f[pattern].resize(tokensN, 0); sumOutside[pattern].resize(tokensN, 0); tree.add(" " + pattern + " "); } fclose(in); //cerr << "# Pattern = " << prob.size() << endl; tree.make(); //cerr << "Tree is built" << endl; }
void selftest() { // self test AhoCorasick tree; tree.add("a"); tree.add("aa"); tree.add("ab"); tree.make(); // 0123456789 vector< pair<int, int> > positions; tree.search("aa ab baa", positions); /*FOR (iter, positions) { cerr << iter->first << " " << iter->second << endl; }*/ assert(positions.size() == 8); //cerr << "self test on AC automaton passed" << endl; }