-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ahocorasick.c
119 lines (94 loc) · 2.94 KB
/
test_ahocorasick.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "string_info.h"
#include "ahocorasick.h"
int test_trie(void) {
char *s[] = {
"ab",
"abc",
"aaaaabddbdaaacccaacbaabacbaadb",
"aaacdbdbcbcdbdbadaacbaadbdbdaacaaaaaadacababdadddacaacbaaaabdacdadadbabbbddaaddaaaaa",
"abacbadaadbcaaabaaacbbaabadbababdbcadbd",
"dbbbdaabaaabaabab",
"daaadaaa",
"dbaac",
"ad",
"bdadaabbaaadaabdd",
"ddaabdd",
"bdbabb",
"abdb",
"adbab",
"aaabaaabcadba",
};
string_s text[100];
aho_trie t;
trie_init(&t); /* トライ木を初期化 */
for (int i=0; i<15; i++) {
strcpy(text[i].str, s[i]);
text[i].len = strlen(s[i]);
if (!trie_add(&t, &text[i], s[i])) /* トライ木にキーを挿入 */
printf("error (unexpected input [^a-d]\n");
printf("here\n");
}
trie_connect(&t); /* 再構成 */
trie_print(&t);
return 0;
}
// 検索成功時に実行される関数(あらかじめahoにセットしておく)
void callback_match_pos(void *arg, aho_match_t *m) {
char *text = (char *)arg;
printf("match text: ");
for (int i=m->pos; i<m->pos+m->len; i++) printf("%c", text[i]);
printf(" (match id: %d position: %d length: %d)\n", m->id, m->pos, m->len);
}
// char *sからそれに相当するstring_sを生成
string_s *new(char *s) {
string_s *n = (string_s *)malloc(sizeof(string_s));
n->len = strlen(s);
strcpy(n->str, s);
return n;
}
int test_ahocora(void) {
ahocorasick aho;
aho_init(&aho); /* aho初期化 */
aho_create_trie(&aho); /* トライ木初期化 */
/* 上二つの関数は必ず実行!! */
/* キーを木に追加 */
aho_add_match_text(&aho, new("bc"));
aho_add_match_text(&aho, new("abc"));
aho_add_match_text(&aho, new("ca"));
aho_add_match_text(&aho, new("a"));
/* アホコラ用にトライ木を再構成 */
aho_connect_trie(&aho);
char test[] = "abcabcabcab";
/* 探索成功時に実行される関数をセットする */
aho_register_match_callback(&aho, callback_match_pos, (void *)test);
trie_print(&aho.trie);
printf("try: %s\n", test);
printf("total match: %d\n", aho_search(&aho, test, strlen(test)));
return 0;
}
int test_input(void) {
ahocorasick aho;
aho_init(&aho);
aho_create_trie(&aho);
char s[500000];
FILE *fp = fopen("data/dat0_in", "r");
fscanf(fp, "%s", s);
// while (~fscanf(fp, "%s", s))
// if (strlen(s) >= 50)
// aho_add_match_text(&aho, s, strlen(s));
fp = fopen("data/dat0_ref", "r");
fscanf(fp, "%s", s);
aho_connect_trie(&aho);
aho_register_match_callback(&aho, callback_match_pos, (void *)s);
printf("total match: %d\n", aho_search(&aho, s, strlen(s)));
return 0;
}
int main(void) {
// test_trie();
test_ahocora();
// test_input();
return 0;
}