Debian Codegraph @

plugins/query_expanders/tsv.c

raw symbols SCIP
1 /* Copyright(C) 2012-2015 Brazil
2
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Lesser General Public License for more details.
12
13 You should have received a copy of the GNU Lesser General Public
14 License along with this library; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #ifdef GRN_EMBEDDED
19 # define GRN_PLUGIN_FUNCTION_TAG query_expanders_tsv
20 #endif
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif /* HAVE_CONFIG_H */
25
26 #include <groonga/plugin.h>
27
28 #include <stdlib.h>
29 #include <string.h>
30
31 #ifdef WIN32
32 # include <windows.h>
33 # include <share.h>
34 #endif /* WIN32 */
35
36 #define MAX_SYNONYM_BYTES 4096
37
38 static grn_hash *synonyms = NULL;
39
40 #ifdef WIN32
41 static char win32_synonyms_file[MAX_PATH] = "";
42 const char *
43 get_system_synonyms_file(void)
44 {
45 if (win32_synonyms_file[0] == '\0') {
46 const char *base_dir;
47 const char *relative_path = GRN_QUERY_EXPANDER_TSV_RELATIVE_SYNONYMS_FILE;
48 size_t base_dir_length;
49
50 base_dir = grn_plugin_windows_base_dir();
51 base_dir_length = strlen(base_dir);
52 grn_strcpy(win32_synonyms_file, MAX_PATH, base_dir);
53 grn_strcat(win32_synonyms_file, MAX_PATH, "/");
54 grn_strcat(win32_synonyms_file, MAX_PATH, relative_path);
55 }
56 return win32_synonyms_file;
57 }
58
59 #else /* WIN32 */
60 const char *
61 get_system_synonyms_file(void)
62 {
63 return GRN_QUERY_EXPANDER_TSV_SYNONYMS_FILE;
64 }
65 #endif /* WIN32 */
66
67 static bool
68 is_comment_mark(char character)
69 {
70 return character == '#';
71 }
72
73 static grn_encoding
74 detect_coding_part(grn_ctx *ctx, const char *line, size_t line_length)
75 {
76 grn_encoding encoding = GRN_ENC_NONE;
77 grn_obj null_terminated_line_buffer;
78 const char *c_line;
79 const char *coding_part_keyword = "coding: ";
80 const char *coding_part;
81 const char *encoding_name;
82
83 GRN_TEXT_INIT(&null_terminated_line_buffer, 0);
84 GRN_TEXT_PUT(ctx, &null_terminated_line_buffer, line, line_length);
85 GRN_TEXT_PUTC(ctx, &null_terminated_line_buffer, '\0');
86
87 c_line = GRN_TEXT_VALUE(&null_terminated_line_buffer);
88 coding_part = strstr(c_line, coding_part_keyword);
89 if (coding_part) {
90 encoding_name = coding_part + strlen(coding_part_keyword);
91 if (grn_strncasecmp(encoding_name, "utf-8", strlen("utf-8")) == 0 ||
92 grn_strncasecmp(encoding_name, "utf8", strlen("utf8")) == 0) {
93 encoding = GRN_ENC_UTF8;
94 } else if (grn_strncasecmp(encoding_name, "sjis", strlen("sjis")) == 0 ||
95 grn_strncasecmp(encoding_name, "Shift_JIS", strlen("Shift_JIS")) == 0) {
96 encoding = GRN_ENC_SJIS;
97 } else if (grn_strncasecmp(encoding_name, "EUC-JP", strlen("EUC-JP")) == 0 ||
98 grn_strncasecmp(encoding_name, "euc_jp", strlen("euc_jp")) == 0) {
99 encoding = GRN_ENC_EUC_JP;
100 } else if (grn_strncasecmp(encoding_name, "latin1", strlen("latin1")) == 0) {
101 encoding = GRN_ENC_LATIN1;
102 } else if (grn_strncasecmp(encoding_name, "KOI8-R", strlen("KOI8-R")) == 0 ||
103 grn_strncasecmp(encoding_name, "koi8r", strlen("koi8r")) == 0) {
104 encoding = GRN_ENC_KOI8R;
105 }
106 } else {
107 encoding = ctx->encoding;
108 }
109 GRN_OBJ_FIN(ctx, &null_terminated_line_buffer);
110
111 return encoding;
112 }
113
114 static grn_encoding
115 guess_encoding(grn_ctx *ctx, const char **line, size_t *line_length)
116 {
117 const char bom[] = {(char)0xef, (char)0xbb, (char)0xbf};
118 size_t bom_length = sizeof(bom);
119
120 if (*line_length >= bom_length && memcmp(*line, bom, bom_length) == 0) {
121 *line += bom_length;
122 *line_length -= bom_length;
123 return GRN_ENC_UTF8;
124 }
125
126 if (!is_comment_mark((*line)[0])) {
127 return ctx->encoding;
128 }
129
130 return detect_coding_part(ctx, (*line) + 1, (*line_length) - 1);
131 }
132
133 static void
134 parse_synonyms_file_line(grn_ctx *ctx, const char *line, size_t line_length,
135 grn_obj *key, grn_obj *value)
136 {
137 size_t i = 0;
138
139 if (is_comment_mark(line[i])) {
140 return;
141 }
142
143 while (i < line_length) {
144 char character = line[i];
145 i++;
146 if (character == '\t') {
147 break;
148 }
149 GRN_TEXT_PUTC(ctx, key, character);
150 }
151
152 if (i == line_length) {
153 return;
154 }
155
156 GRN_TEXT_PUTS(ctx, value, "((");
157 while (i < line_length) {
158 char character = line[i];
159 i++;
160 if (character == '\t') {
161 GRN_TEXT_PUTS(ctx, value, ") OR (");
162 } else {
163 GRN_TEXT_PUTC(ctx, value, character);
164 }
165 }
166 GRN_TEXT_PUTS(ctx, value, "))");
167
168 {
169 grn_id id;
170 void *value_location = NULL;
171
172 id = grn_hash_add(ctx,
173 synonyms,
174 GRN_TEXT_VALUE(key),
175 (unsigned int)GRN_TEXT_LEN(key),
176 &value_location,
177 NULL);
178 if (id == GRN_ID_NIL) {
179 GRN_PLUGIN_LOG(ctx, GRN_LOG_WARNING,
180 "[plugin][query-expander][tsv] "
181 "failed to register key: <%.*s>",
182 (int)GRN_TEXT_LEN(key), GRN_TEXT_VALUE(key));
183 return;
184 }
185
186 if (GRN_TEXT_LEN(value) <= MAX_SYNONYM_BYTES - 1) {
187 GRN_TEXT_PUTC(ctx, value, '\0');
188 } else {
189 grn_bulk_truncate(ctx, value, MAX_SYNONYM_BYTES - 1);
190 GRN_TEXT_PUTC(ctx, value, '\0');
191 }
192 grn_memcpy(value_location, GRN_TEXT_VALUE(value), GRN_TEXT_LEN(value));
193 }
194 }
195
196 static void
197 load_synonyms(grn_ctx *ctx)
198 {
199 static char path_env[GRN_ENV_BUFFER_SIZE];
200 const char *path;
201 grn_file_reader *file_reader;
202 int number_of_lines;
203 grn_encoding encoding;
204 grn_obj line, key, value;
205
206 grn_getenv("GRN_QUERY_EXPANDER_TSV_SYNONYMS_FILE",
207 path_env,
208 GRN_ENV_BUFFER_SIZE);
209 if (path_env[0]) {
210 path = path_env;
211 } else {
212 path = get_system_synonyms_file();
213 }
214 file_reader = grn_file_reader_open(ctx, path);
215 if (!file_reader) {
216 GRN_LOG(ctx, GRN_LOG_WARNING,
217 "[plugin][query-expander][tsv] "
218 "synonyms file doesn't exist: <%s>",
219 path);
220 return;
221 }
222
223 GRN_TEXT_INIT(&line, 0);
224 GRN_TEXT_INIT(&key, 0);
225 GRN_TEXT_INIT(&value, 0);
226 grn_bulk_reserve(ctx, &value, MAX_SYNONYM_BYTES);
227 number_of_lines = 0;
228 while (grn_file_reader_read_line(ctx, file_reader, &line) == GRN_SUCCESS) {
229 const char *line_value = GRN_TEXT_VALUE(&line);
230 size_t line_length = GRN_TEXT_LEN(&line);
231
232 if (line_length > 0 && line_value[line_length - 1] == '\n') {
233 if (line_length > 1 && line_value[line_length - 2] == '\r') {
234 line_length -= 2;
235 } else {
236 line_length -= 1;
237 }
238 }
239 number_of_lines++;
240 if (number_of_lines == 1) {
241 encoding = guess_encoding(ctx, &line_value, &line_length);
242 }
243 GRN_BULK_REWIND(&key);
244 GRN_BULK_REWIND(&value);
245 parse_synonyms_file_line(ctx, line_value, line_length, &key, &value);
246 GRN_BULK_REWIND(&line);
247 }
248 GRN_OBJ_FIN(ctx, &line);
249 GRN_OBJ_FIN(ctx, &key);
250 GRN_OBJ_FIN(ctx, &value);
251
252 grn_file_reader_close(ctx, file_reader);
253 }
254
255 static grn_obj *
256 func_query_expander_tsv(grn_ctx *ctx, int nargs, grn_obj **args,
257 grn_user_data *user_data)
258 {
259 grn_rc rc = GRN_END_OF_DATA;
260 grn_id id;
261 grn_obj *term, *expanded_term;
262 void *value;
263 grn_obj *rc_object;
264
265 term = args[0];
266 expanded_term = args[1];
267 id = grn_hash_get(ctx,
268 synonyms,
269 GRN_TEXT_VALUE(term),
270 (unsigned int)GRN_TEXT_LEN(term),
271 &value);
272 if (id != GRN_ID_NIL) {
273 const char *query = value;
274 GRN_TEXT_PUTS(ctx, expanded_term, query);
275 rc = GRN_SUCCESS;
276 }
277
278 rc_object = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_INT32, 0);
279 if (rc_object) {
280 GRN_INT32_SET(ctx, rc_object, rc);
281 }
282
283 return rc_object;
284 }
285
286 grn_rc
287 GRN_PLUGIN_INIT(grn_ctx *ctx)
288 {
289 if (!synonyms) {
290 synonyms = grn_hash_create(ctx, NULL,
291 GRN_TABLE_MAX_KEY_SIZE,
292 MAX_SYNONYM_BYTES,
293 GRN_OBJ_TABLE_HASH_KEY | GRN_OBJ_KEY_VAR_SIZE);
294 if (!synonyms) {
295 return ctx->rc;
296 }
297 load_synonyms(ctx);
298 }
299 return ctx->rc;
300 }
301
302 grn_rc
303 GRN_PLUGIN_REGISTER(grn_ctx *ctx)
304 {
305 grn_proc_create(ctx, "QueryExpanderTSV", strlen("QueryExpanderTSV"),
306 GRN_PROC_FUNCTION,
307 func_query_expander_tsv, NULL, NULL,
308 0, NULL);
309 return GRN_SUCCESS;
310 }
311
312 grn_rc
313 GRN_PLUGIN_FIN(grn_ctx *ctx)
314 {
315 if (synonyms) {
316 grn_hash_close(ctx, synonyms);
317 synonyms = NULL;
318 }
319 return GRN_SUCCESS;
320 }