lindera-python
Python binding for Lindera, a Japanese morphological analysis engine.
Overview
lindera-python provides a comprehensive Python interface to the Lindera 3.0.0 morphological analysis engine, supporting Japanese, Korean, and Chinese text analysis. This implementation includes all major features:
- Multi-language Support: Japanese (IPADIC, IPADIC-NEologd, UniDic), Korean (ko-dic), Chinese (CC-CEDICT, Jieba)
- Character Filters: Text preprocessing with mapping, regex, Unicode normalization, and Japanese iteration mark handling
- Token Filters: Post-processing filters including lowercase, length filtering, stop words, and Japanese-specific filters
- Flexible Configuration: Configurable tokenization modes and penalty settings
- Metadata Support: Complete dictionary schema and metadata management
Features
Core Components
- TokenizerBuilder: Fluent API for building customized tokenizers
- Tokenizer: High-performance text tokenization with integrated filtering
- CharacterFilter: Pre-processing filters for text normalization
- TokenFilter: Post-processing filters for token refinement
- Metadata & Schema: Dictionary structure and configuration management
- Training & Export (optional): Train custom morphological analysis models from corpus data
Supported Dictionaries
- Japanese: IPADIC, IPADIC-NEologd, UniDic
- Korean: ko-dic
- Chinese: CC-CEDICT, Jieba
- Custom: User dictionary support
Pre-built dictionaries are available from GitHub Releases.
Download a dictionary archive (e.g. lindera-ipadic-*.zip) and specify the extracted path when loading.
Filter Types
Character Filters:
- Mapping filter (character replacement)
- Regex filter (pattern-based replacement)
- Unicode normalization (NFKC, etc.)
- Japanese iteration mark normalization
Token Filters:
- Text case transformation (lowercase, uppercase)
- Length filtering (min/max character length)
- Stop words filtering
- Japanese-specific filters (base form, reading form, etc.)
- Korean-specific filters
Install project dependencies
- pyenv : https://github.com/pyenv/pyenv?tab=readme-ov-file#installation
- Rust : https://www.rust-lang.org/tools/install
Install Python
# Install Python
% pyenv install 3.13.5
Setup repository and activate virtual environment
# Clone lindera project repository
% git clone git@github.com:lindera/lindera.git
% cd lindera
# Create the Python virtual environment lindera-python/.venv with maturin and pytest
% make setup-venv
# Activate Python virtual environment
% source lindera-python/.venv/bin/activate
Install lindera-python in the virtual environment
This command builds the library with development settings (debug build).
(.venv) % cd lindera-python
(.venv) % maturin develop
Quick Start
Basic Tokenization
from lindera import Mode, Tokenizer, load_dictionary
# Load dictionary from a local path (download from GitHub Releases)
dictionary = load_dictionary("/path/to/ipadic")
# Create a tokenizer
tokenizer = Tokenizer(dictionary, mode=Mode.normal())
# Tokenize Japanese text
text = "すもももももももものうち"
tokens = tokenizer.tokenize(text)
for token in tokens:
print(f"Text: {token.surface}, Position: {token.byte_start}-{token.byte_end}")
Converting Tokens to Plain Data
to_dict() returns the token as a plain dict, keeping each field's natural
Python type, so it serializes without a custom encoder:
import json
data = tokens[0].to_dict()
# {'surface': ..., 'byte_start': 0, 'byte_end': 9, 'position': 0,
# 'word_id': 12345, 'is_unknown': False, 'details': [...]}
json.dumps([token.to_dict() for token in tokens])
Using Character Filters
from lindera import Mode, TokenizerBuilder
# Create tokenizer builder
builder = TokenizerBuilder()
builder.set_mode(Mode.normal())
builder.set_dictionary("/path/to/ipadic")
# Add character filters
builder.append_character_filter("mapping", {"mapping": {"ー": "-"}})
builder.append_character_filter("unicode_normalize", {"kind": "nfkc"})
# Build tokenizer with filters
tokenizer = builder.build()
text = "テストー123"
tokens = tokenizer.tokenize(text) # Will apply filters automatically
Using Token Filters
from lindera import Mode, TokenizerBuilder
# Create tokenizer builder
builder = TokenizerBuilder()
builder.set_mode(Mode.normal())
builder.set_dictionary("/path/to/ipadic")
# Add token filters
builder.append_token_filter("lowercase")
builder.append_token_filter("length", {"min": 2, "max": 10})
builder.append_token_filter("japanese_stop_tags", {
"tags": ["助詞,格助詞,一般", "助詞,係助詞", "助詞,連体化", "助動詞"]
})
# Build tokenizer with filters
tokenizer = builder.build()
tokens = tokenizer.tokenize("テキストの解析")
Integrated Pipeline
from lindera import Mode, TokenizerBuilder
# Build tokenizer with integrated filters
builder = TokenizerBuilder()
builder.set_mode(Mode.normal())
builder.set_dictionary("/path/to/ipadic")
# Add character filters
builder.append_character_filter("mapping", {"mapping": {"ー": "-"}})
builder.append_character_filter("unicode_normalize", {"kind": "nfkc"})
# Add token filters
builder.append_token_filter("lowercase")
builder.append_token_filter("japanese_base_form")
# Build and use
tokenizer = builder.build()
tokens = tokenizer.tokenize("コーヒーショップ")
Working with Metadata
from lindera import Metadata
# Get metadata for a specific dictionary
metadata = Metadata.load("/path/to/ipadic")
print(f"Dictionary: {metadata.dictionary_name}")
print(f"Version: {metadata.dictionary_version}")
# Access schema information
schema = metadata.dictionary_schema
print(f"Schema has {len(schema.fields)} fields")
print(f"Fields: {schema.fields[:5]}") # First 5 fields
Advanced Usage
Filter Configuration Examples
Character filters and token filters accept configuration as dictionary arguments:
from lindera import TokenizerBuilder
builder = TokenizerBuilder()
builder.set_dictionary("/path/to/ipadic")
# Character filters with dict configuration
builder.append_character_filter("unicode_normalize", {"kind": "nfkc"})
builder.append_character_filter("japanese_iteration_mark", {
"normalize_kanji": "true",
"normalize_kana": "true"
})
builder.append_character_filter("mapping", {
"mapping": {"リンデラ": "lindera", "トウキョウ": "東京"}
})
# Token filters with dict configuration
builder.append_token_filter("japanese_katakana_stem", {"min": 3})
builder.append_token_filter("length", {"min": 2, "max": 10})
builder.append_token_filter("japanese_stop_tags", {
"tags": ["助詞,格助詞,一般", "助詞,係助詞", "助詞,連体化", "助動詞", "記号,句点", "記号,読点"]
})
# Filters without configuration can omit the dict
builder.append_token_filter("lowercase")
builder.append_token_filter("japanese_base_form")
tokenizer = builder.build()
See examples/ directory for comprehensive examples including:
tokenize.py: Basic tokenizationtokenize_with_filters.py: Using character and token filterstokenize_with_userdict.py: Custom user dictionarytrain_and_export.py: Train and export custom dictionaries (requirestrainfeature)- Multi-language tokenization
- Advanced configuration options
Dictionary Support
Japanese
- IPADIC: Default Japanese dictionary, good for general text
- UniDic: Academic dictionary with detailed morphological information
Korean
- ko-dic: Standard Korean dictionary for morphological analysis
Chinese
- CC-CEDICT: Community-maintained Chinese-English dictionary
Custom Dictionaries
- User dictionary support for domain-specific terms
- CSV format for easy customization
Dictionary Training (Experimental)
lindera-python supports training custom morphological analysis models from annotated corpus data when built with the train feature.
Building with Training Support
# Install with training support
(.venv) % maturin develop --features train
Training a Model
import lindera.trainer
# Train a model from corpus
lindera.trainer.train(
seed="path/to/seed.csv", # Seed lexicon
corpus="path/to/corpus.txt", # Training corpus
char_def="path/to/char.def", # Character definitions
unk_def="path/to/unk.def", # Unknown word definitions
feature_def="path/to/feature.def", # Feature templates
rewrite_def="path/to/rewrite.def", # Rewrite rules
output="model.dat", # Output model file
lambda_=1.0, # L2 regularization coefficient (the default)
max_iter=100, # Max iterations
max_threads=None # Auto-detect CPU cores
)
Exporting Dictionary Files
# Export trained model to dictionary files
lindera.trainer.export(
model="model.dat", # Trained model
output="exported_dict/", # Output directory
metadata="metadata.json" # Optional metadata file
)
This will create:
lex.csv: Lexicon filematrix.def: Connection cost matrixunk.def: Unknown word definitionschar.def: Character definitionsmetadata.json: Dictionary metadata (if provided)
See examples/train_and_export.py for a complete example.
API Reference
Core Classes
TokenizerBuilder: Fluent builder for tokenizer configurationTokenizer: Main tokenization engineMode: Tokenization mode: normal, or decompose with aPenaltyToken: Individual token with text, position, and linguistic featuresCharacterFilter: Text preprocessing filtersTokenFilter: Token post-processing filtersMetadata: Dictionary metadata and configurationSchema: Dictionary schema definition
Training Functions (requires train feature)
train(): Train a morphological analysis model from corpusexport(): Export trained model to dictionary files
See the test_basic.py file for comprehensive API usage examples.
Metadata
Release files for lindera 7.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| lindera-7.0.0.tar.gz | 916.9 kB | Without provenance | Details for lindera-7.0.0.tar.gz |
Built distributions (wheels)
Total release size: 14.8 MB
Release files / lindera-7.0.0.tar.gz
| Download URL | lindera-7.0.0.tar.gz |
|---|---|
| Size | 916.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2df7d16a0b4970b04a3b83fbddeb977d2e0c8f255847590be9cd253a27c3f6bf
|
|
BLAKE2b-256 checksum How to use checksums |
af7fc6b128581768d94efd94cfddfd1d84efb022ea52f8fc2e77b645904ba6f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.16.0
|
Release files / lindera-7.0.0-cp310-abi3-win_arm64.whl
| Download URL | lindera-7.0.0-cp310-abi3-win_arm64.whl |
|---|---|
| Size | 2.1 MB |
| Tags | CPython 3.10 Windows ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
d479b8e665f2b0bfb5e7e4765e9049e0a59cccc62d9726560b49968c9183248d
|
|
BLAKE2b-256 checksum How to use checksums |
f43db3006c930ee51a02652d6de66690f1eac1a648cb629e4ddab9d2cdc2dacb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.16.0
|
Release files / lindera-7.0.0-cp310-abi3-win_amd64.whl
| Download URL | lindera-7.0.0-cp310-abi3-win_amd64.whl |
|---|---|
| Size | 2.3 MB |
| Tags | CPython 3.10 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
56c1f8d680f3c8c6acb894937b20a0650f3e37ef8706a3c8ab09f49976094e14
|
|
BLAKE2b-256 checksum How to use checksums |
a57b303de32cb59b600a21b7fd5db6a94c329493a15117973ae158561bac5abf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.16.0
|
Release files / lindera-7.0.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | lindera-7.0.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 2.5 MB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
56044a934cf811649539cee70af704c021936569c4685a4f3c614919016476e3
|
|
BLAKE2b-256 checksum How to use checksums |
225a1294bd379987bf1999150a302ff3ad0aa6413c8e76439112922c9837eac3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.16.0
|
Release files / lindera-7.0.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
| Download URL | lindera-7.0.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl |
|---|---|
| Size | 2.4 MB |
| Tags | CPython 3.10 Linux glibc 2.17+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
d24281b7b2803320827caa1171b2f34a2ea1a15952b3defdbb9e663758261436
|
|
BLAKE2b-256 checksum How to use checksums |
888d68f931b2ee54a77daa016b3370fa4393455e1d616f6f0baf21757b1edfc5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.16.0
|
Release files / lindera-7.0.0-cp310-abi3-macosx_11_0_arm64.whl
| Download URL | lindera-7.0.0-cp310-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 2.3 MB |
| Tags | CPython 3.10 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
e7a80d9b6cbe6fd6a850d026c05ad51712c1fff4ad9cf2eebbad1809daea6099
|
|
BLAKE2b-256 checksum How to use checksums |
19cb36a6b8bbfd18dfda5161a4155674b09faf34702309b3cbce0d88313c6a0a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.16.0
|
Release files / lindera-7.0.0-cp310-abi3-macosx_10_12_x86_64.whl
| Download URL | lindera-7.0.0-cp310-abi3-macosx_10_12_x86_64.whl |
|---|---|
| Size | 2.3 MB |
| Tags | CPython 3.10 abi3 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
7731a817e2aea97aba2ff85b181507fd5ff069c243faefef7facc78ab6a1dff8
|
|
BLAKE2b-256 checksum How to use checksums |
c658b01a6a5998bdaf528f4ce9be04f0332ff76b88bf41be727ab0f0ae0ed3bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.16.0
|
Security for lindera 7.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.