-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopencc_jieba_capi.h
More file actions
399 lines (367 loc) · 12.2 KB
/
Copy pathopencc_jieba_capi.h
File metadata and controls
399 lines (367 loc) · 12.2 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#ifndef OPENCC_JIEBA_CAPI_H
#define OPENCC_JIEBA_CAPI_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief A Jieba token and its corresponding part-of-speech tag.
*
* Both fields are UTF-8, null-terminated strings.
*
* Arrays returned by this API are terminated by a sentinel entry where both
* `word` and `tag` are NULL.
*/
typedef struct OpenccJiebaTag {
char *word;
char *tag;
} OpenccJiebaTag;
/* =========================================================================
* Metadata
* ========================================================================= */
/**
* @brief Returns the OpenCC-Jieba C ABI version number.
*
* This value is intended for runtime compatibility checks and only changes
* when the C ABI is broken.
*
* @return ABI version number.
*/
uint32_t opencc_jieba_abi_number(void);
/**
* @brief Returns the OpenCC-Jieba version string.
*
* The returned pointer is a UTF-8, null-terminated string valid for the
* lifetime of the program. It must not be freed.
*
* Example: `"0.7.4-beta1"`
*
* @return Version string pointer.
*/
const char *opencc_jieba_version_string(void);
/* =========================================================================
* Lifecycle
* ========================================================================= */
/**
* @brief Creates a new OpenCC-Jieba instance.
*
* The returned instance is used for conversion, segmentation, tagging,
* and keyword extraction.
*
* Destroy it with `opencc_jieba_delete()` when no longer needed.
*
* @return Pointer to a newly allocated instance, or NULL on failure.
*/
void *opencc_jieba_new(void);
/**
* @brief Destroys an OpenCC-Jieba instance.
*
* Passing NULL is safe and has no effect.
*
* @param instance Instance previously returned by `opencc_jieba_new()`.
*/
void opencc_jieba_delete(void *instance);
/**
* @brief Deprecated alias of `opencc_jieba_delete()`.
*
* Passing NULL is safe and has no effect.
*
* @param instance Instance previously returned by `opencc_jieba_new()`.
*/
void opencc_jieba_free(void *instance);
/* =========================================================================
* Conversion
* ========================================================================= */
/**
* @brief Converts text using the specified OpenCC configuration.
*
* @param instance Instance created by `opencc_jieba_new()`.
* @param input Input UTF-8, null-terminated string.
* @param config Conversion config name. Supported values are `"s2t"`, `"s2tw"`,
* `"s2twp"`, `"s2hk"`, `"s2hkp"`, `"t2s"`, `"t2tw"`, `"t2twp"`,
* `"t2hk"`, `"t2hkp"`, `"tw2s"`, `"tw2sp"`, `"tw2t"`, `"tw2tp"`,
* `"hk2s"`, `"hk2sp"`, `"hk2t"`, `"hk2tp"`, `"jp2t"`, or `"t2jp"`.
* @param punctuation Whether punctuation conversion is enabled.
*
* @return Newly allocated UTF-8 string. Free with `opencc_jieba_free_string()`.
* Returns NULL on error.
*/
char *opencc_jieba_convert(
const void *instance,
const char *input,
const char *config,
bool punctuation
);
/**
* @brief Checks whether input text is Simplified or Traditional Chinese.
*
* Return values:
* - `0` = mixed / undetermined
* - `1` = Traditional Chinese
* - `2` = Simplified Chinese
* - `-1` = invalid input or error
*
* @param instance Instance created by `opencc_jieba_new()`.
* @param input Input UTF-8, null-terminated string.
*
* @return Status code described above.
*/
int opencc_jieba_zho_check(const void *instance, const char *input);
/* =========================================================================
* Segmentation and tagging
* ========================================================================= */
/**
* @brief Segments text using Jieba default cut mode.
*
* @param instance Instance created by `opencc_jieba_new()`.
* @param input Input UTF-8, null-terminated string.
* @param hmm Whether to enable HMM new-word discovery.
*
* @return NULL-terminated array of UTF-8 strings.
* Free with `opencc_jieba_free_string_array()`.
* Returns NULL on error.
*/
char **opencc_jieba_cut(const void *instance, const char *input, bool hmm);
/**
* @brief Segments text using Jieba full mode.
*
* @param instance Instance created by `opencc_jieba_new()`.
* @param input Input UTF-8, null-terminated string.
*
* @return NULL-terminated array of UTF-8 strings.
* Free with `opencc_jieba_free_string_array()`.
* Returns NULL on error.
*/
char **opencc_jieba_cut_all(const void *instance, const char *input);
/**
* @brief Segments text using Jieba search mode.
*
* @param instance Instance created by `opencc_jieba_new()`.
* @param input Input UTF-8, null-terminated string.
* @param hmm Whether to enable HMM new-word discovery.
*
* @return NULL-terminated array of UTF-8 strings.
* Free with `opencc_jieba_free_string_array()`.
* Returns NULL on error.
*/
char **opencc_jieba_cut_for_search(const void *instance, const char *input, bool hmm);
/**
* @brief Segments text and joins the tokens with a delimiter.
*
* @param instance Instance created by `opencc_jieba_new()`.
* @param input Input UTF-8, null-terminated string.
* @param hmm Whether to enable HMM new-word discovery.
* @param delimiter UTF-8 delimiter inserted between tokens.
*
* @return Newly allocated UTF-8 string.
* Free with `opencc_jieba_free_string()`.
* Returns NULL on error.
*/
char *opencc_jieba_cut_and_join(
const void *instance,
const char *input,
bool hmm,
const char *delimiter
);
/**
* @brief Performs Jieba part-of-speech tagging.
*
* @param instance Instance created by `opencc_jieba_new()`.
* @param input Input UTF-8, null-terminated string.
* @param hmm Whether to enable HMM new-word discovery.
*
* @return Sentinel-terminated array of `OpenccJiebaTag`.
* Free with `opencc_jieba_free_tag_array()`.
* Returns NULL on error.
*/
OpenccJiebaTag *opencc_jieba_tag(const void *instance, const char *input, bool hmm);
/**
* @brief Joins a NULL-terminated string array using a delimiter.
*
* @param strings NULL-terminated array of UTF-8 strings.
* @param delimiter UTF-8 delimiter inserted between strings.
*
* @return Newly allocated UTF-8 string.
* Free with `opencc_jieba_free_string()`.
* Returns NULL on error.
*/
char *opencc_jieba_join_str(const char *const *strings, const char *delimiter);
/* =========================================================================
* Keyword extraction
* ========================================================================= */
/**
* @brief Extracts top keywords using TextRank or TF-IDF.
*
* `method` must be `"textrank"` or `"tfidf"`.
*
* @param instance Instance created by `opencc_jieba_new()`.
* @param input Input UTF-8, null-terminated string.
* @param top_k Maximum number of keywords to return.
* @param method Keyword extraction method: `"textrank"` or `"tfidf"`.
*
* @return NULL-terminated array of UTF-8 keyword strings.
* Free with `opencc_jieba_free_string_array()`.
* Returns NULL on error.
*/
char **opencc_jieba_keywords(
const void *instance,
const char *input,
size_t top_k,
const char *method
);
/**
* @brief Extracts top keywords using TextRank or TF-IDF with optional POS filtering.
*
* `method` must be `"textrank"` or `"tfidf"`.
*
* `allowed_pos` is a UTF-8, null-terminated, space-separated POS list such as:
* `"n nr ns nt nz v vn"`.
*
* If `allowed_pos` is NULL or an empty string, no POS filtering is applied.
*
* @param instance Instance created by `opencc_jieba_new()`.
* @param input Input UTF-8, null-terminated string.
* @param top_k Maximum number of keywords to return.
* @param method Keyword extraction method: `"textrank"` or `"tfidf"`.
* @param allowed_pos Optional space-separated POS filter list.
*
* @return NULL-terminated array of UTF-8 keyword strings.
* Free with `opencc_jieba_free_string_array()`.
* Returns NULL on error.
*/
char **opencc_jieba_keywords_pos(
const void *instance,
const char *input,
size_t top_k,
const char *method,
const char *allowed_pos
);
/**
* @brief Extracts keywords and their weights using TextRank or TF-IDF.
*
* `method` must be `"textrank"` or `"tfidf"`.
*
* On success:
* - `*out_len` receives the number of keywords
* - `*out_keywords` receives an array of UTF-8 strings
* - `*out_weights` receives an array of `double`
*
* Free both output arrays with `opencc_jieba_free_keywords_and_weights()`.
*
* @param instance Instance created by `opencc_jieba_new()`.
* @param input Input UTF-8, null-terminated string.
* @param top_k Maximum number of keywords to return.
* @param method Keyword extraction method: `"textrank"` or `"tfidf"`.
* @param out_len Output keyword count.
* @param out_keywords Output keyword array.
* @param out_weights Output weight array.
*
* @return `0` on success, negative value on error.
*/
int32_t opencc_jieba_keywords_and_weights(
const void *instance,
const char *input,
size_t top_k,
const char *method,
size_t *out_len,
char ***out_keywords,
double **out_weights
);
/**
* @brief Extracts keywords and their weights using TextRank or TF-IDF
* with optional POS filtering.
*
* `method` must be `"textrank"` or `"tfidf"`.
*
* `allowed_pos` is a UTF-8, null-terminated, space-separated POS list such as:
* `"n nr ns nt nz v vn"`.
*
* If `allowed_pos` is NULL or an empty string, no POS filtering is applied.
*
* On success:
* - `*out_len` receives the number of keywords
* - `*out_keywords` receives an array of UTF-8 strings
* - `*out_weights` receives an array of `double`
*
* Free both output arrays with `opencc_jieba_free_keywords_and_weights()`.
*
* @param instance Instance created by `opencc_jieba_new()`.
* @param input Input UTF-8, null-terminated string.
* @param top_k Maximum number of keywords to return.
* @param method Keyword extraction method: `"textrank"` or `"tfidf"`.
* @param allowed_pos Optional space-separated POS filter list.
* @param out_len Output keyword count.
* @param out_keywords Output keyword array.
* @param out_weights Output weight array.
*
* @return `0` on success, negative value on error.
*/
int32_t opencc_jieba_keywords_and_weights_pos(
const void *instance,
const char *input,
size_t top_k,
const char *method,
const char *allowed_pos,
size_t *out_len,
char ***out_keywords,
double **out_weights
);
/* =========================================================================
* Memory management
* ========================================================================= */
/**
* @brief Frees a string returned by this API.
*
* Safe to call with NULL.
*
* @param ptr String pointer previously returned by this API.
*/
void opencc_jieba_free_string(char *ptr);
/**
* @brief Frees a NULL-terminated string array returned by this API.
*
* Safe to call with NULL.
*
* This is used for arrays returned by:
* - `opencc_jieba_cut()`
* - `opencc_jieba_cut_all()`
* - `opencc_jieba_cut_for_search()`
* - `opencc_jieba_keywords()`
* - `opencc_jieba_keywords_pos()`
*
* @param array NULL-terminated array of UTF-8 strings.
*/
void opencc_jieba_free_string_array(char **array);
/**
* @brief Frees a sentinel-terminated tag array returned by `opencc_jieba_tag()`.
*
* Safe to call with NULL.
*
* @param array Array terminated by an entry where both `word` and `tag` are NULL.
*/
void opencc_jieba_free_tag_array(OpenccJiebaTag *array);
/**
* @brief Frees arrays returned by weighted keyword extraction functions.
*
* Safe to call with NULL pointers.
*
* This is used for outputs returned by:
* - `opencc_jieba_keywords_and_weights()`
* - `opencc_jieba_keywords_and_weights_pos()`
*
* @param keywords Array of UTF-8 keyword strings.
* @param weights Array of keyword weights.
* @param len Number of elements in both arrays.
*/
void opencc_jieba_free_keywords_and_weights(
char **keywords,
double *weights,
size_t len
);
#ifdef __cplusplus
}
#endif
#endif /* OPENCC_JIEBA_CAPI_H */