Text Processing Pipeline

A tokenizer can change the text before it tokenizes it, and the tokens after: character filters run on the text, and token filters on the tokens. In Lindera Go a filter is appended to a TokenizerBuilder, and the filters run in the order they were appended.

Input Text
  --> Character Filters (preprocessing)
  --> Tokenization
  --> Token Filters (postprocessing)
  --> Output Tokens

[!NOTE] This page shows a few commonly used filters as examples -- it is not the complete list. See Filters for the full, authoritative catalogue of every character and token filter that lindera-analysis ships, including parameters and examples.

Appending a filter

Method of TokenizerBuilderWhat it does
AppendCharacterFilter(kind string, args any)Appends a character filter
AppendTokenFilter(kind string, args any)Appends a token filter

kind is the name of the filter, and args its arguments: nil for none, or a Go value that encoding/json encodes as a JSON object. A map is one, and so is a struct with json tags:

type stopTags struct {
	Tags []string `json:"tags"`
}
tokenizer, err := lindera.NewTokenizerBuilder().
	SetDictionary("embedded://ipadic").
	AppendTokenFilter("japanese_stop_tags", stopTags{Tags: []string{"助詞,係助詞", "助動詞"}}).
	Build()

With this filter, 東京は晴れです gives 東京 / 晴れ. (embedded://ipadic names the dictionary a library was built with; with the library of a release it is the path of a directory.)

A json.RawMessage is the JSON text it holds, for arguments a program has as text. A Go string is not: it is encoded as a JSON string, which no filter takes.

The methods return the builder, and the builder keeps the first error, which Build returns. Arguments that cannot be encoded, or that are no JSON object, are such an error, of the kind KindInvalidArgument. A name that is no filter, and arguments a filter does not take, fail when Build builds the tokenizer.

A tokenizer made with NewTokenizer has no filters; a builder makes one with filters.

Character Filters

Character filters transform the input text before tokenization. The last column is what the filter does to a text, with IPADIC.

FilterArgumentsWhat it doesExample
unicode_normalizemap[string]any{"kind": "nfkc"}Applies a Unicode normalization form: nfc, nfkc, nfd or nfkdLindera becomes Lindera
mappingmap[string]any{"mapping": map[string]string{"リンデラ": "Lindera"}}Replaces the strings that are the keys of a table with their valuesリンデラは becomes Lindera / は
japanese_iteration_markmap[string]any{"normalize_kanji": true, "normalize_kana": true}Replaces the Japanese iteration marks (odoriji) with the characters they repeat時々 becomes 時時

Token Filters

Token filters transform or remove tokens after tokenization. The last column is what the filter does to the tokens of a text, with IPADIC.

FilterArgumentsWhat it doesExample
lowercasenilConverts the surfaces of the tokens to lower caseLindera becomes lindera
japanese_base_formnilReplaces an inflected form with its base (dictionary) form, from the details of the dictionary食べ / た becomes 食べる / た
japanese_stop_tagsmap[string]any{"tags": []string{"助詞,格助詞,一般", "助詞,係助詞", "助詞,連体化", "助動詞"}}Removes the tokens whose part of speech is one of the tags東京 / は / 晴れ / です becomes 東京 / 晴れ
japanese_keep_tagsmap[string]any{"tags": []string{"名詞,一般"}}Keeps only the tokens whose part of speech is one of the tags形態素 / 解析 / を / 行う becomes 形態素
japanese_katakana_stemmap[string]any{"min": 3}Removes a trailing prolonged sound mark from the katakana tokens longer than min charactersライブラリー becomes ライブラリ

[!NOTE] Tags are normalized to exactly four comma-separated levels (missing levels are padded with *) and compared for exact equality against the four part-of-speech details of each token, starting at the field named part_of_speech in the dictionary schema (or at the first detail when the schema has no such field). A bare 助詞 therefore never matches IPADIC particle tokens — they always carry a subcategory such as 助詞,係助詞 — while a bare 助動詞 does match, because auxiliary verbs have no subcategory (助動詞,*,*,*).

Complete Pipeline Example

The example below combines two character filters and three token filters into a single pipeline:

tokenizer, err := lindera.NewTokenizerBuilder().
	SetDictionary("embedded://ipadic").
	// Full-width letters become half-width (NFKC).
	AppendCharacterFilter("unicode_normalize", map[string]any{"kind": "nfkc"}).
	// Iteration marks become the characters they repeat.
	AppendCharacterFilter("japanese_iteration_mark", map[string]any{"normalize_kanji": true, "normalize_kana": true}).
	// An inflected word becomes its base form.
	AppendTokenFilter("japanese_base_form", nil).
	// Particles, auxiliary verbs and punctuation are removed.
	AppendTokenFilter("japanese_stop_tags", map[string]any{
		"tags": []string{"助詞,格助詞,一般", "助詞,係助詞", "助詞,連体化", "助動詞", "記号,句点", "記号,読点"},
	}).
	// Letters become lower case.
	AppendTokenFilter("lowercase", nil).
	Build()
if err != nil {
	log.Fatal(err)
}
defer tokenizer.Close()

tokens, err := tokenizer.Tokenize("Linderaは形態素解析を行うライブラリです。")
if err != nil {
	log.Fatal(err)
}
for _, token := range tokens {
	fmt.Printf("%s\t%s\n", token.Surface, strings.Join(token.Details, ","))
}
fmt.Println("EOS")

Expected output:

lindera    名詞,固有名詞,組織,*,*,*,*,*,*
形態素    名詞,一般,*,*,*,*,形態素,ケイタイソ,ケイタイソ
解析    名詞,サ変接続,*,*,*,*,解析,カイセキ,カイセキ
行う    動詞,自立,*,*,五段・ワ行促音便,基本形,行う,オコナウ,オコナウ
ライブラリ    名詞,一般,*,*,*,*,ライブラリ,ライブラリ,ライブラリ
EOS

In this pipeline:

  1. unicode_normalize converts full-width characters to half-width (NFKC normalization)
  2. japanese_iteration_mark resolves iteration marks
  3. japanese_base_form converts inflected tokens to base form
  4. japanese_stop_tags removes particles, auxiliary verbs, and punctuation
  5. lowercase normalizes alphabetic characters to lowercase

The code above is an example of the package (Example_pipeline in lindera-go/example_test.go), which go test runs and whose output it compares.