Debian Codegraph @

plugins/functions/language_model.c

raw symbols SCIP
1 // Copyright (C) 2024 Sutou Kouhei <[email protected]>cxx . . $ `<file>/plugins/functions/language_model.c`/
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 #ifdef GRN_EMBEDDED
18 # define GRN_PLUGIN_FUNCTION_TAG functions_language_model
19 #endif
20
21 #include <groonga/plugin.h>cxx . . $ `<file>/include/groonga/plugin.h`/
22
23 static grn_objcxx . . $ grn_obj# *
24 func_language_model_vectorizecxx . . $ func_language_model_vectorize(fd082858389c8bb0).(grn_ctxcxx . . $ grn_ctx# *ctx,
25 int n_args,
26 grn_objcxx . . $ grn_obj# **args,
27 grn_user_datacxx . . $ grn_user_data# *user_data)
28 {
29 const char *tag = "language_model_vectorize():";
30
31 if (!(n_args == 2 || n_args == 3)) {
32 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx,
33 GRN_INVALID_ARGUMENT,
34 "%s wrong number of arguments (%d for 2..3)",
35 tag,
36 n_args);
37 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
38 }
39
40 grn_objcxx . . $ grn_obj# *vector = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
41 grn_language_model_loadercxx . . $ grn_language_model_loader# *loader = grn_language_model_loader_opencxx . . $ grn_language_model_loader_open(449f1e63bfdae4ae).(ctx);
42 grn_language_modelcxx . . $ grn_language_model# *model = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
43 grn_language_model_inferencercxx . . $ grn_language_model_inferencer# *inferencer = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
44
45 grn_objcxx . . $ grn_obj# *model_name = args[0];
46 if (!grn_obj_is_text_family_bulkcxx . . $ grn_obj_is_text_family_bulk(a5760e5c319a06ee).(ctx, model_name)) {
47 grn_objcxx . . $ grn_obj# inspected;
48 GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&inspected, 0);
49 grn_inspectcxx . . $ grn_inspect(88456f97fe0d41a).(ctx, &inspected, model_name);
50 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx,
51 GRN_INVALID_ARGUMENT,
52 "%s the 1st argument must be model name: %.*s",
53 tag,
54 (int)(GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&inspected)),
55 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&inspected));
56 GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &inspected);
57 goto exit;
58 }
59 grn_language_model_loader_set_modelcxx . . $ grn_language_model_loader_set_model(f84c68e346dfb663).(ctx,
60 loader,
61 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(model_name),
62 GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(model_name));
63
64 grn_objcxx . . $ grn_obj# *text = args[1];
65 if (!grn_obj_is_text_family_bulkcxx . . $ grn_obj_is_text_family_bulk(a5760e5c319a06ee).(ctx, text)) {
66 grn_objcxx . . $ grn_obj# inspected;
67 GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&inspected, 0);
68 grn_inspectcxx . . $ grn_inspect(88456f97fe0d41a).(ctx, &inspected, text);
69 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx,
70 GRN_INVALID_ARGUMENT,
71 "%s the 2nd argument must be text to vectorize: %.*s",
72 tag,
73 (int)(GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&inspected)),
74 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&inspected));
75 GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &inspected);
76 goto exit;
77 }
78
79 grn_objcxx . . $ grn_obj# *options = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
80 if (n_args == 3) {
81 options = args[2];
82 }
83
84 grn_objcxx . . $ grn_obj# *prefix = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
85 if (options) {
86 grn_rccxx . . $ grn_rc# rc = grn_proc_options_parsecxx . . $ grn_proc_options_parse(b3b87877968531ef).(ctx,
87 options,
88 tag,
89 "prefix",
90 GRN_PROC_OPTION_VALUE_RAWcxx . . $ GRN_PROC_OPTION_VALUE_RAW.,
91 &prefix,
92 NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!);
93 if (rc != GRN_SUCCESScxx . . $ GRN_SUCCESS.) {
94 goto exit;
95 }
96
97 if (prefix && !grn_obj_is_text_family_bulkcxx . . $ grn_obj_is_text_family_bulk(a5760e5c319a06ee).(ctx, prefix)) {
98 grn_objcxx . . $ grn_obj# inspected;
99 GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&inspected, 0);
100 grn_inspectcxx . . $ grn_inspect(88456f97fe0d41a).(ctx, &inspected, prefix);
101 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx,
102 GRN_INVALID_ARGUMENT,
103 "%s prefix must be a string: %.*s",
104 tag,
105 (int)(GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&inspected)),
106 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&inspected));
107 GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &inspected);
108 goto exit;
109 }
110 }
111
112 model = grn_language_model_loader_loadcxx . . $ grn_language_model_loader_load(8873608a2279cb92).(ctx, loader);
113 if (!model) {
114 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx,
115 ctx->rc,
116 "%s failed to load model: %s",
117 tag,
118 ctx->errbuf);
119 goto exit;
120 }
121
122 inferencer = grn_language_model_open_inferencercxx . . $ grn_language_model_open_inferencer(7126e52e5c2ba362).(ctx, model);
123 if (!inferencer) {
124 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx,
125 ctx->rc,
126 "%s failed to open model inferencer: %s",
127 tag,
128 ctx->errbuf);
129 goto exit;
130 }
131
132 vector =
133 grn_plugin_proc_alloccxx . . $ grn_plugin_proc_alloc(acb267eca6702fc).(ctx, user_data, GRN_DB_FLOAT32cxx . . $ GRN_DB_FLOAT32., GRN_OBJ_VECTORcxx . . $ `include/groonga/groonga.h:2734:9`!);
134 if (!vector) {
135 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
136 }
137
138 grn_rccxx . . $ grn_rc# rc;
139 if (prefix) {
140 grn_objcxx . . $ grn_obj# prefixed_text;
141 GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&prefixed_text, 0);
142 GRN_TEXT_PUTcxx . . $ `include/groonga/groonga.h:2773:9`!(ctx,
143 &prefixed_text,
144 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(prefix),
145 GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(prefix));
146 GRN_TEXT_PUTcxx . . $ `include/groonga/groonga.h:2773:9`!(ctx, &prefixed_text, GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(text), GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(text));
147 rc = grn_language_model_inferencer_vectorizecxx . . $ grn_language_model_inferencer_vectorize(b74c11687486a08e).(ctx,
148 inferencer,
149 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&prefixed_text),
150 GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&prefixed_text),
151 vector);
152 GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &prefixed_text);
153 } else {
154 rc = grn_language_model_inferencer_vectorizecxx . . $ grn_language_model_inferencer_vectorize(b74c11687486a08e).(ctx,
155 inferencer,
156 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(text),
157 GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(text),
158 vector);
159 }
160
161 if (rc != GRN_SUCCESScxx . . $ GRN_SUCCESS.) {
162 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx,
163 ctx->rc,
164 "%s failed to vectorize: %s",
165 tag,
166 ctx->errbuf);
167 grn_obj_closecxx . . $ grn_obj_close(4865d068a46d0f83).(ctx, vector);
168 vector = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
169 goto exit;
170 }
171
172 exit:
173 grn_language_model_inferencer_closecxx . . $ grn_language_model_inferencer_close(7e6a80401e78dd34).(ctx, inferencer);
174 grn_language_model_closecxx . . $ grn_language_model_close(4e0bbe6dc2c3a62b).(ctx, model);
175 grn_language_model_loader_closecxx . . $ grn_language_model_loader_close(7eb96cf170971ec2).(ctx, loader);
176
177 return vector;
178 }
179
180 static grn_rccxx . . $ grn_rc#
181 applier_language_model_vectorizecxx . . $ applier_language_model_vectorize(6580e31ad1f8e719).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_applier_datacxx . . $ grn_applier_data# *data)
182 {
183 const char *tag = "language_model_vectorize():";
184
185 size_t n_args;
186 grn_objcxx . . $ grn_obj# **args = grn_applier_data_get_argscxx . . $ grn_applier_data_get_args(f080cb29cc6f2c0a).(ctx, data, &n_args);
187
188 if (!(n_args == 2 || n_args == 3)) {
189 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx,
190 GRN_INVALID_ARGUMENT,
191 "%s wrong number of arguments (%d for 2..3)",
192 tag,
193 (uint32_t)n_args);
194 return ctx->rccxx . . $ _grn_ctx#rc.;
195 }
196
197 grn_language_model_loadercxx . . $ grn_language_model_loader# *loader = grn_language_model_loader_opencxx . . $ grn_language_model_loader_open(449f1e63bfdae4ae).(ctx);
198 grn_language_modelcxx . . $ grn_language_model# *model = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
199 grn_language_model_inferencercxx . . $ grn_language_model_inferencer# *inferencer = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
200
201 grn_objcxx . . $ grn_obj# *model_name = args[0];
202 if (!grn_obj_is_text_family_bulkcxx . . $ grn_obj_is_text_family_bulk(a5760e5c319a06ee).(ctx, model_name)) {
203 grn_objcxx . . $ grn_obj# inspected;
204 GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&inspected, 0);
205 grn_inspectcxx . . $ grn_inspect(88456f97fe0d41a).(ctx, &inspected, model_name);
206 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx,
207 GRN_INVALID_ARGUMENT,
208 "%s the 1st argument must be model name: %.*s",
209 tag,
210 (int)(GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&inspected)),
211 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&inspected));
212 GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &inspected);
213 goto exit;
214 }
215 grn_language_model_loader_set_modelcxx . . $ grn_language_model_loader_set_model(f84c68e346dfb663).(ctx,
216 loader,
217 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(model_name),
218 GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(model_name));
219
220 grn_objcxx . . $ grn_obj# *input_column = args[1];
221 if (!(grn_obj_is_text_family_scalar_columncxx . . $ grn_obj_is_text_family_scalar_column(a5760e5c319a06ee).(ctx, input_column) ||
222 grn_obj_is_text_family_scalar_accessorcxx . . $ grn_obj_is_text_family_scalar_accessor(a5760e5c319a06ee).(ctx, input_column))) {
223 grn_objcxx . . $ grn_obj# inspected;
224 GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&inspected, 0);
225 grn_inspectcxx . . $ grn_inspect(88456f97fe0d41a).(ctx, &inspected, input_column);
226 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx,
227 GRN_INVALID_ARGUMENT,
228 "%s the 2nd argument must be a text family column "
229 "or accessor to vectorize: %.*s",
230 tag,
231 (int)(GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&inspected)),
232 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&inspected));
233 GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &inspected);
234 goto exit;
235 }
236
237 grn_objcxx . . $ grn_obj# *options = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
238 if (n_args == 3) {
239 options = args[2];
240 }
241
242 model = grn_language_model_loader_loadcxx . . $ grn_language_model_loader_load(8873608a2279cb92).(ctx, loader);
243 if (!model) {
244 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx,
245 ctx->rc,
246 "%s failed to load model: %s",
247 tag,
248 ctx->errbuf);
249 goto exit;
250 }
251
252 inferencer = grn_language_model_open_inferencercxx . . $ grn_language_model_open_inferencer(7126e52e5c2ba362).(ctx, model);
253 if (!inferencer) {
254 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx,
255 ctx->rc,
256 "%s failed to open model inferencer: %s",
257 tag,
258 ctx->errbuf);
259 goto exit;
260 }
261
262 if (options) {
263 grn_objcxx . . $ grn_obj# *prefix = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
264 grn_rccxx . . $ grn_rc# rc = grn_proc_options_parsecxx . . $ grn_proc_options_parse(b3b87877968531ef).(ctx,
265 options,
266 tag,
267 "prefix",
268 GRN_PROC_OPTION_VALUE_RAWcxx . . $ GRN_PROC_OPTION_VALUE_RAW.,
269 &prefix,
270 NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!);
271 if (rc != GRN_SUCCESScxx . . $ GRN_SUCCESS.) {
272 goto exit;
273 }
274
275 if (prefix && !grn_obj_is_text_family_bulkcxx . . $ grn_obj_is_text_family_bulk(a5760e5c319a06ee).(ctx, prefix)) {
276 grn_objcxx . . $ grn_obj# inspected;
277 GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&inspected, 0);
278 grn_inspectcxx . . $ grn_inspect(88456f97fe0d41a).(ctx, &inspected, prefix);
279 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx,
280 GRN_INVALID_ARGUMENT,
281 "%s prefix must be a string: %.*s",
282 tag,
283 (int)(GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&inspected)),
284 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&inspected));
285 GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &inspected);
286 goto exit;
287 }
288 if (prefix && GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(prefix) > 0) {
289 grn_language_model_inferencer_set_input_column_value_prefixcxx . . $ grn_language_model_inferencer_set_input_column_value_prefix(ac8f96ddd5ed290d).(
290 ctx,
291 inferencer,
292 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(prefix),
293 (int64_tcxx . . $ int64_t#)GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(prefix));
294 if (ctx->rccxx . . $ _grn_ctx#rc. != GRN_SUCCESScxx . . $ GRN_SUCCESS.) {
295 goto exit;
296 }
297 }
298 }
299
300 grn_rccxx . . $ grn_rc# rc = grn_language_model_inferencer_vectorize_appliercxx . . $ grn_language_model_inferencer_vectorize_applier(beb1711181908fad).(ctx,
301 inferencer,
302 input_column,
303 data);
304 if (rc != GRN_SUCCESScxx . . $ GRN_SUCCESS.) {
305 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx,
306 ctx->rc,
307 "%s failed to vectorize: %s",
308 tag,
309 ctx->errbuf);
310 goto exit;
311 }
312
313 exit:
314 grn_language_model_inferencer_closecxx . . $ grn_language_model_inferencer_close(7e6a80401e78dd34).(ctx, inferencer);
315 grn_language_model_closecxx . . $ grn_language_model_close(4e0bbe6dc2c3a62b).(ctx, model);
316 grn_language_model_loader_closecxx . . $ grn_language_model_loader_close(7eb96cf170971ec2).(ctx, loader);
317
318 return ctx->rccxx . . $ _grn_ctx#rc.;
319 }
320
321 grn_rccxx . . $ grn_rc#
322 GRN_PLUGIN_INITcxx . . $ `include/groonga/plugin.h:43:9`!(grn_ctxcxx . . $ grn_ctx# *ctx)
323 {
324 return ctx->rccxx . . $ _grn_ctx#rc.;
325 }
326
327 grn_rccxx . . $ grn_rc#
328 GRN_PLUGIN_REGISTERcxx . . $ `include/groonga/plugin.h:44:9`!(grn_ctxcxx . . $ grn_ctx# *ctx)
329 {
330 grn_rccxx . . $ grn_rc# rc = GRN_SUCCESScxx . . $ GRN_SUCCESS.;
331
332 {
333 grn_objcxx . . $ grn_obj# *proc = grn_proc_createcxx . . $ grn_proc_create(195df001e5324f57).(ctx,
334 "language_model_vectorize",
335 -1,
336 GRN_PROC_FUNCTIONcxx . . $ GRN_PROC_FUNCTION.,
337 func_language_model_vectorizecxx . . $ func_language_model_vectorize(fd082858389c8bb0).,
338 NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!,
339 NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!,
340 0,
341 NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!);
342 grn_proc_set_appliercxx . . $ grn_proc_set_applier(5e518908f149fea).(ctx, proc, applier_language_model_vectorizecxx . . $ applier_language_model_vectorize(6580e31ad1f8e719).);
343 }
344
345 return rc;
346 }
347
348 grn_rccxx . . $ grn_rc#
349 GRN_PLUGIN_FINcxx . . $ `include/groonga/plugin.h:45:9`!(grn_ctxcxx . . $ grn_ctx# *ctx)
350 {
351 return GRN_SUCCESScxx . . $ GRN_SUCCESS.;
352 }