lindera-nodejs 3.0.5

A Node.js binding for Lindera.
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
//! Dictionary schema definitions.
//!
//! This module provides schema structures that define the format and fields
//! of dictionary entries.

use std::collections::HashMap;

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

/// Field type in dictionary schema.
///
/// Defines the type of a field in the dictionary entry.
#[napi(string_enum)]
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.
///
/// Describes a single field in the dictionary entry format.
#[napi(object)]
pub struct JsFieldDefinition {
    /// Field index in the record.
    pub index: u32,
    /// Field name.
    pub name: String,
    /// Field type.
    pub field_type: JsFieldType,
    /// Optional description of the field.
    pub description: Option<String>,
}

impl From<FieldDefinition> for JsFieldDefinition {
    fn from(field_def: FieldDefinition) -> Self {
        JsFieldDefinition {
            index: field_def.index as u32,
            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 as usize,
            name: field_def.name,
            field_type: field_def.field_type.into(),
            description: field_def.description,
        }
    }
}

/// Dictionary schema definition.
///
/// Defines the structure and fields of dictionary entries.
#[napi(js_name = "Schema")]
pub struct JsSchema {
    /// Field names in the schema.
    fields: Vec<String>,
    /// Index map for fast field lookup.
    field_index_map: HashMap<String, usize>,
}

#[napi]
impl JsSchema {
    /// Creates a new schema with the specified field names.
    ///
    /// # Arguments
    ///
    /// * `fields` - Array of field name strings.
    #[napi(constructor)]
    pub fn new(fields: Vec<String>) -> Self {
        let field_index_map = fields
            .iter()
            .enumerate()
            .map(|(i, f)| (f.clone(), i))
            .collect();
        Self {
            fields,
            field_index_map,
        }
    }

    /// Creates a default schema matching the IPADIC format (13 fields).
    ///
    /// # Returns
    ///
    /// A schema with the standard IPADIC field definitions.
    #[napi(factory)]
    pub fn create_default() -> Self {
        Self::new(vec![
            "surface".to_string(),
            "left_context_id".to_string(),
            "right_context_id".to_string(),
            "cost".to_string(),
            "major_pos".to_string(),
            "middle_pos".to_string(),
            "small_pos".to_string(),
            "fine_pos".to_string(),
            "conjugation_type".to_string(),
            "conjugation_form".to_string(),
            "base_form".to_string(),
            "reading".to_string(),
            "pronunciation".to_string(),
        ])
    }

    /// Returns the field names in the schema.
    #[napi(getter)]
    pub fn fields(&self) -> Vec<String> {
        self.fields.clone()
    }

    /// Returns the index of the specified field name.
    ///
    /// # Arguments
    ///
    /// * `field_name` - Name of the field to look up.
    ///
    /// # Returns
    ///
    /// The zero-based index of the field, or `undefined` if not found.
    #[napi]
    pub fn get_field_index(&self, field_name: String) -> Option<u32> {
        self.field_index_map.get(&field_name).map(|&i| i as u32)
    }

    /// Returns the total number of fields in the schema.
    #[napi]
    pub fn field_count(&self) -> u32 {
        self.fields.len() as u32
    }

    /// Returns the field name at the specified index.
    ///
    /// # Arguments
    ///
    /// * `index` - Zero-based index.
    ///
    /// # Returns
    ///
    /// The field name, or `undefined` if the index is out of range.
    #[napi]
    pub fn get_field_name(&self, index: u32) -> Option<String> {
        self.fields.get(index as usize).cloned()
    }

    /// Returns the custom fields (index 4 and above).
    ///
    /// # Returns
    ///
    /// An array of custom field names.
    #[napi]
    pub fn get_custom_fields(&self) -> Vec<String> {
        if self.fields.len() > 4 {
            self.fields[4..].to_vec()
        } else {
            Vec::new()
        }
    }

    /// Returns all field names in the schema.
    ///
    /// # Returns
    ///
    /// An array of all field names.
    #[napi]
    pub fn get_all_fields(&self) -> Vec<String> {
        self.fields.clone()
    }

    /// Returns the field definition for the specified field name.
    ///
    /// # Arguments
    ///
    /// * `name` - Name of the field to look up.
    ///
    /// # Returns
    ///
    /// The field definition, or `undefined` if not found.
    #[napi]
    pub fn get_field_by_name(&self, name: String) -> Option<JsFieldDefinition> {
        self.field_index_map.get(&name).map(|&index| {
            let field_type = match index {
                0 => JsFieldType::Surface,
                1 => JsFieldType::LeftContextId,
                2 => JsFieldType::RightContextId,
                3 => JsFieldType::Cost,
                _ => JsFieldType::Custom,
            };

            JsFieldDefinition {
                index: index as u32,
                name,
                field_type,
                description: None,
            }
        })
    }

    /// Validates that a CSV record matches the schema.
    ///
    /// # Arguments
    ///
    /// * `record` - Array of field values to validate.
    #[napi]
    pub fn validate_record(&self, record: Vec<String>) -> napi::Result<()> {
        if record.len() < self.fields.len() {
            return Err(napi::Error::new(
                napi::Status::InvalidArg,
                format!(
                    "CSV row has {} fields but schema requires {} fields",
                    record.len(),
                    self.fields.len()
                ),
            ));
        }

        for (index, field_name) in self.fields.iter().enumerate() {
            if index < record.len() && record[index].trim().is_empty() {
                return Err(napi::Error::new(
                    napi::Status::InvalidArg,
                    format!("Field {field_name} is missing or empty"),
                ));
            }
        }

        Ok(())
    }
}

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

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

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

    #[test]
    fn test_js_field_type_to_field_type_all_variants() {
        assert!(matches!(
            FieldType::from(JsFieldType::Surface),
            FieldType::Surface
        ));
        assert!(matches!(
            FieldType::from(JsFieldType::LeftContextId),
            FieldType::LeftContextId
        ));
        assert!(matches!(
            FieldType::from(JsFieldType::RightContextId),
            FieldType::RightContextId
        ));
        assert!(matches!(
            FieldType::from(JsFieldType::Cost),
            FieldType::Cost
        ));
        assert!(matches!(
            FieldType::from(JsFieldType::Custom),
            FieldType::Custom
        ));
    }

    #[test]
    fn test_field_type_to_js_field_type_all_variants() {
        assert!(matches!(
            JsFieldType::from(FieldType::Surface),
            JsFieldType::Surface
        ));
        assert!(matches!(
            JsFieldType::from(FieldType::LeftContextId),
            JsFieldType::LeftContextId
        ));
        assert!(matches!(
            JsFieldType::from(FieldType::RightContextId),
            JsFieldType::RightContextId
        ));
        assert!(matches!(
            JsFieldType::from(FieldType::Cost),
            JsFieldType::Cost
        ));
        assert!(matches!(
            JsFieldType::from(FieldType::Custom),
            JsFieldType::Custom
        ));
    }

    #[test]
    fn test_js_schema_new_builds_index_map() {
        let schema = JsSchema::new(vec!["a".to_string(), "b".to_string(), "c".to_string()]);
        assert_eq!(schema.get_field_index("a".to_string()), Some(0));
        assert_eq!(schema.get_field_index("b".to_string()), Some(1));
        assert_eq!(schema.get_field_index("c".to_string()), Some(2));
    }

    #[test]
    fn test_js_schema_get_field_index_not_found() {
        let schema = JsSchema::new(vec!["x".to_string()]);
        assert_eq!(schema.get_field_index("y".to_string()), None);
    }

    #[test]
    fn test_js_schema_field_count() {
        let schema = JsSchema::new(vec!["a".to_string(), "b".to_string(), "c".to_string()]);
        assert_eq!(schema.field_count(), 3);
    }

    #[test]
    fn test_js_schema_field_count_empty() {
        let schema = JsSchema::new(vec![]);
        assert_eq!(schema.field_count(), 0);
    }

    #[test]
    fn test_js_schema_get_custom_fields() {
        let schema = JsSchema::new(vec![
            "surface".to_string(),
            "left_context_id".to_string(),
            "right_context_id".to_string(),
            "cost".to_string(),
            "pos1".to_string(),
            "pos2".to_string(),
        ]);
        let custom = schema.get_custom_fields();
        assert_eq!(custom, vec!["pos1".to_string(), "pos2".to_string()]);
    }

    #[test]
    fn test_js_schema_get_custom_fields_no_custom() {
        let schema = JsSchema::new(vec![
            "surface".to_string(),
            "left_context_id".to_string(),
            "right_context_id".to_string(),
            "cost".to_string(),
        ]);
        let custom = schema.get_custom_fields();
        assert!(custom.is_empty());
    }

    #[test]
    fn test_js_schema_get_custom_fields_fewer_than_4() {
        let schema = JsSchema::new(vec!["surface".to_string()]);
        let custom = schema.get_custom_fields();
        assert!(custom.is_empty());
    }

    #[test]
    fn test_js_schema_create_default_has_13_fields() {
        let schema = JsSchema::create_default();
        assert_eq!(schema.field_count(), 13);
    }

    #[test]
    fn test_js_schema_create_default_field_names() {
        let schema = JsSchema::create_default();
        assert_eq!(schema.get_field_index("surface".to_string()), Some(0));
        assert_eq!(
            schema.get_field_index("pronunciation".to_string()),
            Some(12)
        );
    }

    #[test]
    fn test_js_schema_to_lindera_schema_roundtrip() {
        let fields = vec![
            "surface".to_string(),
            "left_context_id".to_string(),
            "right_context_id".to_string(),
            "cost".to_string(),
            "pos".to_string(),
        ];
        let js_schema = JsSchema::new(fields.clone());
        let lindera_schema: Schema = js_schema.into();
        let roundtripped: JsSchema = lindera_schema.into();
        assert_eq!(roundtripped.field_count(), 5);
        assert_eq!(roundtripped.get_field_index("pos".to_string()), Some(4));
    }

    #[test]
    fn test_lindera_schema_to_js_schema() {
        let lindera_schema = Schema::new(vec!["a".to_string(), "b".to_string()]);
        let js_schema: JsSchema = lindera_schema.into();
        assert_eq!(js_schema.field_count(), 2);
        assert_eq!(js_schema.get_field_index("a".to_string()), Some(0));
        assert_eq!(js_schema.get_field_index("b".to_string()), Some(1));
    }
}