2 * example of Snowball dictionary
3 * http://snowball.tartarus.org/
4 * Teodor Sigaev <teodor@sigaev.ru>
13 #include "snowball/header.h"
14 #include "snowball/english_stem.h"
15 #include "snowball/russian_stem.h"
21 int (*stem) (struct SN_env * z);
25 PG_FUNCTION_INFO_V1(snb_en_init);
26 Datum snb_en_init(PG_FUNCTION_ARGS);
28 PG_FUNCTION_INFO_V1(snb_ru_init);
29 Datum snb_ru_init(PG_FUNCTION_ARGS);
31 PG_FUNCTION_INFO_V1(snb_lexize);
32 Datum snb_lexize(PG_FUNCTION_ARGS);
35 snb_en_init(PG_FUNCTION_ARGS)
37 DictSnowball *d = (DictSnowball *) malloc(sizeof(DictSnowball));
41 (errcode(ERRCODE_OUT_OF_MEMORY),
42 errmsg("out of memory")));
43 memset(d, 0, sizeof(DictSnowball));
44 d->stoplist.wordop = lowerstr;
46 if (!PG_ARGISNULL(0) && PG_GETARG_POINTER(0) != NULL)
48 text *in = PG_GETARG_TEXT_P(0);
50 readstoplist(in, &(d->stoplist));
51 sortstoplist(&(d->stoplist));
52 PG_FREE_IF_COPY(in, 0);
55 d->z = english_create_env();
58 freestoplist(&(d->stoplist));
60 (errcode(ERRCODE_OUT_OF_MEMORY),
61 errmsg("out of memory")));
63 d->stem = english_stem;
69 snb_ru_init(PG_FUNCTION_ARGS)
71 DictSnowball *d = (DictSnowball *) malloc(sizeof(DictSnowball));
75 (errcode(ERRCODE_OUT_OF_MEMORY),
76 errmsg("out of memory")));
77 memset(d, 0, sizeof(DictSnowball));
78 d->stoplist.wordop = lowerstr;
80 if (!PG_ARGISNULL(0) && PG_GETARG_POINTER(0) != NULL)
82 text *in = PG_GETARG_TEXT_P(0);
84 readstoplist(in, &(d->stoplist));
85 sortstoplist(&(d->stoplist));
86 PG_FREE_IF_COPY(in, 0);
89 d->z = russian_create_env();
92 freestoplist(&(d->stoplist));
94 (errcode(ERRCODE_OUT_OF_MEMORY),
95 errmsg("out of memory")));
97 d->stem = russian_stem;
103 snb_lexize(PG_FUNCTION_ARGS)
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);
110 memset(res, 0, sizeof(TSLexeme) * 2);
111 if (*txt == '\0' || searchstoplist(&(d->stoplist), txt))
117 SN_set_current(d->z, strlen(txt), txt);
119 if (d->z->p && d->z->l)
121 txt = repalloc(txt, d->z->l + 1);
122 memcpy(txt, d->z->p, d->z->l);
128 PG_RETURN_POINTER(res);