aboutsummaryrefslogtreecommitdiff
path: root/liblangdet/langdet.h
blob: d5cb9b356637eae196307b5c35b84a32dc3290e8 (plain) (blame)
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
#ifndef LANGDET_H
#define LANGDET_H

#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

#define LANGDET_NUM_LABELS 20
#define LANGDET_MAX_TOKENS 512

typedef struct langdet langdet_t;

typedef struct {
    const char *label;
    float score;
    int tokens;
    int truncated;
    /* Label pointers belong to the detector and remain valid until langdet_free().
     * Scores are stored in this result, in the checkpoint's id2label order. */
    const char *labels[LANGDET_NUM_LABELS];
    float scores[LANGDET_NUM_LABELS];
} langdet_result_t;

/* Library calls must not overlap across application threads, even for different
 * handles. Internal CPU workers cooperate on one prediction at a time. */

/* Load the model, tokenizer, classifier, and labels from model_dir.
 * n_threads is 1..16, or 0 to detect the CPU count and cap it at 16.
 * On macOS it sets kernel workers; Accelerate manages its own threads.
 * On Linux it sets OpenBLAS threads; the auxiliary kernel pool stays serial.
 * Return NULL on failure. An optional err[errlen] receives a NUL-terminated
 * message on failure and is cleared on success. Model-file readers may also
 * write diagnostics to stderr. */
langdet_t *langdet_open(const char *model_dir, int n_threads, char *err, size_t errlen);

/* Classify NUL-terminated UTF-8 text. Truncate to 512 tokens, retaining </s>.
 * Empty text is accepted but does not yield a meaningful language prediction.
 * Return 0 on success, or -1 with a zeroed result on failure. The optional
 * error buffer follows the same contract as langdet_open(). */
int langdet_predict(langdet_t *detector, const char *text, langdet_result_t *result,
                    char *err, size_t errlen);

/* Release the detector and its resources. NULL is accepted. */
void langdet_free(langdet_t *detector);

#ifdef __cplusplus
}
#endif

#endif /* LANGDET_H */