]> granicus.if.org Git - postgresql/blob - contrib/tsearch2/dict_snowball.c
Update misleading comment about the use of lanpltrusted ... it is
[postgresql] / contrib / tsearch2 / dict_snowball.c
1 /*
2  * example of Snowball dictionary
3  * http://snowball.tartarus.org/
4  * Teodor Sigaev <teodor@sigaev.ru>
5  */
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "postgres.h"
10
11 #include "dict.h"
12 #include "common.h"
13 #include "snowball/header.h"
14 #include "snowball/english_stem.h"
15 #include "snowball/russian_stem.h"
16
17 typedef struct
18 {
19         struct SN_env *z;
20         StopList        stoplist;
21         int                     (*stem) (struct SN_env * z);
22 }       DictSnowball;
23
24
25 PG_FUNCTION_INFO_V1(snb_en_init);
26 Datum           snb_en_init(PG_FUNCTION_ARGS);
27
28 PG_FUNCTION_INFO_V1(snb_ru_init);
29 Datum           snb_ru_init(PG_FUNCTION_ARGS);
30
31 PG_FUNCTION_INFO_V1(snb_lexize);
32 Datum           snb_lexize(PG_FUNCTION_ARGS);
33
34 Datum
35 snb_en_init(PG_FUNCTION_ARGS)
36 {
37         DictSnowball *d = (DictSnowball *) malloc(sizeof(DictSnowball));
38
39         if (!d)
40                 ereport(ERROR,
41                                 (errcode(ERRCODE_OUT_OF_MEMORY),
42                                  errmsg("out of memory")));
43         memset(d, 0, sizeof(DictSnowball));
44         d->stoplist.wordop = lowerstr;
45
46         if (!PG_ARGISNULL(0) && PG_GETARG_POINTER(0) != NULL)
47         {
48                 text       *in = PG_GETARG_TEXT_P(0);
49
50                 readstoplist(in, &(d->stoplist));
51                 sortstoplist(&(d->stoplist));
52                 PG_FREE_IF_COPY(in, 0);
53         }
54
55         d->z = english_create_env();
56         if (!d->z)
57         {
58                 freestoplist(&(d->stoplist));
59                 ereport(ERROR,
60                                 (errcode(ERRCODE_OUT_OF_MEMORY),
61                                  errmsg("out of memory")));
62         }
63         d->stem = english_stem;
64
65         PG_RETURN_POINTER(d);
66 }
67
68 Datum
69 snb_ru_init(PG_FUNCTION_ARGS)
70 {
71         DictSnowball *d = (DictSnowball *) malloc(sizeof(DictSnowball));
72
73         if (!d)
74                 ereport(ERROR,
75                                 (errcode(ERRCODE_OUT_OF_MEMORY),
76                                  errmsg("out of memory")));
77         memset(d, 0, sizeof(DictSnowball));
78         d->stoplist.wordop = lowerstr;
79
80         if (!PG_ARGISNULL(0) && PG_GETARG_POINTER(0) != NULL)
81         {
82                 text       *in = PG_GETARG_TEXT_P(0);
83
84                 readstoplist(in, &(d->stoplist));
85                 sortstoplist(&(d->stoplist));
86                 PG_FREE_IF_COPY(in, 0);
87         }
88
89         d->z = russian_create_env();
90         if (!d->z)
91         {
92                 freestoplist(&(d->stoplist));
93                 ereport(ERROR,
94                                 (errcode(ERRCODE_OUT_OF_MEMORY),
95                                  errmsg("out of memory")));
96         }
97         d->stem = russian_stem;
98
99         PG_RETURN_POINTER(d);
100 }
101
102 Datum
103 snb_lexize(PG_FUNCTION_ARGS)
104 {
105         DictSnowball *d = (DictSnowball *) PG_GETARG_POINTER(0);
106         char       *in = (char *) PG_GETARG_POINTER(1);
107         char       *txt = pnstrdup(in, PG_GETARG_INT32(2));
108         TSLexeme          *res = palloc(sizeof(TSLexeme) * 2);
109
110         memset(res, 0, sizeof(TSLexeme) * 2);
111         if (*txt == '\0' || searchstoplist(&(d->stoplist), txt))
112         {
113                 pfree(txt);
114         }
115         else
116         {
117                 SN_set_current(d->z, strlen(txt), txt);
118                 (d->stem) (d->z);
119                 if (d->z->p && d->z->l)
120                 {
121                         txt = repalloc(txt, d->z->l + 1);
122                         memcpy(txt, d->z->p, d->z->l);
123                         txt[d->z->l] = '\0';
124                 }
125                 res->lexeme = txt;
126         }
127
128         PG_RETURN_POINTER(res);
129 }