Lindera .NET

Lindera .NET is the .NET binding of the Lindera morphological analysis engine: the package Lindera on nuget.org, which calls the C API of Lindera (Lindera C) through source-generated P/Invoke. C#, F# and the other .NET languages use it.

[!NOTE] The binding is experimental, as the C API it calls is (Lindera C). It has the whole of that API: it tokenizes (with a builder or from a dictionary that is loaded, with modes, user dictionaries, character filters and token filters, and as the surfaces alone, the N best tokenizations, or several texts in one call), reads and writes the metadata and the schema of a dictionary, builds dictionaries, and trains a dictionary from a corpus.

Features

  • Multi-language support: Tokenize Japanese (IPADIC, IPADIC NEologd, UniDic, JUMAN, SudachiDict), Korean (ko-dic), and Chinese (CC-CEDICT, Jieba) text
  • Modes and user dictionaries: The normal mode and the decompose mode with its penalty, and words a program adds to those of a dictionary
  • N-best results and batches: The N best tokenizations of a text, and several texts in one call, on threads of the library
  • Dictionary management: Build a dictionary and a user dictionary from their source files, and read the metadata and the schema of a dictionary
  • Training: Train a dictionary from an annotated corpus, with the settings of lindera dictionary train
  • Text processing pipeline: Compose character filters and token filters, with their arguments as JSON
  • Dependency analysis: The token filter japanese_dependency writes the bunsetsu of each token and what it depends on
  • Tokens as .NET values: A tokenization returns a list of objects, with nothing to dispose, and offsets that index the .NET string
  • Threads in parallel: Threads tokenize with one tokenizer at the same time
  • One package: The package has the native library for Linux, macOS and Windows on x64 and arm64; Native AOT and trimming are supported

The package and the library

The package has the library of Lindera C for each platform, in runtimes/<rid>/native/, and .NET loads the one of the machine the program runs on (Installation). The package has the version of Lindera, and has the library of that version.

The assembly of the package is Lindera.DotNet.dll, and its namespace Lindera. It is not named Lindera.dll: on Windows, whose file names ignore case, that would be the file of the library, lindera.dll, which a program published for a runtime has next to it.

The package targets .NET 10, and runs on .NET 10 and later.

How the package works

Lifetimes

A Tokenizer, a TokenizerBuilder, a Dictionary, a UserDictionary, a Metadata and a Schema hold memory of the library, which Dispose frees; using disposes them at the end of a scope. One that is not disposed is freed when the garbage collector finds it, but the garbage collector does not see the memory of the library, so a program that makes many of them disposes them. A call after Dispose throws ObjectDisposedException, and a second Dispose does nothing.

The tokens of a result are .NET values: the result of the library is freed before Tokenize returns. The details of a token (Details) are made strings when they are first read; until then the token keeps the bytes of the tokens of its text, which the tokens of one call share.

Offsets

Start and End of a token are indexes of the .NET string (UTF-16 code units), and ByteStart and ByteEnd the offsets in its UTF-8, as the library gives them. text[token.Start..token.End] is the range of the token in the text that was tokenized, which is its surface unless a filter rewrote the surface (lowercase, normalization, readings). A character filter that replaced a range can make an offset fall inside a character: the start is then moved to the start of that character, and the end to its end.

Errors

A failure of the library is a LinderaException, with the kind of the failure (Kind) and the message of the library (Message):

KindMeaning
InvalidArgumentAn argument is not valid
DictionaryA dictionary could not be found, loaded or resolved
BuildA tokenizer or another component could not be built
IOAn input or output operation failed
ParseData could not be parsed or decoded
ValidationA schema or a record is not valid
RuntimeA failure of no other kind
PanicThe library panicked, which is a bug of the library
IncompatibleLibraryThe library that was loaded is not the one of the package: another interface, or an older version
try
{
    Dictionary.Load("/no/such/dictionary");
}
catch (LinderaException e) when (e.Kind == LinderaErrorKind.IO)
{
    Console.WriteLine(e.Message);
}

What .NET can tell before it calls the library throws the exceptions of .NET: ArgumentNullException for a null argument, ArgumentException for a string with a lone surrogate (which has no UTF-8), and ObjectDisposedException after Dispose.

Threads

Threads may tokenize with one Tokenizer at the same time, and their calls run in parallel. A tokenizer of the library serves one call at a time, so the Tokenizer keeps copies of it, which share the dictionary: it makes one when a call finds none free, up to the number of processors (Environment.ProcessorCount when the tokenizer was made), and a call beyond that waits for a copy. A call that runs when the tokenizer is disposed finishes; a call that waits for a copy throws ObjectDisposedException.

The methods that tokenize several texts in one call (TokenizeBatch and the like) run on threads of the library, on the first tokenizer of the library, which keeps those threads, so the batches of one tokenizer take turns.

Threads may use one Dictionary, one UserDictionary and one Schema at the same time, and share a TokenizerBuilder and a Metadata, whose calls take turns. A Mode and a Penalty are values, which hold nothing of the library.

Versions

LinderaLibrary.PackageVersion is the version of the package, and LinderaLibrary.Version the version of the library the program runs with. The first call that makes an object of the library checks that the library has the interface the package is written for and is not older than the package, and throws LinderaException with the kind IncompatibleLibrary otherwise.

LinderaLibrary.HasFeature tells whether the library was built with a feature ("train", "dependency", "embed-ipadic" and so on).

Documentation