LOGO

Multifast 2.0.0

A Total Solution for
Mass String Search & Substitution

Download

Example 2

  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
/*
 * example2.c: Describes the _replace()/_rep_flush() function pair of the 
 * ahocorasick library
 * 
 * This file is part of multifast.
 *
    Copyright 2010-2015 Kamiar Kanani <kamiar.kanani@gmail.com>

    multifast is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    multifast is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with multifast.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include "ahocorasick.h"

#define PATTERN(p,r)    {{p,sizeof(p)-1},{r,sizeof(r)-1},{{0},0}}
#define CHUNK(c)        {c,sizeof(c)-1}

AC_PATTERN_t patterns[] = {
    PATTERN("city", "[S1]"),    /* Replace "simplicity" with "[S1]" */
    PATTERN("the ", ""),        /* Replace "the " with an empty string */
    PATTERN("and", NULL),       /* Do not replace "and" */
    PATTERN("experience", "[S2]"),
    PATTERN("exp", "[S3]"),
    PATTERN("simplicity", "[S4]"),
    PATTERN("ease", "[S5]"),
};
#define PATTERN_COUNT (sizeof(patterns)/sizeof(AC_PATTERN_t))

AC_TEXT_t input_chunks[] = {
    CHUNK("experience "),
    CHUNK("the ease "),
    CHUNK("and simpli"),
    CHUNK("city of multifast"),
};
#define CHUNK_COUNT (sizeof(input_chunks)/sizeof(AC_TEXT_t))

/* Define a call-back function of type MF_REPLACE_CALBACK_f */
void listener (AC_TEXT_t *text, void *user);

/* The call-back function is called when:
 *      1. the replacement buffer is full
 *      2. the _rep_flush() is called
 * 
 * Replacement buffer size is determined by the MF_REPLACEMENT_BUFFER_SIZE 
 * macro
 */

int main (int argc, char **argv)
{
    unsigned int i;
    AC_TRIE_t *trie;
    
    /* Get a new trie */
    trie = ac_trie_create ();
    
    /* Add patterns to the trie */
    for (i = 0; i < PATTERN_COUNT; i++)
    {
        if (ac_trie_add (trie, &patterns[i], 0) != ACERR_SUCCESS)
            printf("Failed to add pattern \"%.*s\"\n", 
                    (int)patterns[i].ptext.length, patterns[i].ptext.astring);
    }
    
    /* Finalize the trie */
    ac_trie_finalize (trie);
    
    printf("Normal replace mode:\n");

    for (i = 0; i < CHUNK_COUNT; i++)
        /* Replace */
        multifast_replace (trie, 
                &input_chunks[i], MF_REPLACE_MODE_NORMAL, listener, 0);
    
    /* Flush the buffer */
    multifast_rep_flush (trie, 0);
    
    /* After the last chunk you must call the rep_flush() function in order to 
     * receive the final result in your call-back function. The last rep_flush
     * call must be done with 0 in its second parameter.
     * 
     * It is possible to receive intermediate results by calling rep_flush with
     * a non-zero value in the second parameter. The intermediate results may
     * not be as you expect, Because the replacement algorithm keeps the 
     * prefixes in a backlog buffer.
     */
    
    printf("\nLazy replace mode:\n");
    
    /* There are two replacement modes:
     *      1. Normal
     *      2. Lazy
     * 
     * In the normal mode:
     *      - Any pattern occurrence is replaced
     *      - Factor patterns are ignored
     * 
     * In the lazy mode:
     *      - The first occurrence is replaced
     *      - If the first occurrence has a common factor with a successor 
     *        pattern, then the successor is ignored
     * 
     * Example:
     * 
     * Patterns and replacements:
     *      abc -> x
     *      cb -> y
     *      b -> z
     * 
     * Input text and replacement result:
     * 
     *  - Normal mode:
     *      abc => x
     *      abcb => xy
     * 
     *  - Lazy mode:
     *      abc => azc
     *      abcb => azy
     * 
     */
    
    for (i = 0; i < CHUNK_COUNT; i++)
        /* Replace */
        multifast_replace (trie, 
                &input_chunks[i], MF_REPLACE_MODE_LAZY, listener, 0);
    
    /* Flush the buffer */
    multifast_rep_flush (trie, 0);
    
    printf("\n");
    
    /* Release the trie */
    ac_trie_release (trie);
    
    return 0;
}

void listener (AC_TEXT_t *text, void *user)
{
    printf ("%.*s", (int)text->length, text->astring);
}