Skip to main content

lindera_ruby/
lib.rs

1//! # Lindera Ruby Bindings
2//!
3//! Ruby bindings for [Lindera](https://github.com/lindera/lindera), a morphological analysis library for CJK text.
4//!
5//! Lindera provides high-performance tokenization and morphological analysis for:
6//! - Japanese (IPADIC, IPADIC NEologd, UniDic)
7//! - Korean (ko-dic)
8//! - Chinese (CC-CEDICT, Jieba)
9//!
10//! ## Features
11//!
12//! - **Dictionary management**: Build, load, and use custom dictionaries
13//! - **Tokenization**: Multiple tokenization modes (normal, decompose)
14//! - **Filters**: Character and token filtering pipeline
15//! - **Training**: Train custom morphological models (with `train` feature)
16//! - **User dictionaries**: Support for custom user dictionaries
17//!
18//! ## Examples
19//!
20//! ```ruby
21//! require "lindera"
22//!
23//! # Create a tokenizer
24//! builder = Lindera::TokenizerBuilder.new
25//! tokenizer = builder.build
26//!
27//! # Tokenize text
28//! tokens = tokenizer.tokenize("関西国際空港")
29//! tokens.each { |token| puts "#{token.surface}: #{token.details}" }
30//! ```
31
32pub mod character_filter;
33pub mod dictionary;
34pub mod error;
35pub mod metadata;
36pub mod mode;
37pub mod schema;
38pub mod token;
39pub mod token_filter;
40pub mod tokenizer;
41pub mod util;
42
43#[cfg(feature = "train")]
44pub mod trainer;
45
46use magnus::{Error, Ruby, function};
47
48/// Returns the version of the lindera-ruby package.
49///
50/// # Returns
51///
52/// A string with the package version.
53fn version() -> String {
54    env!("CARGO_PKG_VERSION").to_string()
55}
56
57/// Ruby extension initialization entry point.
58///
59/// Defines the `Lindera` module and all its classes and functions.
60#[magnus::init]
61fn init(ruby: &Ruby) -> Result<(), Error> {
62    let module = ruby.define_module("Lindera")?;
63
64    // Version
65    module.define_module_function("version", function!(version, 0))?;
66
67    // Schema / FieldDefinition / FieldType
68    schema::define(ruby, &module)?;
69
70    // Metadata
71    metadata::define(ruby, &module)?;
72
73    // Mode / Penalty
74    mode::define(ruby, &module)?;
75
76    // Token
77    token::define(ruby, &module)?;
78
79    // Dictionary / UserDictionary + load/build functions
80    dictionary::define(ruby, &module)?;
81
82    // TokenizerBuilder / Tokenizer
83    tokenizer::define(ruby, &module)?;
84
85    // Trainer (feature = "train")
86    #[cfg(feature = "train")]
87    trainer::define(ruby, &module)?;
88
89    Ok(())
90}