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 .NET 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-analysisships, including parameters and examples.
Appending a filter
Method of TokenizerBuilder | What it does |
|---|---|
AppendCharacterFilter(string kind, string? argsJson = null) | Appends a character filter |
AppendTokenFilter(string kind, string? argsJson = null) | Appends a token filter |
kind is the name of the filter, and argsJson its arguments as a JSON object, or null for none:
using TokenizerBuilder builder = new TokenizerBuilder()
.SetDictionary("embedded://ipadic")
.AppendTokenFilter("japanese_stop_tags", """{"tags": ["助詞,係助詞", "助動詞"]}""");
using Tokenizer tokenizer = builder.Build();
Console.WriteLine(string.Join(" / ", tokenizer.Tokenize("東京は晴れです").Select(t => t.Surface)));
This prints 東京 / 晴れ. (embedded://ipadic names the dictionary a library was built with; with the library of the package it is the path of a directory.)
Arguments a program has as values can be made JSON with System.Text.Json:
string args = System.Text.Json.JsonSerializer.Serialize(new Dictionary<string, string[]>
{
["tags"] = ["助詞,係助詞", "助動詞"],
});
using TokenizerBuilder builder = new TokenizerBuilder()
.SetDictionary("embedded://ipadic")
.AppendTokenFilter("japanese_stop_tags", args);
The package takes JSON text and not .NET values: turning any value into JSON needs reflection, which trimming and Native AOT do not allow. A program that is trimmed or compiled with Native AOT makes its JSON with a source-generated JsonSerializerContext, or writes it as text.
JSON that is not valid is a LinderaException of the kind InvalidArgument, at once. A name that is no filter, and arguments a filter does not take, fail when Build builds the tokenizer.
A tokenizer made with Tokenizer.Create 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.
| Filter | Arguments | What it does | Example |
|---|---|---|---|
unicode_normalize | {"kind": "nfkc"} | Applies a Unicode normalization form: nfc, nfkc, nfd or nfkd | Lindera becomes Lindera |
mapping | {"mapping": {"リンデラ": "Lindera"}} | Replaces the strings that are the keys of a table with their values | リンデラは becomes Lindera / は |
japanese_iteration_mark | {"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.
| Filter | Arguments | What it does | Example |
|---|---|---|---|
lowercase | none | Converts the surfaces of the tokens to lower case | Lindera becomes lindera |
japanese_base_form | none | Replaces an inflected form with its base (dictionary) form, from the details of the dictionary | 食べ / た becomes 食べる / た |
japanese_stop_tags | {"tags": ["助詞,格助詞,一般", "助詞,係助詞", "助詞,連体化", "助動詞"]} | Removes the tokens whose part of speech is one of the tags | 東京 / は / 晴れ / です becomes 東京 / 晴れ |
japanese_keep_tags | {"tags": ["名詞,一般"]} | Keeps only the tokens whose part of speech is one of the tags | 形態素 / 解析 / を / 行う becomes 形態素 |
japanese_katakana_stem | {"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 namedpart_of_speechin 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 (助動詞,*,*,*).
A token filter that rewrites the surface (lowercase, japanese_base_form) leaves Start and End on the range of the original text, which is then not the surface.
Complete Pipeline Example
The example below combines two character filters and three token filters into a single pipeline:
using TokenizerBuilder builder = new TokenizerBuilder()
.SetDictionary("embedded://ipadic")
// Full-width letters become half-width (NFKC).
.AppendCharacterFilter("unicode_normalize", """{"kind": "nfkc"}""")
// Iteration marks become the characters they repeat.
.AppendCharacterFilter("japanese_iteration_mark", """{"normalize_kanji": true, "normalize_kana": true}""")
// An inflected word becomes its base form.
.AppendTokenFilter("japanese_base_form")
// Particles, auxiliary verbs and punctuation are removed.
.AppendTokenFilter("japanese_stop_tags", """
{"tags": ["助詞,格助詞,一般", "助詞,係助詞", "助詞,連体化", "助動詞", "記号,句点", "記号,読点"]}
""")
// Letters become lower case.
.AppendTokenFilter("lowercase");
using Tokenizer tokenizer = builder.Build();
foreach (Token token in tokenizer.Tokenize("Linderaは形態素解析を行うライブラリです。"))
{
Console.WriteLine($"{token.Surface}\t{string.Join(',', token.Details)}");
}
Console.WriteLine("EOS");
Expected output:
lindera 名詞,固有名詞,組織,*,*,*,*,*,*
形態素 名詞,一般,*,*,*,*,形態素,ケイタイソ,ケイタイソ
解析 名詞,サ変接続,*,*,*,*,解析,カイセキ,カイセキ
行う 動詞,自立,*,*,五段・ワ行促音便,基本形,行う,オコナウ,オコナウ
ライブラリ 名詞,一般,*,*,*,*,ライブラリ,ライブラリ,ライブラリ
EOS
In this pipeline:
unicode_normalizeconverts full-width characters to half-width (NFKC normalization)japanese_iteration_markresolves iteration marksjapanese_base_formconverts inflected tokens to base formjapanese_stop_tagsremoves particles, auxiliary verbs, and punctuationlowercasenormalizes alphabetic characters to lowercase
The code above is a test of the package (The_complete_pipeline in lindera-dotnet/tests/Lindera.Tests/DocsExamplesTests.cs), which checks its output; the filters of the tables are checked by PipelineTests.cs.