Quick Start

This guide shows how to tokenize text from a C program with Lindera C.

Basic Tokenization

The program below is lindera-c/examples/tokenize.c. It builds a tokenizer through a builder, tokenizes a text, and prints each token with its details:

/* Tokenizes a text and prints each token with its details, one per line, as
 * `lindera tokenize` does.
 *
 *     tokenize [<dictionary> [<text>]]
 *
 * <dictionary> is the path of a directory that holds a built dictionary, or
 * `embedded://<name>` for a dictionary the library was built with. The
 * default is `embedded://ipadic`. <text> is UTF-8.
 */

#include <stdio.h>
#include <string.h>

#include "lindera.h"

/* Prints what failed with the message of the error, frees the error, and
 * returns the exit code of a failure. */
static int fail(const char *what, LinderaStatus status, LinderaError *error) {
    LinderaStr message = lindera_error_message(error);

    fprintf(stderr, "%s: %s: %.*s\n", what, lindera_status_name(status), (int)message.len,
            (const char *)message.ptr);
    lindera_error_free(error);
    return 1;
}

int main(int argc, char **argv) {
    const char *dictionary = argc > 1 ? argv[1] : "embedded://ipadic";
    const char *text = argc > 2 ? argv[2] : "関西国際空港限定トートバッグ";
    LinderaTokenizerBuilder *builder = NULL;
    LinderaTokenizer *tokenizer = NULL;
    LinderaResult *result = NULL;
    LinderaTokens tokens;
    LinderaError *error = NULL;
    LinderaStatus status;
    size_t i;
    size_t j;

    /* The header this program was compiled with and the library it runs
     * with must be of the same interface. */
    if (lindera_abi_version() != LINDERA_ABI_VERSION) {
        fprintf(stderr, "lindera.h is of the interface %u, and the library of %u\n",
                (unsigned)LINDERA_ABI_VERSION, (unsigned)lindera_abi_version());
        return 1;
    }

    /* A builder collects the settings; the tokenizer is built from them. */
    status = lindera_tokenizer_builder_new(&builder, &error);
    if (status != LINDERA_STATUS_OK) {
        return fail("lindera_tokenizer_builder_new", status, error);
    }
    status = lindera_tokenizer_builder_set_dictionary(builder, (const uint8_t *)dictionary,
                                                      strlen(dictionary), &error);
    if (status == LINDERA_STATUS_OK) {
        status = lindera_tokenizer_builder_build(builder, &tokenizer, &error);
    }
    lindera_tokenizer_builder_free(builder);
    if (status != LINDERA_STATUS_OK) {
        return fail("building the tokenizer", status, error);
    }

    /* The tokens are arrays that belong to the result: they are read
     * without another call, and freed with the result. */
    status = lindera_tokenizer_tokenize(tokenizer, (const uint8_t *)text, strlen(text), &result,
                                        &tokens, &error);
    if (status != LINDERA_STATUS_OK) {
        lindera_tokenizer_free(tokenizer);
        return fail("lindera_tokenizer_tokenize", status, error);
    }
    for (i = 0; i < tokens.len; i++) {
        const LinderaToken *token = &tokens.tokens[i];

        printf("%.*s\t", (int)token->surface.len, (const char *)token->surface.ptr);
        for (j = 0; j < token->details_len; j++) {
            printf("%s%.*s", j == 0 ? "" : ",", (int)token->details[j].len,
                   (const char *)token->details[j].ptr);
        }
        printf("\n");
    }
    printf("EOS\n");

    lindera_result_free(result);
    lindera_tokenizer_free(tokenizer);
    return 0;
}

Compile it against an installed library (see Installation) and run it with the path of a dictionary:

export PKG_CONFIG_PATH="$PWD/dist/lib/pkgconfig"
cc -o tokenize lindera-c/examples/tokenize.c $(pkg-config --cflags --libs lindera)
LD_LIBRARY_PATH="$PWD/dist/lib" ./tokenize /path/to/lindera-ipadic

Note: Download a pre-built dictionary from GitHub Releases and specify the path to the extracted directory. A library built with --features embed-ipadic needs none: the program then uses embedded://ipadic, its default.

Expected output:

関西国際空港    名詞,固有名詞,組織,*,*,*,関西国際空港,カンサイコクサイクウコウ,カンサイコクサイクーコー
限定    名詞,サ変接続,*,*,*,*,限定,ゲンテイ,ゲンテイ
トートバッグ    名詞,一般,*,*,*,*,*,*,*
EOS

A second argument is the text:

LD_LIBRARY_PATH="$PWD/dist/lib" ./tokenize /path/to/lindera-ipadic 東京都に住む
東京    名詞,固有名詞,地域,一般,*,*,東京,トウキョウ,トーキョー
都      名詞,接尾,地域,*,*,*,都,ト,ト
に      助詞,格助詞,一般,*,*,*,に,ニ,ニ
住む    動詞,自立,*,*,五段・マ行,基本形,住む,スム,スム
EOS

What the program does

  1. It checks the interface. lindera_abi_version() must return the LINDERA_ABI_VERSION of the header the program was compiled with.
  2. It builds a tokenizer. A builder collects the settings (here the dictionary alone), and lindera_tokenizer_builder_build makes the tokenizer. The builder is freed afterwards: the tokenizer does not need it.
  3. It tokenizes. lindera_tokenizer_tokenize gives a result, and fills a LinderaTokens with the array of the tokens. The program reads tokens.tokens[i] for i below tokens.len without calling the library again.
  4. It frees what it made. lindera_result_free frees the tokens and every string they point to, and lindera_tokenizer_free the tokenizer.

Each call that can fail returns a status. On a failure the program prints the message of the error object and frees it.

Accessing Token Properties

A token is a struct. Its strings are LinderaStr values, a pointer and a length without a NUL at the end, so they are printed with %.*s:

const LinderaToken *token = &tokens.tokens[i];

printf("Surface: %.*s\n", (int)token->surface.len, (const char *)token->surface.ptr);
printf("Byte range: %zu..%zu\n", token->byte_start, token->byte_end);
printf("Position: %zu\n", token->position);
printf("Word ID: %u\n", (unsigned)token->word_id);
printf("Unknown: %s\n", (token->flags & LINDERA_TOKEN_FLAG_UNKNOWN) != 0 ? "yes" : "no");
printf("Details: %zu\n", token->details_len);

The tokens and their strings are valid until lindera_result_free. A program that keeps a string longer copies it.

Filters

Character filters and token filters are appended to the builder, with their arguments as a JSON object:

static const char normalize[] = "unicode_normalize";
static const char nfkc[] = "{\"kind\":\"nfkc\"}";
static const char lowercase[] = "lowercase";

lindera_tokenizer_builder_append_character_filter(builder, (const uint8_t *)normalize,
                                                  strlen(normalize), (const uint8_t *)nfkc,
                                                  strlen(nfkc), &error);
lindera_tokenizer_builder_append_token_filter(builder, (const uint8_t *)lowercase,
                                              strlen(lowercase), NULL, 0, &error);

With these two, LINDERA is one token, lindera. See Tokenizer API for the functions of the builder.