lindera-wasm 3.0.3

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
use wasm_bindgen::prelude::*;

use lindera::dictionary::{FieldDefinition, FieldType, Schema};

/// Field type in dictionary schema.
#[wasm_bindgen(js_name = "FieldType")]
#[derive(Debug, Clone, Copy)]
pub enum JsFieldType {
    /// Surface form (word text)
    Surface,
    /// Left context ID for morphological analysis
    LeftContextId,
    /// Right context ID for morphological analysis
    RightContextId,
    /// Word cost (used in path selection)
    Cost,
    /// Custom field (morphological features)
    Custom,
}

impl From<FieldType> for JsFieldType {
    fn from(field_type: FieldType) -> Self {
        match field_type {
            FieldType::Surface => JsFieldType::Surface,
            FieldType::LeftContextId => JsFieldType::LeftContextId,
            FieldType::RightContextId => JsFieldType::RightContextId,
            FieldType::Cost => JsFieldType::Cost,
            FieldType::Custom => JsFieldType::Custom,
        }
    }
}

impl From<JsFieldType> for FieldType {
    fn from(field_type: JsFieldType) -> Self {
        match field_type {
            JsFieldType::Surface => FieldType::Surface,
            JsFieldType::LeftContextId => FieldType::LeftContextId,
            JsFieldType::RightContextId => FieldType::RightContextId,
            JsFieldType::Cost => FieldType::Cost,
            JsFieldType::Custom => FieldType::Custom,
        }
    }
}

/// Field definition in dictionary schema.
#[wasm_bindgen(js_name = "FieldDefinition")]
#[derive(Clone)]
pub struct JsFieldDefinition {
    pub index: usize,
    #[wasm_bindgen(getter_with_clone)]
    pub name: String,
    pub field_type: JsFieldType,
    #[wasm_bindgen(getter_with_clone)]
    pub description: Option<String>,
}

#[wasm_bindgen]
impl JsFieldDefinition {
    #[wasm_bindgen(constructor)]
    pub fn new(
        index: usize,
        name: String,
        field_type: JsFieldType,
        description: Option<String>,
    ) -> Self {
        Self {
            index,
            name,
            field_type,
            description,
        }
    }
}

impl From<FieldDefinition> for JsFieldDefinition {
    fn from(field_def: FieldDefinition) -> Self {
        JsFieldDefinition {
            index: field_def.index,
            name: field_def.name,
            field_type: field_def.field_type.into(),
            description: field_def.description,
        }
    }
}

impl From<JsFieldDefinition> for FieldDefinition {
    fn from(field_def: JsFieldDefinition) -> Self {
        FieldDefinition {
            index: field_def.index,
            name: field_def.name,
            field_type: field_def.field_type.into(),
            description: field_def.description,
        }
    }
}

/// Dictionary schema definition.
#[wasm_bindgen(js_name = "Schema")]
#[derive(Clone)]
pub struct JsSchema {
    pub(crate) inner: Schema,
}

#[wasm_bindgen]
impl JsSchema {
    #[wasm_bindgen(constructor)]
    pub fn new(fields: Vec<String>) -> Self {
        Self {
            inner: Schema::new(fields),
        }
    }

    pub fn create_default() -> Self {
        Self {
            inner: Schema::default(),
        }
    }

    pub fn get_field_index(&self, field_name: &str) -> Option<usize> {
        self.inner.get_field_index(field_name)
    }

    pub fn field_count(&self) -> usize {
        self.inner.get_all_fields().len()
    }

    pub fn get_field_name(&self, index: usize) -> Option<String> {
        self.inner.get_all_fields().get(index).cloned()
    }

    pub fn get_custom_fields(&self) -> Vec<String> {
        let fields = self.inner.get_all_fields();
        if fields.len() > 4 {
            fields[4..].to_vec()
        } else {
            Vec::new()
        }
    }

    pub fn get_all_fields(&self) -> Vec<String> {
        self.inner.get_all_fields().to_vec()
    }

    pub fn get_field_by_name(&self, name: &str) -> Option<JsFieldDefinition> {
        self.get_field_index(name).map(|index| {
            let field_type = if index < 4 {
                match index {
                    0 => JsFieldType::Surface,
                    1 => JsFieldType::LeftContextId,
                    2 => JsFieldType::RightContextId,
                    3 => JsFieldType::Cost,
                    _ => unreachable!(),
                }
            } else {
                JsFieldType::Custom
            };

            JsFieldDefinition {
                index,
                name: name.to_string(),
                field_type,
                description: None,
            }
        })
    }
}

impl From<Schema> for JsSchema {
    fn from(schema: Schema) -> Self {
        JsSchema { inner: schema }
    }
}

impl From<JsSchema> for Schema {
    fn from(schema: JsSchema) -> Self {
        schema.inner
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(target_arch = "wasm32")]
    use wasm_bindgen_test::wasm_bindgen_test;

    #[cfg(target_arch = "wasm32")]
    #[wasm_bindgen_test]
    fn test_schema_new_wasm() {
        let fields = vec![
            "surface".to_string(),
            "left_id".to_string(),
            "right_id".to_string(),
            "cost".to_string(),
            "pos".to_string(),
        ];
        let schema = JsSchema::new(fields);

        assert_eq!(schema.field_count(), 5);
    }

    #[cfg(target_arch = "wasm32")]
    #[wasm_bindgen_test]
    fn test_schema_field_operations_wasm() {
        let fields = vec![
            "surface".to_string(),
            "left_id".to_string(),
            "right_id".to_string(),
            "cost".to_string(),
            "pos".to_string(),
            "reading".to_string(),
        ];
        let schema = JsSchema::new(fields.clone());

        // field_count
        assert_eq!(schema.field_count(), 6);

        // get_field_index
        assert_eq!(schema.get_field_index("surface"), Some(0));
        assert_eq!(schema.get_field_index("pos"), Some(4));
        assert_eq!(schema.get_field_index("nonexistent"), None);

        // get_field_name
        assert_eq!(schema.get_field_name(0), Some("surface".to_string()));
        assert_eq!(schema.get_field_name(999), None);

        // get_all_fields
        let all = schema.get_all_fields();
        assert_eq!(all, fields);

        // get_custom_fields (fields beyond index 3)
        let custom = schema.get_custom_fields();
        assert_eq!(custom, vec!["pos".to_string(), "reading".to_string()]);
    }

    #[cfg(target_arch = "wasm32")]
    #[wasm_bindgen_test]
    fn test_schema_get_field_by_name_wasm() {
        let fields = vec![
            "surface".to_string(),
            "left_id".to_string(),
            "right_id".to_string(),
            "cost".to_string(),
            "pos".to_string(),
        ];
        let schema = JsSchema::new(fields);

        // Built-in field
        let surface_field = schema.get_field_by_name("surface").unwrap();
        assert_eq!(surface_field.index, 0);
        assert_eq!(surface_field.name, "surface");
        assert!(matches!(surface_field.field_type, JsFieldType::Surface));

        // Custom field
        let pos_field = schema.get_field_by_name("pos").unwrap();
        assert_eq!(pos_field.index, 4);
        assert!(matches!(pos_field.field_type, JsFieldType::Custom));

        // Non-existent field
        assert!(schema.get_field_by_name("nonexistent").is_none());
    }

    #[test]
    fn test_schema_new() {
        let fields = vec![
            "surface".to_string(),
            "left_id".to_string(),
            "right_id".to_string(),
            "cost".to_string(),
            "pos".to_string(),
        ];
        let schema = JsSchema::new(fields);

        assert_eq!(schema.field_count(), 5);
    }

    #[test]
    fn test_schema_field_operations() {
        let fields = vec![
            "surface".to_string(),
            "left_id".to_string(),
            "right_id".to_string(),
            "cost".to_string(),
            "pos".to_string(),
            "reading".to_string(),
        ];
        let schema = JsSchema::new(fields.clone());

        assert_eq!(schema.field_count(), 6);

        assert_eq!(schema.get_field_index("surface"), Some(0));
        assert_eq!(schema.get_field_index("pos"), Some(4));
        assert_eq!(schema.get_field_index("nonexistent"), None);

        assert_eq!(schema.get_field_name(0), Some("surface".to_string()));
        assert_eq!(schema.get_field_name(999), None);

        let all = schema.get_all_fields();
        assert_eq!(all, fields);

        let custom = schema.get_custom_fields();
        assert_eq!(custom, vec!["pos".to_string(), "reading".to_string()]);
    }

    #[test]
    fn test_schema_get_custom_fields_no_custom() {
        let fields = vec![
            "surface".to_string(),
            "left_id".to_string(),
            "right_id".to_string(),
            "cost".to_string(),
        ];
        let schema = JsSchema::new(fields);

        assert!(schema.get_custom_fields().is_empty());
    }

    #[test]
    fn test_schema_create_default() {
        let schema = JsSchema::create_default();

        assert_eq!(schema.field_count(), 13);
        assert_eq!(schema.get_field_index("surface"), Some(0));
        assert_eq!(schema.get_field_index("left_context_id"), Some(1));
        assert_eq!(schema.get_field_index("right_context_id"), Some(2));
        assert_eq!(schema.get_field_index("cost"), Some(3));
        assert_eq!(schema.get_field_index("major_pos"), Some(4));
        assert_eq!(schema.get_field_index("pronunciation"), Some(12));
    }

    #[test]
    fn test_schema_get_field_by_name() {
        let fields = vec![
            "surface".to_string(),
            "left_id".to_string(),
            "right_id".to_string(),
            "cost".to_string(),
            "pos".to_string(),
        ];
        let schema = JsSchema::new(fields);

        let surface_field = schema.get_field_by_name("surface").unwrap();
        assert_eq!(surface_field.index, 0);
        assert_eq!(surface_field.name, "surface");
        assert!(matches!(surface_field.field_type, JsFieldType::Surface));

        let left_id_field = schema.get_field_by_name("left_id").unwrap();
        assert_eq!(left_id_field.index, 1);
        assert!(matches!(
            left_id_field.field_type,
            JsFieldType::LeftContextId
        ));

        let right_id_field = schema.get_field_by_name("right_id").unwrap();
        assert_eq!(right_id_field.index, 2);
        assert!(matches!(
            right_id_field.field_type,
            JsFieldType::RightContextId
        ));

        let cost_field = schema.get_field_by_name("cost").unwrap();
        assert_eq!(cost_field.index, 3);
        assert!(matches!(cost_field.field_type, JsFieldType::Cost));

        let pos_field = schema.get_field_by_name("pos").unwrap();
        assert_eq!(pos_field.index, 4);
        assert!(matches!(pos_field.field_type, JsFieldType::Custom));

        assert!(schema.get_field_by_name("nonexistent").is_none());
    }

    #[test]
    fn test_field_type_from_into_conversions() {
        let pairs = [
            (JsFieldType::Surface, FieldType::Surface),
            (JsFieldType::LeftContextId, FieldType::LeftContextId),
            (JsFieldType::RightContextId, FieldType::RightContextId),
            (JsFieldType::Cost, FieldType::Cost),
            (JsFieldType::Custom, FieldType::Custom),
        ];

        for (js_type, lindera_type) in pairs {
            let converted: FieldType = js_type.into();
            assert_eq!(
                std::mem::discriminant(&converted),
                std::mem::discriminant(&lindera_type)
            );

            let back: JsFieldType = lindera_type.into();
            assert_eq!(
                std::mem::discriminant(&back),
                std::mem::discriminant(&js_type)
            );
        }
    }

    #[test]
    fn test_field_definition_new() {
        let field = JsFieldDefinition::new(
            0,
            "surface".to_string(),
            JsFieldType::Surface,
            Some("Surface form".to_string()),
        );

        assert_eq!(field.index, 0);
        assert_eq!(field.name, "surface");
        assert!(matches!(field.field_type, JsFieldType::Surface));
        assert_eq!(field.description, Some("Surface form".to_string()));
    }

    #[test]
    fn test_field_definition_from_into_conversions() {
        let js_field = JsFieldDefinition::new(
            4,
            "pos".to_string(),
            JsFieldType::Custom,
            Some("Part of speech".to_string()),
        );

        let lindera_field: FieldDefinition = js_field.into();
        assert_eq!(lindera_field.index, 4);
        assert_eq!(lindera_field.name, "pos");
        assert!(matches!(lindera_field.field_type, FieldType::Custom));
        assert_eq!(
            lindera_field.description,
            Some("Part of speech".to_string())
        );

        let back: JsFieldDefinition = lindera_field.into();
        assert_eq!(back.index, 4);
        assert_eq!(back.name, "pos");
        assert!(matches!(back.field_type, JsFieldType::Custom));
    }

    #[test]
    fn test_schema_from_into_conversions() {
        let js_schema = JsSchema::new(vec![
            "surface".to_string(),
            "left_id".to_string(),
            "right_id".to_string(),
            "cost".to_string(),
        ]);

        let lindera_schema: Schema = js_schema.into();
        assert_eq!(lindera_schema.get_all_fields().len(), 4);

        let back: JsSchema = lindera_schema.into();
        assert_eq!(back.field_count(), 4);
        assert_eq!(back.get_field_name(0), Some("surface".to_string()));
    }
}