forked from WojciechMula/pyahocorasick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Automaton.c
1226 lines (1003 loc) · 29.2 KB
/
Automaton.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This is part of pyahocorasick Python module.
Automaton class implementation.
(this file includes Automaton_pickle.c)
Author : Wojciech Muła, [email protected]
WWW : http://0x80.pl
License : BSD-3-Clause (see LICENSE)
*/
#include "Automaton.h"
#include "slist.h"
static PyTypeObject automaton_type;
#define automaton_doc \
"Automaton(value_type=ahocorasick.STORE_ANY)\n\n" \
"Create a new empty Automaton. value_type is optional and one of these constants:\n" \
" - ahocorasick.STORE_ANY : The associated value can be any Python object (default).\n" \
" - ahocorasick.STORE_LENGTH : The length of an added string key is automatically\n" \
" used as the associated value stored in the trie for that key.\n" \
" - ahocorasick.STORE_INTS : The associated value must be a 32-bit integer."
static bool
check_store(const int store) {
switch (store) {
case STORE_LENGTH:
case STORE_INTS:
case STORE_ANY:
return true;
default:
PyErr_SetString(
PyExc_ValueError,
"store value must be one of ahocorasick.STORE_LENGTH, STORE_INTS or STORE_ANY"
);
return false;
} // switch
}
static bool
check_kind(const int kind) {
switch (kind) {
case EMPTY:
case TRIE:
case AHOCORASICK:
return true;
default:
PyErr_SetString(
PyExc_ValueError,
"kind value must be one of ahocorasick.EMPTY, TRIE or AHOCORASICK"
);
return false;
}
}
static bool
check_key_type(const int store) {
switch (store) {
case KEY_STRING:
case KEY_SEQUENCE:
return true;
default:
PyErr_SetString(
PyExc_ValueError,
"key_type must have value KEY_STRING or KEY_SEQUENCE"
);
return false;
} // switch
}
static PyObject*
automaton_new(PyTypeObject* self, PyObject* args, PyObject* kwargs) {
Automaton* automaton = NULL;
int key_type;
int store;
automaton = (Automaton*)PyObject_New(Automaton, &automaton_type);
if (UNLIKELY(automaton == NULL))
return NULL;
// commons settings
automaton->version = 0;
automaton->stats.version = -1;
automaton->count = 0;
automaton->longest_word = 0;
automaton->kind = EMPTY;
automaton->root = NULL;
if (UNLIKELY(PyTuple_Size(args) == 9)) {
// unpickle: count, data, kind, store, version, values
size_t count;
void* data;
size_t size;
int version;
int word_count;
int longest_word;
AutomatonKind kind;
KeysStore store;
KeyType key_type;
PyObject* values = NULL;
#ifdef PY3K
const char* fmt = "ky#iiiiiiO";
#else
const char* fmt = "ks#iiiiiiO";
#endif
if (not PyArg_ParseTuple(args, fmt, &count, &data, &size, &kind, &store, &key_type, &version, &word_count, &longest_word, &values)) {
PyErr_SetString(PyExc_ValueError, "Invalid data: unable to load from pickle.");
goto error;
}
if (!check_store(store) || !check_kind(kind) || !check_key_type(key_type)) {
goto error;
}
if (kind != EMPTY) {
if (values == Py_None) {
Py_XDECREF(values);
values = NULL;
}
if (automaton_unpickle(automaton, count, data, size, values)) {
automaton->kind = kind;
automaton->store = store;
automaton->key_type = key_type;
automaton->version = version;
automaton->count = word_count;
automaton->longest_word = longest_word;
}
else
goto error;
}
Py_XDECREF(values);
}
else {
store = STORE_ANY;
key_type = KEY_STRING;
// construct new object
if (PyArg_ParseTuple(args, "ii", &store, &key_type)) {
if (not check_store(store)) {
goto error;
}
if (not check_key_type(key_type)) {
goto error;
}
}
else if (PyArg_ParseTuple(args, "i", &store)) {
if (not check_store(store)) {
goto error;
}
}
PyErr_Clear();
automaton->store = store;
automaton->key_type = key_type;
}
//ok:
return (PyObject*)automaton;
error:
Py_XDECREF(automaton);
return NULL;
}
static void
automaton_del(PyObject* self) {
#define automaton ((Automaton*)self)
automaton_clear(self, NULL);
PyObject_Del(self);
#undef automaton
}
#define automaton_len_doc \
"Return the number of distinct keys added to the trie."
static ssize_t
automaton_len(PyObject* self) {
#define automaton ((Automaton*)self)
return automaton->count;
#undef automaton
}
#define automaton_add_word_doc \
"add_word(key, [value])\n\n" \
"Add a key string to the dict-like trie and associate this key with a value.\n" \
"value is optional or mandatory depending how the Automaton instance was created.\n" \
"Return True if the word key is inserted and did not exists in the trie or False\n" \
"otherwise.\n\n" \
"The value is either mandatory or optional:\n" \
" - If the Automaton was created without argument (the default) as Automaton() or\n" \
" with Automaton(ahocorasik.STORE_ANY) then the value is required and can be any\n" \
" Python object.\n\n" \
" - If the Automaton was created with Automaton(ahocorasik.STORE_LENGTH) then\n" \
" associating a value is not allowed --- len(word) is saved automatically as a\n" \
" value instead.\n\n" \
" - If the Automaton was created with Automaton(ahocorasik.STORE_INTS) then the\n" \
" value is optional. If provided it must be an integer, otherwise it defaults to\n" \
" len(automaton) which is therefore the order index in which keys are added to the\n" \
" trie.\n\n" \
"Calling add_word() invalidates all iterators only if the new key did not exist\n" \
"in the trie so far (i.e. the method returned True)."
static PyObject*
automaton_add_word(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)
// argument
PyObject* py_value = NULL;
struct Input input;
Py_ssize_t integer = 0;
TrieNode* node;
bool new_word;
if (!prepare_input_from_tuple(self, args, 0, &input)) {
return NULL;
}
switch (automaton->store) {
case STORE_ANY:
py_value = PyTuple_GetItem(args, 1);
if (not py_value) {
PyErr_SetString(PyExc_ValueError, "A value object is required as second argument.");
return NULL;
}
break;
case STORE_INTS:
py_value = PyTuple_GetItem(args, 1);
if (py_value) {
if (PyNumber_Check(py_value)) {
integer = PyNumber_AsSsize_t(py_value, PyExc_ValueError);
if (integer == -1 and PyErr_Occurred())
return NULL;
}
else {
PyErr_SetString(PyExc_TypeError, "An integer value is required as second argument.");
return NULL;
}
}
else {
// default
PyErr_Clear();
integer = automaton->count + 1;
}
break;
case STORE_LENGTH:
integer = input.wordlen;
break;
default:
PyErr_SetString(PyExc_SystemError, "Invalid value for this key: see documentation for supported values.");
return NULL;
}
node = NULL;
new_word = false;
if (input.wordlen > 0) {
node = trie_add_word(automaton, input.word, input.wordlen, &new_word);
destroy_input(&input);
if (node == NULL) {
return NULL;
}
} else {
destroy_input(&input);
}
if (node) {
switch (automaton->store) {
case STORE_ANY:
if (not new_word and node->eow)
// replace
Py_DECREF(node->output.object);
Py_INCREF(py_value);
node->output.object = py_value;
break;
default:
node->output.integer = integer;
} // switch
if (new_word) {
automaton->version += 1; // change version only when new word appeared
if (input.wordlen > automaton->longest_word)
automaton->longest_word = (int)input.wordlen;
Py_RETURN_TRUE;
}
else {
Py_RETURN_FALSE;
}
}
Py_RETURN_FALSE;
}
static void
clear_aux(TrieNode* node, KeysStore store) {
unsigned i;
if (node) {
switch (store) {
case STORE_INTS:
case STORE_LENGTH:
// nop
break;
case STORE_ANY:
if (node->output.object)
Py_DECREF(node->output.object);
break;
}
for (i=0; i < node->n; i++) {
TrieNode* child = node->next[i];
if (child != node) // avoid self-loops!
clear_aux(child, store);
}
xfree(node->next);
memory_free(node);
}
#undef automaton
}
#define automaton_clear_doc\
"Remove all keys from the trie. This method invalidates all iterators."
static PyObject*
automaton_clear(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)
clear_aux(automaton->root, automaton->store);
automaton->count = 0;
automaton->longest_word = 0;
automaton->kind = EMPTY;
automaton->root = NULL;
automaton->version += 1;
Py_RETURN_NONE;
#undef automaton
}
static int
automaton_contains(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)
TrieNode* node;
struct Input input;
if (!prepare_input(self, args, &input)) {
return -1;
}
node = trie_find(automaton->root, input.word, input.wordlen);
destroy_input(&input);
return (node and node->eow);
#undef automaton
}
#define automaton_exists_doc \
"exists(key)\n\n" \
"Return True if the key is present in the trie. Same as using the 'in' keyword."
static PyObject*
automaton_exists(PyObject* self, PyObject* args) {
PyObject* word;
word = PyTuple_GetItem(args, 0);
if (word)
switch (automaton_contains(self, word)) {
case 1:
Py_RETURN_TRUE;
case 0:
Py_RETURN_FALSE;
default:
return NULL;
}
else
return NULL;
}
#define automaton_match_doc \
"match(key)\n\n" \
"Return True if there is a prefix (or key) equal to key present in the trie.\n\n" \
"For example if the key 'example' has been added to the trie, then calls to\n" \
"match('e'), match('ex'), ..., match('exampl') or match('example') all return\n" \
"True. But exists() is True only when calling exists('example')"
static PyObject*
automaton_match(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)
TrieNode* node;
struct Input input;
if (!prepare_input_from_tuple(self, args, 0, &input)) {
return NULL;
}
node = trie_find(automaton->root, input.word, input.wordlen);;
destroy_input(&input);
if (node)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
#undef automaton
}
#define automaton_longest_prefix_doc \
"longest_prefix(string)\n\n" \
"Return the length of the longest prefix of string that exists in the trie."
static PyObject*
automaton_longest_prefix(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)
int len;
struct Input input;
if (!prepare_input_from_tuple(self, args, 0, &input)) {
return NULL;
}
len = trie_longest(automaton->root, input.word, input.wordlen);
destroy_input(&input);
return Py_BuildValue("i", len);
#undef automaton
}
#define automaton_get_doc \
"get(key[, default])\n\n" \
"Return the value associated with the key string.\n" \
"Raise a KeyError exception if the key is not in the trie and no default is provided.\n" \
"Return the optional default value if provided and the key is not in the trie."
static PyObject*
automaton_get(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)
struct Input input;
PyObject* py_def;
TrieNode* node;
if (!prepare_input_from_tuple(self, args, 0, &input)) {
return NULL;
}
node = trie_find(automaton->root, input.word, input.wordlen);
destroy_input(&input);
if (node and node->eow) {
switch (automaton->store) {
case STORE_INTS:
case STORE_LENGTH:
return Py_BuildValue("i", node->output.integer);
case STORE_ANY:
Py_INCREF(node->output.object);
return node->output.object;
default:
PyErr_SetNone(PyExc_ValueError);
return NULL;
}
}
else {
py_def = PyTuple_GetItem(args, 1);
if (py_def) {
Py_INCREF(py_def);
return py_def;
}
else {
PyErr_Clear();
PyErr_SetNone(PyExc_KeyError);
return NULL;
}
}
#undef automaton
}
typedef struct AutomatonQueueItem {
LISTITEM_data;
TrieNode* node;
} AutomatonQueueItem;
#define automaton_make_automaton_doc \
"Finalize and create the Aho-Corasick automaton based on the keys already added\n" \
"to the trie. This does not require additional memory. After successful creation\n" \
"the Automaton.kind attribute is set to ahocorasick.AHOCORASICK."
static PyObject*
automaton_make_automaton(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)
AutomatonQueueItem* item;
List queue;
unsigned i;
TrieNode* node;
TrieNode* child;
TrieNode* state;
if (automaton->kind != TRIE)
Py_RETURN_FALSE;
list_init(&queue);
// 1. setup nodes at first level: they fail back to the root
ASSERT(automaton->root);
for (i=0; i < automaton->root->n; i++) {
TrieNode* child = trienode_get_ith_unsafe(automaton->root, i);
ASSERT(child);
// fail edges go to the root
// every other letters loop on root - implicit (see automaton_next)
child->fail = automaton->root;
item = (AutomatonQueueItem*)list_item_new(sizeof(AutomatonQueueItem));
if (item) {
item->node = child;
list_append(&queue, (ListItem*)item);
}
else
goto no_mem;
}
// 2. make links
while (true) {
AutomatonQueueItem* item = (AutomatonQueueItem*)list_pop_first(&queue);
if (item == NULL)
break;
else {
node = item->node;
memory_free(item);
}
for (i=0; i < node->n; i++) {
child = node->next[i];
ASSERT(child);
item = (AutomatonQueueItem*)list_item_new(sizeof(AutomatonQueueItem));
if (item) {
item->node = child;
list_append(&queue, (ListItem*)item);
}
else
goto no_mem;
state = node->fail;
ASSERT(state);
ASSERT(child);
while (state != automaton->root and\
not trienode_get_next(state, child->letter)) {
state = state->fail;
ASSERT(state);
}
child->fail = trienode_get_next(state, child->letter);
if (child->fail == NULL)
child->fail = automaton->root;
ASSERT(child->fail);
}
}
automaton->kind = AHOCORASICK;
automaton->version += 1;
list_delete(&queue);
Py_RETURN_NONE;
#undef automaton
no_mem:
list_delete(&queue);
PyErr_NoMemory();
return NULL;
}
#define automaton_find_all_doc \
"find_all(string, callback, [start, [end]])\n\n" \
"Perform the Aho-Corasick search procedure using the provided input string and\n" \
"iterate over the matching tuples (end_index, value) for keys found in string.\n" \
"Invoke the callback callable for each matching tuple.\n\n" \
"The callback callable must accept two positional arguments:\n" \
" - end_index is the end index in the input string where a trie key string was found.\n" \
" - value is the value associated with the found key string.\n\n" \
"The start and end optional arguments can be used to limit the search to an\n" \
"input string slice as in string[start:end].\n\n" \
"Equivalent to a loop on iter() calling a callable at each iteration."
static PyObject*
automaton_find_all(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)
struct Input input;
ssize_t start;
ssize_t end;
PyObject* callback;
PyObject* callback_ret;
ssize_t i;
TrieNode* state;
TrieNode* tmp;
if (automaton->kind != AHOCORASICK)
Py_RETURN_NONE;
// arg 1
if (!prepare_input_from_tuple(self, args, 0, &input)) {
return NULL;
}
// arg 2
callback = PyTuple_GetItem(args, 1);
if (callback == NULL) {
destroy_input(&input);
return NULL;
}
else
if (not PyCallable_Check(callback)) {
PyErr_SetString(PyExc_TypeError, "The callback argument must be a callable such as a function.");
destroy_input(&input);
return NULL;
}
// parse start/end
if (pymod_parse_start_end(args, 2, 3, 0, input.wordlen, &start, &end)) {
destroy_input(&input);
return NULL;
}
state = automaton->root;
for (i=start; i < end; i++) {
state = tmp = ahocorasick_next(state, automaton->root, input.word[i]);
// return output
while (tmp) {
if (tmp->eow) {
if (automaton->store == STORE_ANY)
callback_ret = PyObject_CallFunction(callback, "iO", i, tmp->output.object);
else
callback_ret = PyObject_CallFunction(callback, "ii", i, tmp->output.integer);
if (callback_ret == NULL) {
destroy_input(&input);
return NULL;
} else
Py_DECREF(callback_ret);
}
tmp = tmp->fail;
}
}
#undef automaton
destroy_input(&input);
Py_RETURN_NONE;
}
static PyObject*
automaton_items_create(PyObject* self, PyObject* args, const ItemsType type) {
#define automaton ((Automaton*)self)
PyObject* arg1 = NULL;
PyObject* arg2 = NULL;
PyObject* arg3 = NULL;
TRIE_LETTER_TYPE* word = NULL;
ssize_t wordlen = 0;
TRIE_LETTER_TYPE wildcard;
bool use_wildcard = false;
PatternMatchType matchtype = MATCH_AT_LEAST_PREFIX;
AutomatonItemsIter* iter;
// arg 1: prefix/prefix pattern
if (args)
arg1 = PyTuple_GetItem(args, 0);
else
arg1 = NULL;
if (arg1) {
arg1 = pymod_get_string(arg1, &word, &wordlen);
if (arg1 == NULL)
goto error;
}
else {
PyErr_Clear();
word = NULL;
wordlen = 0;
}
// arg 2: wildcard
if (args)
arg2 = PyTuple_GetItem(args, 1);
else
arg2 = NULL;
if (arg2) {
TRIE_LETTER_TYPE* tmp;
ssize_t len = 0;
arg2 = pymod_get_string(arg2, &tmp, &len);
if (arg2 == NULL)
goto error;
else {
if (len == 1) {
wildcard = tmp[0];
use_wildcard = true;
}
else {
PyErr_SetString(PyExc_ValueError, "Wildcard must be a single character.");
goto error;
}
}
}
else {
PyErr_Clear();
wildcard = 0;
use_wildcard = false;
}
// arg3: matchtype
matchtype = MATCH_AT_LEAST_PREFIX;
if (args) {
arg3 = PyTuple_GetItem(args, 2);
if (arg3) {
Py_ssize_t val = PyNumber_AsSsize_t(arg3, PyExc_OverflowError);
if (val == -1 and PyErr_Occurred())
goto error;
switch ((PatternMatchType)val) {
case MATCH_AT_LEAST_PREFIX:
case MATCH_AT_MOST_PREFIX:
case MATCH_EXACT_LENGTH:
matchtype = (PatternMatchType)val;
break;
default:
PyErr_SetString(PyExc_ValueError,
"The optional how third argument must beone of:\n"
"MATCH_EXACT_LENGTH, MATCH_AT_LEAST_PREFIX or MATCH_AT_LEAST_PREFIX"
);
goto error;
}
}
else {
PyErr_Clear();
if (use_wildcard)
matchtype = MATCH_EXACT_LENGTH;
else
matchtype = MATCH_AT_LEAST_PREFIX;
}
}
//
iter = (AutomatonItemsIter*)automaton_items_iter_new(
automaton,
word,
wordlen,
use_wildcard,
wildcard,
matchtype);
Py_XDECREF(arg1);
Py_XDECREF(arg2);
if (iter) {
iter->type = type;
return (PyObject*)iter;
}
else
return NULL;
error:
Py_XDECREF(arg1);
Py_XDECREF(arg2);
return NULL;
#undef automaton
}
#define automaton_keys_doc \
"keys([prefix, [wildcard, [how]]])\n\n" \
"Return an iterator on keys.\n" \
"If the optional prefix string is provided, only yield keys starting with this prefix.\n" \
"If the optional wildcard is provided as a single character string, then the\n" \
"prefix is treated as a simple pattern using this character as a wildcard.\n\n" \
"The optional how argument is used to control how strings are matched using one\n" \
"of these possible values:\n\n" \
" - ahocorasick.MATCH_EXACT_LENGTH (default)\n" \
" Yield matches that have the same exact length as the prefix length.\n" \
" - ahocorasick.MATCH_AT_LEAST_PREFIX\n" \
" Yield matches that have a length greater or equal to the prefix length.\n" \
" - ahocorasick.MATCH_AT_MOST_PREFIX\n" \
" Yield matches that have a length lesser or equal to the prefix length."
static PyObject*
automaton_keys(PyObject* self, PyObject* args) {
return automaton_items_create(self, args, ITER_KEYS);
}
static PyObject*
automaton_iterate(PyObject* self) {
return automaton_items_create(self, NULL, ITER_KEYS);
}
#define automaton_values_doc \
"values([prefix, [wildcard, [how]]])\n\n" \
"Return an iterator on values associated with each keys.\n" \
"Keys are matched optionally to the prefix using the same logic and\n" \
"arguments as in the keys() method."
static PyObject*
automaton_values(PyObject* self, PyObject* args) {
return automaton_items_create(self, args, ITER_VALUES);
}
#define automaton_items_doc \
"items([prefix, [wildcard, [how]]])\n\n" \
"Return an iterator on tuples of (key, value).\n" \
"Keys are matched optionally to the prefix using the same logic and\n" \
"arguments as in the keys() method."
static PyObject*
automaton_items(PyObject* self, PyObject* args) {
return automaton_items_create(self, args, ITER_ITEMS);
}
#define automaton_iter_doc \
"iter(string, [start, [end]])\n\n" \
"Perform the Aho-Corasick search procedure using the provided input string.\n" \
"Return an iterator of tuples (end_index, value) for keys found in string where:\n\n" \
"- end_index is the end index in the input string where a trie key string was found.\n" \
"- value is the value associated with the found key string.\n\n" \
"The start and end optional arguments can be used to limit the search to an\n" \
"input string slice as in string[start:end]."
static PyObject*
automaton_iter(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)
PyObject* object;
ssize_t start;
ssize_t end;
if (automaton->kind != AHOCORASICK) {
PyErr_SetString(PyExc_AttributeError,"Not an Aho-Corasick automaton yet: "
"call add_word to add some keys and call make_automaton to "
"convert the trie to an automaton.");
return NULL;
}
object = PyTuple_GetItem(args, 0);
if (object) {
#ifdef PY3K
#ifdef AHOCORASICK_UNICODE
if (PyUnicode_Check(object)) {
start = 0;
#if PY_MINOR_VERSION >= 3
end = PyUnicode_GET_LENGTH(object);
#else
end = PyUnicode_GET_SIZE(object);
#endif
}
else {
PyErr_SetString(PyExc_TypeError, "string required");
return NULL;
}
#else
if (PyBytes_Check(object)) {
start = 0;
end = PyBytes_GET_SIZE(object);
}
else {
PyErr_SetString(PyExc_TypeError, "bytes required");
return NULL;
}
#endif
#else
if (PyString_Check(object)) {
start = 0;
end = PyString_GET_SIZE(object);
} else {
PyErr_SetString(PyExc_TypeError, "string required");
return NULL;
}
#endif
}
else
return NULL;
if (pymod_parse_start_end(args, 1, 2, start, end, &start, &end))
return NULL;
return automaton_search_iter_new(
automaton,
object,
(int)start,
(int)end
);
#undef automaton
}
static void
get_stats_aux(TrieNode* node, AutomatonStatistics* stats, int depth) {
unsigned i;
stats->nodes_count += 1;
stats->words_count += (int)(node->eow);
stats->links_count += node->n;
stats->total_size += trienode_get_size(node);
if (depth > stats->longest_word)
stats->longest_word = depth;
for (i=0; i < node->n; i++)
get_stats_aux(node->next[i], stats, depth + 1);
}
static void
get_stats(Automaton* automaton) {
automaton->stats.nodes_count = 0;
automaton->stats.words_count = 0;
automaton->stats.longest_word = 0;
automaton->stats.links_count = 0;
automaton->stats.sizeof_node = sizeof(TrieNode);
automaton->stats.total_size = 0;
if (automaton->kind != EMPTY)
get_stats_aux(automaton->root, &automaton->stats, 0);
automaton->stats.version = automaton->version;
}
#define automaton_get_stats_doc \
"get_stats()\n\n" \
"Return a dictionary containing Automaton statistics.\n" \
" - nodes_count --- total number of nodes\n" \
" - words_count --- same as len(automaton)\n" \
" - longest_word --- length of the longest word\n" \
" - links_count --- number of edges\n" \
" - sizeof_node --- size of single node in bytes\n" \
" - total_size --- total size of trie in bytes (about\n" \
" nodes_count * size_of node + links_count * size of pointer)."
static PyObject*
automaton_get_stats(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)
PyObject* dict;
if (automaton->stats.version != automaton->version)
get_stats(automaton);
dict = Py_BuildValue(