File: snowball.c

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (196 lines) | stat: -rw-r--r-- 4,967 bytes parent folder | download | duplicates (4)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2010-2017, VU University Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

#include <config.h>
#include <SWI-Prolog.h>
#include <SWI-Stream.h>
#include "libstemmer_c/include/libstemmer.h"
#include <pthread.h>
#include <string.h>
#include <assert.h>
#include <errno.h>

#define STEMMER_BUCKETS (32)		/* cache CACHE_SIZE languages */

typedef struct stemmer
{ atom_t		language;
  struct stemmer       *next;
  struct sb_stemmer    *stemmer;
} stemmer;

typedef struct
{ stemmer	*stemmers[STEMMER_BUCKETS];
} stem_cache;

static pthread_key_t stem_key;
#ifndef __WINDOWS__
static pthread_once_t stem_key_once = PTHREAD_ONCE_INIT;
#endif

static void
stem_destroy_cache(void *buf)
{ stem_cache *cache = buf;
  int i;

  for(i=0; i<STEMMER_BUCKETS; i++)
  { stemmer *s = cache->stemmers[i];
    stemmer *n;

    for( ; s; s = n)
    { n = s->next;

      PL_unregister_atom(s->language);
      sb_stemmer_delete(s->stemmer);
      PL_free(s);
    }
  }

  PL_free(cache);
}

static void
stem_key_alloc(void)
{ pthread_key_create(&stem_key, stem_destroy_cache);
}

static stem_cache *
get_cache(void)
{ stem_cache *cache;

#ifndef __WINDOWS__
  pthread_once(&stem_key_once, stem_key_alloc);
#endif

  if ( (cache=(stem_cache*)pthread_getspecific(stem_key)) )
    return cache;
  if ( (cache = PL_malloc(sizeof(stem_cache))) )
    memset(cache, 0, sizeof(*cache));

  pthread_setspecific(stem_key, cache);

  return cache;
}


#define ATOM_HASH(a) ((unsigned int)(a>>7) & (STEMMER_BUCKETS-1))

static int
get_lang_stemmer(term_t t, struct sb_stemmer **stemmerp)
{ stem_cache *cache = get_cache();
  atom_t lang;
  int k;
  stemmer *s;
  struct sb_stemmer *st;
  const char *lname;

  if ( !PL_get_atom(t, &lang) )
    return PL_type_error("atom", t);

  k = ATOM_HASH(lang);
  for(s=cache->stemmers[k]; s; s=s->next)
  { if ( s->language == lang )
    { *stemmerp = s->stemmer;
      return TRUE;
    }
  }

  if ( !(lname=PL_atom_chars(lang)) ||
       !(st=sb_stemmer_new(lname, NULL)) )
  { if ( errno == ENOMEM )
      return PL_resource_error("memory");
    else
      return PL_domain_error("snowball_algorithm", t);
  }

  s = PL_malloc(sizeof(*s));
  s->language = lang;
  s->stemmer = st;
  PL_register_atom(lang);

  s->next = cache->stemmers[k];
  cache->stemmers[k] = s;

  *stemmerp = st;
  return TRUE;
}


static foreign_t
snowball(term_t lang, term_t in, term_t out)
{ struct sb_stemmer *stemmer = NULL;
  char *s;
  size_t len, olen;
  const sb_symbol *stemmed;

  if ( !get_lang_stemmer(lang, &stemmer) )
    return FALSE;
  if ( !PL_get_nchars(in, &len, &s,
		      CVT_ATOM|CVT_STRING|CVT_LIST|REP_UTF8|CVT_EXCEPTION) )
    return FALSE;

  if ( !(stemmed = sb_stemmer_stem(stemmer, (const sb_symbol*)s, (int)len)) )
    return PL_resource_error("memory");
  olen = sb_stemmer_length(stemmer);

  return PL_unify_chars(out, PL_ATOM|REP_UTF8, olen, (const char*)stemmed);
}


static foreign_t
snowball_algorithms(term_t list)
{ term_t tail = PL_copy_term_ref(list);
  term_t head = PL_new_term_ref();
  const char **algos = sb_stemmer_list();
  int i;

  for(i=0; algos[i]; i++)
  { if ( !PL_unify_list(tail, head, tail) ||
	 !PL_unify_atom_chars(head, algos[i]) )
      return FALSE;
  }

  return PL_unify_nil(tail);
}

install_t
install_snowball(void)
{ assert(sizeof(sb_symbol) == sizeof(char));

  PL_register_foreign("snowball", 3, snowball, 0);
  PL_register_foreign("snowball_algorithms", 1, snowball_algorithms, 0);

#ifdef __WINDOWS__
  stem_key_alloc();
#endif
}