PostgreSQL Source Code  git master
dict_snowball.c File Reference
#include "postgres.h"
#include "commands/defrem.h"
#include "tsearch/ts_locale.h"
#include "tsearch/ts_utils.h"
#include "snowball/libstemmer/header.h"
#include "snowball/libstemmer/stem_ISO_8859_1_danish.h"
#include "snowball/libstemmer/stem_ISO_8859_1_dutch.h"
#include "snowball/libstemmer/stem_ISO_8859_1_english.h"
#include "snowball/libstemmer/stem_ISO_8859_1_finnish.h"
#include "snowball/libstemmer/stem_ISO_8859_1_french.h"
#include "snowball/libstemmer/stem_ISO_8859_1_german.h"
#include "snowball/libstemmer/stem_ISO_8859_1_indonesian.h"
#include "snowball/libstemmer/stem_ISO_8859_1_irish.h"
#include "snowball/libstemmer/stem_ISO_8859_1_italian.h"
#include "snowball/libstemmer/stem_ISO_8859_1_norwegian.h"
#include "snowball/libstemmer/stem_ISO_8859_1_porter.h"
#include "snowball/libstemmer/stem_ISO_8859_1_portuguese.h"
#include "snowball/libstemmer/stem_ISO_8859_1_spanish.h"
#include "snowball/libstemmer/stem_ISO_8859_1_swedish.h"
#include "snowball/libstemmer/stem_ISO_8859_2_hungarian.h"
#include "snowball/libstemmer/stem_ISO_8859_2_romanian.h"
#include "snowball/libstemmer/stem_KOI8_R_russian.h"
#include "snowball/libstemmer/stem_UTF_8_arabic.h"
#include "snowball/libstemmer/stem_UTF_8_danish.h"
#include "snowball/libstemmer/stem_UTF_8_dutch.h"
#include "snowball/libstemmer/stem_UTF_8_english.h"
#include "snowball/libstemmer/stem_UTF_8_finnish.h"
#include "snowball/libstemmer/stem_UTF_8_french.h"
#include "snowball/libstemmer/stem_UTF_8_german.h"
#include "snowball/libstemmer/stem_UTF_8_greek.h"
#include "snowball/libstemmer/stem_UTF_8_hungarian.h"
#include "snowball/libstemmer/stem_UTF_8_indonesian.h"
#include "snowball/libstemmer/stem_UTF_8_irish.h"
#include "snowball/libstemmer/stem_UTF_8_italian.h"
#include "snowball/libstemmer/stem_UTF_8_lithuanian.h"
#include "snowball/libstemmer/stem_UTF_8_nepali.h"
#include "snowball/libstemmer/stem_UTF_8_norwegian.h"
#include "snowball/libstemmer/stem_UTF_8_porter.h"
#include "snowball/libstemmer/stem_UTF_8_portuguese.h"
#include "snowball/libstemmer/stem_UTF_8_romanian.h"
#include "snowball/libstemmer/stem_UTF_8_russian.h"
#include "snowball/libstemmer/stem_UTF_8_spanish.h"
#include "snowball/libstemmer/stem_UTF_8_swedish.h"
#include "snowball/libstemmer/stem_UTF_8_tamil.h"
#include "snowball/libstemmer/stem_UTF_8_turkish.h"
Include dependency graph for dict_snowball.c:

Go to the source code of this file.

Data Structures

struct  stemmer_module
 
struct  DictSnowball
 

Macros

#define STEMMER_MODULE(name, enc, senc)   {#name, enc, name##_##senc##_create_env, name##_##senc##_close_env, name##_##senc##_stem}
 

Typedefs

typedef struct stemmer_module stemmer_module
 
typedef struct DictSnowball DictSnowball
 

Functions

 PG_FUNCTION_INFO_V1 (dsnowball_init)
 
 PG_FUNCTION_INFO_V1 (dsnowball_lexize)
 
static void locate_stem_module (DictSnowball *d, const char *lang)
 
Datum dsnowball_init (PG_FUNCTION_ARGS)
 
Datum dsnowball_lexize (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 
static const stemmer_module stemmer_modules []
 

Macro Definition Documentation

◆ STEMMER_MODULE

#define STEMMER_MODULE (   name,
  enc,
  senc 
)    {#name, enc, name##_##senc##_create_env, name##_##senc##_close_env, name##_##senc##_stem}

Definition at line 87 of file dict_snowball.c.

Typedef Documentation

◆ DictSnowball

typedef struct DictSnowball DictSnowball

◆ stemmer_module

Function Documentation

◆ dsnowball_init()

Datum dsnowball_init ( PG_FUNCTION_ARGS  )

Definition at line 204 of file dict_snowball.c.

References CurrentMemoryContext, defGetString(), DefElem::defname, DictSnowball::dictCtx, ereport, errcode(), errmsg(), ERROR, SN_env::l, lfirst, locate_stem_module(), lowerstr(), palloc0(), PG_GETARG_POINTER, PG_RETURN_POINTER, readstoplist(), DictSnowball::stem, and DictSnowball::stoplist.

205 {
206  List *dictoptions = (List *) PG_GETARG_POINTER(0);
207  DictSnowball *d;
208  bool stoploaded = false;
209  ListCell *l;
210 
211  d = (DictSnowball *) palloc0(sizeof(DictSnowball));
212 
213  foreach(l, dictoptions)
214  {
215  DefElem *defel = (DefElem *) lfirst(l);
216 
217  if (strcmp(defel->defname, "stopwords") == 0)
218  {
219  if (stoploaded)
220  ereport(ERROR,
221  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
222  errmsg("multiple StopWords parameters")));
224  stoploaded = true;
225  }
226  else if (strcmp(defel->defname, "language") == 0)
227  {
228  if (d->stem)
229  ereport(ERROR,
230  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
231  errmsg("multiple Language parameters")));
232  locate_stem_module(d, defGetString(defel));
233  }
234  else
235  {
236  ereport(ERROR,
237  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
238  errmsg("unrecognized Snowball parameter: \"%s\"",
239  defel->defname)));
240  }
241  }
242 
243  if (!d->stem)
244  ereport(ERROR,
245  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
246  errmsg("missing Language parameter")));
247 
249 
251 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
StopList stoplist
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
char * lowerstr(const char *str)
Definition: ts_locale.c:239
int(* stem)(struct SN_env *z)
#define ERROR
Definition: elog.h:43
char * defGetString(DefElem *def)
Definition: define.c:49
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define ereport(elevel, rest)
Definition: elog.h:141
void readstoplist(const char *fname, StopList *s, char *(*wordop)(const char *))
Definition: ts_utils.c:68
void * palloc0(Size size)
Definition: mcxt.c:980
static void locate_stem_module(DictSnowball *d, const char *lang)
#define lfirst(lc)
Definition: pg_list.h:190
int errmsg(const char *fmt,...)
Definition: elog.c:784
MemoryContext dictCtx
char * defname
Definition: parsenodes.h:730
Definition: pg_list.h:50

◆ dsnowball_lexize()

Datum dsnowball_lexize ( PG_FUNCTION_ARGS  )

Definition at line 254 of file dict_snowball.c.

References DictSnowball::dictCtx, SN_env::l, TSLexeme::lexeme, lowerstr_with_len(), MemoryContextSwitchTo(), DictSnowball::needrecode, SN_env::p, palloc0(), pfree(), pg_any_to_server(), PG_GETARG_INT32, PG_GETARG_POINTER, PG_RETURN_POINTER, pg_server_to_any(), PG_UTF8, repalloc(), searchstoplist(), SN_set_current(), DictSnowball::stem, DictSnowball::stoplist, and DictSnowball::z.

255 {
257  char *in = (char *) PG_GETARG_POINTER(1);
258  int32 len = PG_GETARG_INT32(2);
259  char *txt = lowerstr_with_len(in, len);
260  TSLexeme *res = palloc0(sizeof(TSLexeme) * 2);
261 
262  if (*txt == '\0' || searchstoplist(&(d->stoplist), txt))
263  {
264  pfree(txt);
265  }
266  else
267  {
268  MemoryContext saveCtx;
269 
270  /*
271  * recode to utf8 if stemmer is utf8 and doesn't match server encoding
272  */
273  if (d->needrecode)
274  {
275  char *recoded;
276 
277  recoded = pg_server_to_any(txt, strlen(txt), PG_UTF8);
278  if (recoded != txt)
279  {
280  pfree(txt);
281  txt = recoded;
282  }
283  }
284 
285  /* see comment about d->dictCtx */
286  saveCtx = MemoryContextSwitchTo(d->dictCtx);
287  SN_set_current(d->z, strlen(txt), (symbol *) txt);
288  d->stem(d->z);
289  MemoryContextSwitchTo(saveCtx);
290 
291  if (d->z->p && d->z->l)
292  {
293  txt = repalloc(txt, d->z->l + 1);
294  memcpy(txt, d->z->p, d->z->l);
295  txt[d->z->l] = '\0';
296  }
297 
298  /* back recode if needed */
299  if (d->needrecode)
300  {
301  char *recoded;
302 
303  recoded = pg_any_to_server(txt, strlen(txt), PG_UTF8);
304  if (recoded != txt)
305  {
306  pfree(txt);
307  txt = recoded;
308  }
309  }
310 
311  res->lexeme = txt;
312  }
313 
314  PG_RETURN_POINTER(res);
315 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
StopList stoplist
symbol * p
Definition: api.h:15
int SN_set_current(struct SN_env *z, int size, const symbol *s)
Definition: api.c:58
char * lowerstr_with_len(const char *str, int len)
Definition: ts_locale.c:252
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
int(* stem)(struct SN_env *z)
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:654
signed int int32
Definition: c.h:346
void pfree(void *pointer)
Definition: mcxt.c:1056
char * lexeme
Definition: ts_public.h:111
void * palloc0(Size size)
Definition: mcxt.c:980
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
struct SN_env * z
bool searchstoplist(StopList *s, char *key)
Definition: ts_utils.c:141
MemoryContext dictCtx
int l
Definition: api.h:16
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:581
unsigned char symbol
Definition: api.h:2

◆ locate_stem_module()

static void locate_stem_module ( DictSnowball d,
const char *  lang 
)
static

Definition at line 163 of file dict_snowball.c.

References stemmer_module::create, stemmer_module::enc, ereport, errcode(), errmsg(), ERROR, GetDatabaseEncoding(), GetDatabaseEncodingName(), stemmer_module::name, DictSnowball::needrecode, PG_SQL_ASCII, pg_strcasecmp(), PG_UTF8, stemmer_module::stem, DictSnowball::stem, and DictSnowball::z.

Referenced by dsnowball_init().

164 {
165  const stemmer_module *m;
166 
167  /*
168  * First, try to find exact match of stemmer module. Stemmer with
169  * PG_SQL_ASCII encoding is treated as working with any server encoding
170  */
171  for (m = stemmer_modules; m->name; m++)
172  {
173  if ((m->enc == PG_SQL_ASCII || m->enc == GetDatabaseEncoding()) &&
174  pg_strcasecmp(m->name, lang) == 0)
175  {
176  d->stem = m->stem;
177  d->z = m->create();
178  d->needrecode = false;
179  return;
180  }
181  }
182 
183  /*
184  * Second, try to find stemmer for needed language for UTF8 encoding.
185  */
186  for (m = stemmer_modules; m->name; m++)
187  {
188  if (m->enc == PG_UTF8 && pg_strcasecmp(m->name, lang) == 0)
189  {
190  d->stem = m->stem;
191  d->z = m->create();
192  d->needrecode = true;
193  return;
194  }
195  }
196 
197  ereport(ERROR,
198  (errcode(ERRCODE_UNDEFINED_OBJECT),
199  errmsg("no Snowball stemmer available for language \"%s\" and encoding \"%s\"",
200  lang, GetDatabaseEncodingName())));
201 }
int errcode(int sqlerrcode)
Definition: elog.c:570
int(* stem)(struct SN_env *z)
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define ERROR
Definition: elog.h:43
struct SN_env *(* create)(void)
Definition: dict_snowball.c:81
#define ereport(elevel, rest)
Definition: elog.h:141
int GetDatabaseEncoding(void)
Definition: mbutils.c:1046
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1052
static const stemmer_module stemmer_modules[]
Definition: dict_snowball.c:90
int(* stem)(struct SN_env *)
Definition: dict_snowball.c:83
struct SN_env * z
int errmsg(const char *fmt,...)
Definition: elog.c:784
const char * name
Definition: dict_snowball.c:79

◆ PG_FUNCTION_INFO_V1() [1/2]

PG_FUNCTION_INFO_V1 ( dsnowball_init  )

◆ PG_FUNCTION_INFO_V1() [2/2]

PG_FUNCTION_INFO_V1 ( dsnowball_lexize  )

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 70 of file dict_snowball.c.

◆ stemmer_modules

const stemmer_module stemmer_modules[]
static

Definition at line 90 of file dict_snowball.c.