Lindera Go

Lindera Go is the Go binding of the Lindera morphological analysis engine: the module github.com/lindera/lindera/lindera-go/v7, whose package lindera calls the C API of Lindera (Lindera C) through cgo.

[!NOTE] The binding is experimental, as the C API it calls is (Lindera C). It has the whole of that API: it tokenizes, 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
  • Text processing pipeline: Compose character filters and token filters, with their arguments as Go values
  • 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
  • Dependency analysis: The token filter japanese_dependency writes the bunsetsu of each token and what it depends on
  • Tokens as Go values: A tokenization returns a slice of structs, with nothing to free
  • Goroutines in parallel: Goroutines tokenize with one tokenizer at the same time
  • One program file: By default the program links the static library, and runs by itself

The module and the library

The module is Go code alone. The library it calls is the one of Lindera C, which a release has in an archive for each system, and which is installed apart (Installation). The module and the library have the version of Lindera: the module of a version is used with the library of that version.

How the package works

Lifetimes

A Dictionary, a UserDictionary, a Tokenizer and a TokenizerBuilder hold memory of the library, which Close frees. One that is no longer reachable is freed when the garbage collector finds it, but a program that makes many of them closes them. A call on an object after its Close fails with ErrClosed, and a second Close does nothing.

A Metadata and a Schema hold little, and have no Close: they are freed when they are no longer reachable.

The tokens of a result are Go values. Their strings are parts of one copy of the bytes of the result, so a string that is kept keeps the bytes of its text alive; strings.Clone makes a string that stands alone.

Errors

A failure of the library is an *Error, with the kind of the failure (Kind) and a message (Message). Each kind has an error that errors.Is tells it by:

KindErrorMeaning
KindInvalidArgumentErrInvalidArgumentAn argument is not valid
KindDictionaryErrDictionaryA dictionary could not be found, loaded or resolved
KindBuildErrBuildA tokenizer or another component could not be built
KindIOErrIOAn input or output operation failed
KindParseErrParseData could not be parsed or decoded
KindValidationErrValidationA schema or a record is not valid
KindRuntimeErrRuntimeA failure of no other kind
KindPanicErrPanicThe library panicked, which is a bug of the library
_, err := lindera.LoadDictionary("/no/such/dictionary")
fmt.Println(errors.Is(err, lindera.ErrIO))

var e *lindera.Error
if errors.As(err, &e) {
	fmt.Println(e.Kind)
}

This prints true and io. Which kind a failure has follows how Lindera classifies its own errors, as the statuses of the C API do.

The methods of a TokenizerBuilder that set something return the builder and no error, so that the calls can be chained. The builder keeps the first error, does nothing after it, and Build returns it (Err does too).

Options

What a function can be told and does not need is an option: a value a With... function gives, passed after the arguments the function needs, as in lindera.NewTokenizer(dictionary, lindera.WithMode(mode)). Without an option, the function does what the library does by default.

Goroutines

Goroutines 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 CPUs the program uses (GOMAXPROCS when the tokenizer was made), and a call beyond that waits for a copy. Close waits for the calls that are running.

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

Goroutines may use one Dictionary, one UserDictionary and one Schema at the same time. A TokenizerBuilder and a Metadata, which are changed, are for one goroutine at a time.

Versions

lindera.ModuleVersion is the version of the module, and lindera.Version() the version of the library the program runs with. The package does not compile with a header (lindera.h) of another interface or of an older version than the module, and at run time, with a shared library that is not the one the package was built for, loading a dictionary or making a tokenizer fails with ErrIncompatibleLibrary.

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

Documentation