lindera-wasm 1.3.1

A morphological analysis library for WebAssembly.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! # lindera-wasm
//!
//! WebAssembly bindings for [Lindera](https://github.com/lindera/lindera), a morphological analysis library.
//!
//! This crate provides WASM bindings that enable Japanese, Korean, and Chinese text tokenization
//! in web browsers and Node.js environments.
//!
//! ## Features
//!
//! - **Multiple dictionaries**: IPADIC, UniDic (Japanese), ko-dic (Korean), CC-CEDICT (Chinese)
//! - **Flexible tokenization modes**: Normal and decompose modes
//! - **Character filters**: Unicode normalization and more
//! - **Token filters**: Lowercase, compound word handling, number normalization
//! - **Custom user dictionaries**: Support for user-defined dictionaries
//!
//! ## Usage
//!
//! ### Web (Browser)
//!
//! ```javascript
//! import __wbg_init, { TokenizerBuilder } from 'lindera-wasm-web-ipadic';
//!
//! __wbg_init().then(() => {
//!     const builder = new TokenizerBuilder();
//!     builder.setDictionary("embedded://ipadic");
//!     builder.setMode("normal");
//!
//!     const tokenizer = builder.build();
//!     const tokens = tokenizer.tokenize("関西国際空港");
//!     console.log(tokens);
//! });
//! ```
//!
//! ### Node.js
//!
//! ```javascript
//! const { TokenizerBuilder } = require('lindera-wasm-nodejs-ipadic');
//!
//! const builder = new TokenizerBuilder();
//! builder.setDictionary("embedded://ipadic");
//! builder.setMode("normal");
//!
//! const tokenizer = builder.build();
//! const tokens = tokenizer.tokenize("関西国際空港");
//! console.log(tokens);
//! ```

use std::str::FromStr;

use serde_json::Value;
use wasm_bindgen::prelude::*;

use lindera::mode::Mode;
use lindera::token::Token;
use lindera::tokenizer::{
    Tokenizer as LinderaTokenizer, TokenizerBuilder as LinderaTokenizerBuilder,
};

const VERSION: &str = env!("CARGO_PKG_VERSION");

/// Gets the version of the lindera-wasm library.
///
/// # Returns
///
/// The version string of the library (e.g., "1.0.0").
///
/// # Examples
///
/// ```javascript
/// import { getVersion } from 'lindera-wasm';
/// console.log(getVersion()); // "1.0.0"
/// ```
#[wasm_bindgen(js_name = "getVersion")]
pub fn get_version() -> String {
    VERSION.to_string()
}

/// Converts snake_case strings to camelCase.
///
/// This is used internally to convert Rust field names to JavaScript-friendly camelCase.
fn to_camel_case(s: &str) -> String {
    let mut result = String::new();
    let mut capitalize_next = false;

    for c in s.chars() {
        if c == '_' {
            capitalize_next = true;
        } else if capitalize_next {
            result.push(c.to_ascii_uppercase());
            capitalize_next = false;
        } else {
            result.push(c);
        }
    }

    result
}

/// Converts a serde_json::Value to a JsValue recursively.
///
/// This function handles conversion of various JSON types to their JavaScript equivalents,
/// including objects, arrays, strings, numbers, booleans, and null values.
fn value_to_js(value: Value) -> Result<JsValue, JsValue> {
    match value {
        Value::String(s) => Ok(JsValue::from_str(s.as_str())),
        Value::Number(n) => {
            if let Some(i) = n.as_u64() {
                Ok(JsValue::from_f64(i as f64))
            } else if let Some(i) = n.as_i64() {
                Ok(JsValue::from_f64(i as f64))
            } else if let Some(f) = n.as_f64() {
                Ok(JsValue::from_f64(f))
            } else {
                Ok(JsValue::from_str(&n.to_string()))
            }
        }
        Value::Bool(b) => Ok(JsValue::from_bool(b)),
        Value::Null => Ok(JsValue::NULL),
        Value::Array(arr) => {
            let js_arr = js_sys::Array::new();
            for item in arr {
                js_arr.push(&value_to_js(item)?);
            }
            Ok(js_arr.into())
        }
        Value::Object(map) => {
            let js_obj = js_sys::Object::new();
            for (key, val) in map {
                // Change key to camel case
                let js_key = JsValue::from_str(to_camel_case(&key).as_str());
                let js_val = value_to_js(val)?;
                js_sys::Reflect::set(&js_obj, &js_key, &js_val)
                    .map_err(|e| JsValue::from_str(&format!("Failed to set property: {e:?}")))?;
            }
            Ok(js_obj.into())
        }
    }
}

/// Converts a vector of tokens to a JavaScript array of objects.
///
/// Each token is converted to a JavaScript object with camelCase field names.
fn convert_to_js_objects(tokens: Vec<Token>) -> Result<JsValue, JsValue> {
    let js_array = js_sys::Array::new();
    for mut token in tokens {
        js_array.push(&value_to_js(token.as_value())?);
    }

    Ok(js_array.into())
}

/// Builder for creating a [`Tokenizer`] instance.
///
/// `TokenizerBuilder` provides a fluent API for configuring and building a tokenizer
/// with various options such as dictionary selection, tokenization mode, character filters,
/// and token filters.
///
/// # Examples
///
/// ```javascript
/// const builder = new TokenizerBuilder();
/// builder.setDictionary("embedded://ipadic");
/// builder.setMode("normal");
/// builder.setKeepWhitespace(false);
/// builder.appendCharacterFilter("unicode_normalize", { "kind": "nfkc" });
/// builder.appendTokenFilter("lowercase");
///
/// const tokenizer = builder.build();
/// ```
#[wasm_bindgen]
pub struct TokenizerBuilder {
    inner: LinderaTokenizerBuilder,
}

#[wasm_bindgen]
impl TokenizerBuilder {
    /// Creates a new `TokenizerBuilder` instance.
    ///
    /// # Returns
    ///
    /// A new `TokenizerBuilder` instance.
    ///
    /// # Errors
    ///
    /// Returns an error if the builder cannot be initialized.
    ///
    /// # Examples
    ///
    /// ```javascript
    /// const builder = new TokenizerBuilder();
    /// ```
    #[wasm_bindgen(constructor)]
    pub fn new() -> Result<Self, JsValue> {
        let inner =
            LinderaTokenizerBuilder::new().map_err(|e| JsValue::from_str(&e.to_string()))?;

        Ok(Self { inner })
    }

    /// Builds and returns a configured [`Tokenizer`] instance.
    ///
    /// This method consumes the builder and creates the final tokenizer with all
    /// configured settings.
    ///
    /// # Returns
    ///
    /// A configured `Tokenizer` instance.
    ///
    /// # Errors
    ///
    /// Returns an error if the tokenizer cannot be built with the current configuration.
    ///
    /// # Examples
    ///
    /// ```javascript
    /// const builder = new TokenizerBuilder();
    /// builder.setDictionary("embedded://ipadic");
    /// const tokenizer = builder.build();
    /// ```
    pub fn build(self) -> Result<Tokenizer, JsValue> {
        let inner = self
            .inner
            .build()
            .map_err(|e| JsValue::from_str(&e.to_string()))?;

        Ok(Tokenizer { inner })
    }

    /// Sets the tokenization mode.
    ///
    /// # Parameters
    ///
    /// - `mode`: The tokenization mode. Valid values are:
    ///   - `"normal"`: Standard tokenization
    ///   - `"decompose"`: Decomposes compound words into their components
    ///
    /// # Errors
    ///
    /// Returns an error if the mode string is invalid.
    ///
    /// # Examples
    ///
    /// ```javascript
    /// builder.setMode("normal");
    /// // or
    /// builder.setMode("decompose");
    /// ```
    #[wasm_bindgen(js_name = "setMode")]
    pub fn set_mode(&mut self, mode: &str) -> Result<(), JsValue> {
        let m = Mode::from_str(mode).map_err(|e| JsValue::from_str(&e.to_string()))?;
        self.inner.set_segmenter_mode(&m);

        Ok(())
    }

    /// Sets the dictionary to use for tokenization.
    ///
    /// # Parameters
    ///
    /// - `uri`: The dictionary URI. Valid embedded dictionaries are:
    ///   - `"embedded://ipadic"`: Japanese IPADIC dictionary
    ///   - `"embedded://unidic"`: Japanese UniDic dictionary
    ///   - `"embedded://ko-dic"`: Korean ko-dic dictionary
    ///   - `"embedded://cc-cedict"`: Chinese CC-CEDICT dictionary
    ///
    /// # Examples
    ///
    /// ```javascript
    /// builder.setDictionary("embedded://ipadic");
    /// ```
    #[wasm_bindgen(js_name = "setDictionary")]
    pub fn set_dictionary(&mut self, uri: &str) -> Result<(), JsValue> {
        self.inner.set_segmenter_dictionary(uri);

        Ok(())
    }

    /// Sets a user-defined dictionary.
    ///
    /// User dictionaries allow you to add custom words and their properties
    /// to supplement the main dictionary.
    ///
    /// # Parameters
    ///
    /// - `uri`: The URI to the user dictionary file.
    ///
    /// # Examples
    ///
    /// ```javascript
    /// builder.setUserDictionary("path/to/user_dict.csv");
    /// ```
    #[wasm_bindgen(js_name = "setUserDictionary")]
    pub fn set_user_dictionary(&mut self, uri: &str) -> Result<(), JsValue> {
        self.inner.set_segmenter_user_dictionary(uri);

        Ok(())
    }

    /// Sets whether to keep whitespace tokens in the output.
    ///
    /// # Parameters
    ///
    /// - `keep`: If `true`, whitespace tokens are preserved; if `false`, they are removed.
    ///
    /// # Examples
    ///
    /// ```javascript
    /// builder.setKeepWhitespace(false); // Remove whitespace tokens
    /// // or
    /// builder.setKeepWhitespace(true);  // Keep whitespace tokens
    /// ```
    #[wasm_bindgen(js_name = "setKeepWhitespace")]
    pub fn set_keep_whitespace(&mut self, keep: bool) -> Result<(), JsValue> {
        self.inner.set_segmenter_keep_whitespace(keep);

        Ok(())
    }

    /// Appends a character filter to the tokenization pipeline.
    ///
    /// Character filters transform the input text before tokenization.
    ///
    /// # Parameters
    ///
    /// - `name`: The name of the character filter (e.g., `"unicode_normalize"`).
    /// - `args`: A JavaScript object containing filter-specific arguments.
    ///
    /// # Errors
    ///
    /// Returns an error if the arguments cannot be parsed.
    ///
    /// # Examples
    ///
    /// ```javascript
    /// builder.appendCharacterFilter("unicode_normalize", { "kind": "nfkc" });
    /// ```
    #[wasm_bindgen(js_name = "appendCharacterFilter")]
    pub fn append_character_filter(&mut self, name: &str, args: JsValue) -> Result<(), JsValue> {
        let a = serde_wasm_bindgen::from_value::<Value>(args)
            .map_err(|e| JsValue::from_str(&e.to_string()))?;

        self.inner.append_character_filter(name, &a);

        Ok(())
    }

    /// Appends a token filter to the tokenization pipeline.
    ///
    /// Token filters transform or filter the tokens after tokenization.
    ///
    /// # Parameters
    ///
    /// - `name`: The name of the token filter (e.g., `"lowercase"`, `"japanese_number"`).
    /// - `args`: A JavaScript object containing filter-specific arguments.
    ///
    /// # Errors
    ///
    /// Returns an error if the arguments cannot be parsed.
    ///
    /// # Examples
    ///
    /// ```javascript
    /// builder.appendTokenFilter("lowercase");
    /// builder.appendTokenFilter("japanese_number", { "tags": ["名詞,数"] });
    /// ```
    #[wasm_bindgen(js_name = "appendTokenFilter")]
    pub fn append_token_filter(&mut self, name: &str, args: JsValue) -> Result<(), JsValue> {
        let a = serde_wasm_bindgen::from_value::<Value>(args)
            .map_err(|e| JsValue::from_str(&e.to_string()))?;

        self.inner.append_token_filter(name, &a);

        Ok(())
    }
}

/// A tokenizer for morphological analysis.
///
/// The `Tokenizer` performs text tokenization based on the configuration
/// provided by [`TokenizerBuilder`].
///
/// # Examples
///
/// ```javascript
/// const builder = new TokenizerBuilder();
/// builder.setDictionary("embedded://ipadic");
/// builder.setMode("normal");
///
/// const tokenizer = builder.build();
/// const tokens = tokenizer.tokenize("関西国際空港");
/// console.log(tokens);
/// // Output: [
/// //   { surface: "関西国際空港", ... },
/// //   ...
/// // ]
/// ```
#[wasm_bindgen]
pub struct Tokenizer {
    inner: LinderaTokenizer,
}

#[wasm_bindgen]
impl Tokenizer {
    /// Tokenizes the input text.
    ///
    /// Analyzes the input text and returns an array of token objects. Each token
    /// contains information such as surface form, part-of-speech tags, reading, etc.
    /// Field names in the returned objects are in camelCase.
    ///
    /// # Parameters
    ///
    /// - `input_text`: The text to tokenize.
    ///
    /// # Returns
    ///
    /// A JavaScript array of token objects. Each token object contains:
    /// - `surface`: The surface form of the token
    /// - `pos`: Part-of-speech tags
    /// - Additional language-specific fields
    ///
    /// # Errors
    ///
    /// Returns an error if tokenization fails.
    ///
    /// # Examples
    ///
    /// ```javascript
    /// const tokens = tokenizer.tokenize("東京都に行く");
    /// tokens.forEach(token => {
    ///     console.log(token.surface, token.pos);
    /// });
    /// ```
    pub fn tokenize(&self, input_text: &str) -> Result<JsValue, JsValue> {
        let tokens = self
            .inner
            .tokenize(input_text)
            .map_err(|e| JsValue::from_str(&e.to_string()))?;

        let js_value = convert_to_js_objects(tokens)?;

        Ok(js_value)
    }
}

#[cfg(test)]
mod tests {
    #[cfg(target_arch = "wasm32")]
    use wasm_bindgen_test::wasm_bindgen_test;

    #[cfg(target_arch = "wasm32")]
    #[wasm_bindgen_test]
    fn test_tokenize() {
        use crate::TokenizerBuilder;
        use serde_json::Value;

        let mut builder = TokenizerBuilder::new().unwrap();
        builder.set_mode("normal").unwrap();
        builder.set_dictionary("embedded://ipadic").unwrap();

        let tokenizer = builder.build().unwrap();

        let t = tokenizer.tokenize("関西国際空港限定トートバッグ").unwrap();
        let tokens: Vec<Value> = serde_wasm_bindgen::from_value(t).unwrap();

        assert_eq!(tokens.len(), 3);
        assert_eq!(tokens[0].get("surface").unwrap(), "関西国際空港");
        assert_eq!(tokens[1].get("surface").unwrap(), "限定");
        assert_eq!(tokens[2].get("surface").unwrap(), "トートバッグ");
    }

    #[cfg(target_arch = "wasm32")]
    #[wasm_bindgen_test]
    fn test_camel_case() {
        use crate::to_camel_case;

        assert_eq!(to_camel_case("a"), "a");
        assert_eq!(to_camel_case("a_b"), "aB");
        assert_eq!(to_camel_case("a_b_c"), "aBC");
        assert_eq!(to_camel_case("a_b_c_d"), "aBCD");
        assert_eq!(to_camel_case("a_b_c_d_e"), "aBCDE");
    }
}