root/libswish3/trunk/src/libswish3/analyzer.c

Revision 2176, 2.4 kB (checked in by karpet, 2 months ago)

all tests passing, all (known) leaks fixed

Line 
1 /*
2  * This file is part of libswish3
3  * Copyright (C) 2007 Peter Karman
4  *
5  *  libswish3 is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  libswish3 is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with libswish3; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 /* text analyzer
21    tokenize strings, stemming
22
23 */
24
25 #include "libswish3.h"
26
27 extern int SWISH_DEBUG;
28
29 swish_Analyzer *
30 swish_init_analyzer(
31     swish_Config *config
32 )
33 {
34     swish_Analyzer *a;
35     a = swish_xmalloc(sizeof(swish_Analyzer));
36
37 /* TODO get these all from config */
38     a->maxwordlen = SWISH_MAX_WORD_LEN;
39     a->minwordlen = SWISH_MIN_WORD_LEN;
40     a->lc = 1;
41     a->ref_cnt = 0;
42     a->tokenize = config->flags->tokenize;
43
44     if (!a->tokenize && SWISH_DEBUG)
45         SWISH_DEBUG_MSG("skipping tokenizer");
46
47 /* tokenizer set in the parse* function */
48     a->tokenizer = NULL;
49
50 /* TODO get stemmer via config */
51     a->stemmer = NULL;
52
53 /* TODO standalone regex lib */
54     a->regex = NULL;
55
56     a->stash = NULL;
57    
58     if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) {
59         SWISH_DEBUG_MSG("analyzer ptr 0x%x", (long int)a);
60     }
61
62     return a;
63 }
64
65 /*
66    IMPORTANT -- any struct members that require unique free()s should
67    do that prior to calling this function.
68    stemmer, for example, or regex
69 */
70
71 void
72 swish_free_analyzer(
73     swish_Analyzer *a
74 )
75 {
76     if (a->ref_cnt != 0) {
77         SWISH_WARN("analyzer ref_cnt != 0: %d\n", a->ref_cnt);
78     }
79     if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) {
80         SWISH_DEBUG_MSG("free analyzer");
81         swish_mem_debug();
82     }
83    
84     if (a->stash != NULL)
85         SWISH_WARN("Analyzer->stash not freed 0x%x", (long int)a->stash);
86        
87     if (a->regex != NULL)
88         SWISH_WARN("Analyzer->regex not freed 0x%x", (long int)a->regex);
89        
90     if (a->stemmer != NULL)
91         SWISH_WARN("Analyzer->stemmer not freed");
92        
93    
94     swish_xfree(a);
95 }
Note: See TracBrowser for help on using the browser.