Lindera
A morphological analysis library in Rust. This project fork from kuromoji-rs.
Lindera aims to build a library which is easy to install and provides concise APIs for various Rust applications.
Feature flags
As of v5.0, this crate is a pure morphological segmenter around the
Segmenter API. The analysis chain (character filters, token filters, and the
Tokenizer) lives in the companion
lindera-analysis crate:
[]
# Pure segmenter
= "5.0"
# With the analysis chain (character filters, token filters, Tokenizer)
= "5.0"
= "5.0"
See the migration guide when upgrading from v4.
Segmentation example
Basic segmentation
Put the following in Cargo.toml:
[]
= { = "5.0", = ["embed-ipadic"] }
This example covers the basic usage of Lindera as a pure segmenter — no additional crates required.
It will:
- Create a segmenter in normal mode
- Segment the input text
- Output the tokens
use Cow;
use load_dictionary;
use Mode;
use Segmenter;
use LinderaResult;
The above example can be run as follows:
% cargo run -p lindera --features=embed-ipadic --example=segment
Tokenization examples
The Tokenizer and the filter chain below are provided by the
lindera-analysis crate:
[]
= { = "5.0", = ["embed-ipadic"] }
= "5.0"
Basic tokenization
This example covers the basic usage of the tokenizer.
It will:
- Create a tokenizer in normal mode
- Tokenize the input text
- Output the tokens
use load_dictionary;
use Mode;
use Segmenter;
use Tokenizer;
use LinderaResult;
The above example can be run as follows:
% cargo run -p lindera-analysis --features=embed-ipadic --example=tokenize
You can see the result as follows:
text: 関西国際空港限定トートバッグ
token: 関西国際空港 名詞,固有名詞,組織,*,*,*,関西国際空港,カンサイコクサイクウコウ,カンサイコクサイクーコー
token: 限定 名詞,サ変接続,*,*,*,*,限定,ゲンテイ,ゲンテイ
token: トートバッグ UNK
Tokenization with user dictionary
You can give user dictionary entries along with the default system dictionary. User dictionary should be a CSV with following format.
<surface>,<part_of_speech>,<reading>
Put the following in Cargo.toml:
[]
= { = "5.0", = ["embed-ipadic"] }
= "5.0"
For example:
% cat ./resources/simple_userdic.csv
東京スカイツリー,カスタム名詞,トウキョウスカイツリー
東武スカイツリーライン,カスタム名詞,トウブスカイツリーライン
とうきょうスカイツリー駅,カスタム名詞,トウキョウスカイツリーエキ
With an user dictionary, Tokenizer will be created as follows:
use PathBuf;
use load_dictionary;
use Mode;
use Segmenter;
use Tokenizer;
The above example can be run by cargo run --example:
% cargo run -p lindera-analysis --features=embed-ipadic --example=tokenize_with_user_dict
text: 東京スカイツリーの最寄り駅はとうきょうスカイツリー駅です
token: 東京スカイツリー カスタム名詞,*,*,*,*,*,東京スカイツリー,トウキョウスカイツリー,*
token: の 助詞,連体化,*,*,*,*,の,ノ,ノ
token: 最寄り駅 名詞,一般,*,*,*,*,最寄り駅,モヨリエキ,モヨリエキ
token: は 助詞,係助詞,*,*,*,*,は,ハ,ワ
token: とうきょうスカイツリー駅 カスタム名詞,*,*,*,*,*,とうきょうスカイツリー駅,トウキョウスカイツリーエキ,*
token: です 助動詞,*,*,*,特殊・デス,基本形,です,デス,デス
Tokenize with filters
Put the following in Cargo.toml:
[]
= { = "5.0", = ["embed-ipadic"] }
= "5.0"
This example covers the basic usage of Lindera Analysis Framework.
It will:
- Apply character filter for Unicode normalization (NFKC)
- Tokenize the input text with IPADIC
- Apply token filters for removing stop tags (Part-of-speech) and Japanese Katakana stem filter
use BoxCharacterFilter;
use JapaneseIterationMarkCharacterFilter;
use ;
use load_dictionary;
use Mode;
use Segmenter;
use BoxTokenFilter;
use JapaneseCompoundWordTokenFilter;
use JapaneseNumberTokenFilter;
use JapaneseStopTagsTokenFilter;
use Tokenizer;
use LinderaResult;
The above example can be run as follows:
% cargo run -p lindera-analysis --features=embed-ipadic --example=tokenize_with_filters
You can see the result as follows:
text: Linderaは形態素解析エンジンです。ユーザー辞書も利用可能です。
token: "Lindera", start: 0, end: 21, details: Some(["UNK"])
token: "形態素", start: 24, end: 33, details: Some(["名詞", "一般", "*", "*", "*", "*", "形態素", "ケイタイソ", "ケイタイソ"])
token: "解析", start: 33, end: 39, details: Some(["名詞", "サ変接続", "*", "*", "*", "*", "解析", "カイセキ", "カイセキ"])
token: "エンジン", start: 39, end: 54, details: Some(["名詞", "一般", "*", "*", "*", "*", "エンジン", "エンジン", "エンジン"])
token: "ユーザー", start: 63, end: 75, details: Some(["名詞", "一般", "*", "*", "*", "*", "ユーザー", "ユーザー", "ユーザー"])
token: "辞書", start: 75, end: 81, details: Some(["名詞", "一般", "*", "*", "*", "*", "辞書", "ジショ", "ジショ"])
token: "利用", start: 84, end: 90, details: Some(["名詞", "サ変接続", "*", "*", "*", "*", "利用", "リヨウ", "リヨー"])
token: "可能", start: 90, end: 96, details: Some(["名詞", "形容動詞語幹", "*", "*", "*", "*", "可能", "カノウ", "カノー"])
Configuration file
Lindera is able to read YAML format configuration files. Specify the path to the following file in the environment variable LINDERA_CONFIG_PATH. You can use it easily without having to code the behavior of the tokenizer in Rust code.
segmenter:
mode: "normal"
dictionary: "embedded://ipadic"
# user_dictionary: "./resources/user_dict/ipadic_simple_userdic.csv"
character_filters:
- kind: "unicode_normalize"
args:
kind: "nfkc"
- kind: "japanese_iteration_mark"
args:
normalize_kanji: true
normalize_kana: true
- kind: mapping
args:
mapping:
リンデラ: Lindera
token_filters:
- kind: "japanese_compound_word"
args:
kind: "ipadic"
tags:
- "名詞,数"
- "名詞,接尾,助数詞"
new_tag: "名詞,数"
- kind: "japanese_number"
args:
tags:
- "名詞,数"
- kind: "japanese_stop_tags"
args:
tags:
- "接続詞"
- "助詞"
- "助詞,格助詞"
- "助詞,格助詞,一般"
- "助詞,格助詞,引用"
- "助詞,格助詞,連語"
- "助詞,係助詞"
- "助詞,副助詞"
- "助詞,間投助詞"
- "助詞,並立助詞"
- "助詞,終助詞"
- "助詞,副助詞/並立助詞/終助詞"
- "助詞,連体化"
- "助詞,副詞化"
- "助詞,特殊"
- "助動詞"
- "記号"
- "記号,一般"
- "記号,読点"
- "記号,句点"
- "記号,空白"
- "記号,括弧閉"
- "その他,間投"
- "フィラー"
- "非言語音"
- kind: "japanese_katakana_stem"
args:
min: 3
- kind: "remove_diacritical_mark"
args:
japanese: false
% export LINDERA_CONFIG_PATH=./resources/lindera.yml
use PathBuf;
use TokenizerBuilder;
use LinderaResult;
Environment Variables
LINDERA_BUILD_DICTIONARY_CACHE_DIR
The LINDERA_BUILD_DICTIONARY_CACHE_DIR environment variable designates a build-time cache directory for the embedded-dictionary build pipeline. It is read only by the dictionary crates' build scripts and has no effect at runtime.
When set, each build stores two kinds of files under $LINDERA_BUILD_DICTIONARY_CACHE_DIR/<version>/ (where <version> is the dictionary crate version):
- the downloaded distribution archive (validated with MD5; invalid files are re-downloaded)
- the built binary dictionary that gets embedded into the crate
This enables:
- Offline builds: once cached, subsequent builds need no network access
- Faster builds: download and dictionary build are skipped when valid cached files exist
- Reproducible builds: consistent dictionary versions across builds
Usage:
export LINDERA_BUILD_DICTIONARY_CACHE_DIR=/path/to/cache
cargo build --features=embed-ipadic
Notes:
- The directory is managed automatically and is safe to delete; contents are re-downloaded and rebuilt as needed
- Version subdirectories accumulate across upgrades and are not garbage-collected; old ones can be removed freely
- Setting this variable causes dictionary crates to download and build their dictionaries even when no
embed-*feature is enabled (useful for pre-populating the cache)
Deprecated: the previous name
LINDERA_DICTIONARIES_PATHstill works as a fallback (the new name wins when both are set) and will be removed in v6.0.0.
LINDERA_CONFIG_PATH
The LINDERA_CONFIG_PATH environment variable specifies the path to a YAML configuration file for the tokenizer. This allows you to configure tokenizer behavior without modifying Rust code.
export LINDERA_CONFIG_PATH=./resources/lindera.yml
See the Configuration file section for details on the configuration format.
DOCS_RS
The DOCS_RS environment variable is automatically set by docs.rs when building documentation. When this variable is detected, Lindera creates dummy dictionary files instead of downloading actual dictionary data, allowing documentation to be built without network access or large file downloads.
This is primarily used internally by docs.rs and typically doesn't need to be set by users.
LINDERA_WORKDIR
The LINDERA_WORKDIR environment variable is automatically set during the build process by the lindera-dictionary crate. It points to the directory containing the built dictionary data files and is used internally by dictionary crates to locate their data files.
This variable is set automatically and should not be modified by users.
API reference
The API reference is available. Please see following URL: