Lindera C

Lindera C is a C API for the Lindera morphological analysis engine: a header (lindera.h) and a library, built from the crate lindera-c. C and C++ programs use it directly, and so do the languages that call C, such as Go (cgo) and C# (P/Invoke).

[!NOTE] The API is experimental. It can change in a minor version of Lindera; LINDERA_ABI_VERSION changes when it does (see Versions). It tokenizes (one text or several in one call, the best tokenization or the N best, with a mode, a user dictionary, and character and token filters), reads the metadata and the schema of a dictionary, builds dictionaries, and trains them from a corpus.

Features

  • Multi-language support: Tokenize Japanese (IPADIC, IPADIC NEologd, UniDic, JUMAN, SudachiDict), Korean (ko-dic), and Chinese (CC-CEDICT, Jieba) text
  • Text processing pipeline: Compose character filters and token filters, with their arguments as JSON
  • Multiple tokenization modes: Normal and decompose modes, with the penalty of decompose mode
  • N-best tokenization: Retrieve several tokenization candidates ranked by cost
  • User dictionaries: Extend a dictionary with the words of a CSV file
  • Batches: Tokenize several texts in one call, on several threads
  • Dictionary building: Build a dictionary and a user dictionary from their source files, with the metadata and the schema of a dictionary
  • Training: Train a model from an annotated corpus, and export the source files of a dictionary from it
  • Dependency analysis: The token filter japanese_dependency writes the bunsetsu of each token and what it depends on
  • One call per text: A tokenization returns arrays of structs that a program reads without calling the library again
  • Shared and static libraries, with a pkg-config file

The files

FileWhat it is
lindera.hThe header: every type, constant and function
liblindera.so (Linux), liblindera.dylib (macOS), lindera.dll with its import library lindera.dll.lib (Windows)The shared library
liblindera.a (Linux, macOS), lindera.lib (Windows)The static library
lindera.pcThe pkg-config file. Its Libs.private names what a program that links the static library needs

The rules of the API

Every function follows the same rules, which the top of lindera.h repeats.

Objects

An object (a dictionary, a user dictionary, a metadata, a schema, the definition of a field, a mode, a penalty, a builder, a tokenizer, a result, the settings of a training, an error) is an opaque struct behind a pointer. A function that returns such a pointer, or stores one through its out argument, makes an object, which the caller frees with lindera_<type>_free: lindera_dictionary_load and lindera_tokenizer_builder_build make one, and so does a function that gives a copy, such as lindera_mode_penalty or lindera_dictionary_metadata. Freeing NULL does nothing. No other function takes the ownership of an argument: what a program made, it frees, once.

Errors

A function that can fail returns a LinderaStatus and takes LinderaError **error as its last argument.

StatusValueMeaning
LINDERA_STATUS_OK0The call succeeded
LINDERA_STATUS_INVALID_ARGUMENT1An argument is not valid
LINDERA_STATUS_DICTIONARY2A dictionary could not be found, loaded or resolved
LINDERA_STATUS_BUILD3A tokenizer or another component could not be built
LINDERA_STATUS_IO4An input or output operation failed
LINDERA_STATUS_PARSE5Data could not be parsed or decoded
LINDERA_STATUS_VALIDATION6A schema or a record is not valid
LINDERA_STATUS_RUNTIME7A failure of no other kind
LINDERA_STATUS_PANIC8The library panicked, which is a bug of the library

Which status a failure has follows how Lindera classifies its own errors: a dictionary path that does not exist is LINDERA_STATUS_IO, for example, and a tokenizer that cannot be built from its settings is LINDERA_STATUS_PARSE. A program treats every value but LINDERA_STATUS_OK as a failure, and reads the message.

When a call fails and error is not NULL, the function stores an error object there. lindera_error_message reads its message, and lindera_error_free frees it. A program that needs the status alone passes NULL. The library keeps no error per thread, so a program whose work moves between threads (a goroutine, a task of a thread pool) reads the error it was given.

LinderaError *error = NULL;
LinderaStatus status = lindera_tokenizer_builder_build(builder, &tokenizer, &error);
if (status != LINDERA_STATUS_OK) {
    LinderaStr message = lindera_error_message(error);
    fprintf(stderr, "%s: %.*s\n", lindera_status_name(status), (int)message.len,
            (const char *)message.ptr);
    lindera_error_free(error);
}

Text

Text goes into the library as a pointer and a length (const uint8_t *ptr, size_t len), in UTF-8. It needs no NUL at its end, and may have one inside. Bytes that are not UTF-8 are LINDERA_STATUS_INVALID_ARGUMENT. A path is text too.

Text comes out as a LinderaStr, a pointer and a length. It borrows from the object it was read from, and is valid until that object is freed. It does not end with a NUL: print it with %.*s, or copy it. Only static text (a version, the name of a status) is a const char * that ends with a NUL.

Threads

Any thread may use or free any object. Functions that take a const pointer may run at the same time on the same object; the others (the functions that change a builder, and the _free functions) need the program to keep them apart.

Several threads may tokenize with one tokenizer at the same time: the calls take turns. To tokenize in parallel, give each thread a tokenizer of its own: lindera_tokenizer_clone makes a copy of one.

Panics

A panic inside the library does not leave it: the function returns LINDERA_STATUS_PANIC, with the message of the panic in the error object, and the process goes on. The panic hook of Rust still writes a line to the standard error. A function that returns a value returns the value its comment in the header gives.

Versions

NameWhat it is
LINDERA_VERSION_MAJOR, _MINOR, _PATCHThe version of Lindera the header belongs to
lindera_version()The version of the library, as text ("7.3.0")
lindera_version_number()The same as a number: major * 10000 + minor * 100 + patch
LINDERA_ABI_VERSIONThe version of the binary interface the header describes
lindera_abi_version()The version of the binary interface of the library

A header and a library agree when lindera_abi_version() returns LINDERA_ABI_VERSION. A program compares the two when it starts, and does not use a library that returns another value. The number changes when a function is removed or changes its arguments, or when a struct or a constant of the header changes; adding a function does not change it.

The library has the same functions in every build. lindera_has_feature tells whether it was built with a feature ("train", "dependency", "embed-ipadic" and so on).

Documentation