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
use jpreprocess_core::pronunciation::Pronunciation;

use jpreprocess_njd::NJDNode;

use crate::word_attr::*;

pub struct Word {
    pos: Option<u8>,
    ctype: Option<u8>,
    cform: Option<u8>,
    pub moras: Pronunciation,
}

impl Word {
    pub fn to_b(&self) -> String {
        format!(
            "/B:{}-{}_{}",
            Self::format_id(self.pos, true),
            Self::format_id(self.ctype, false),
            Self::format_id(self.cform, false)
        )
    }
    pub fn to_c(&self) -> String {
        format!(
            "/C:{}_{}+{}",
            Self::format_id(self.pos, true),
            Self::format_id(self.ctype, false),
            Self::format_id(self.cform, false)
        )
    }
    pub fn to_d(&self) -> String {
        format!(
            "/D:{}+{}_{}",
            Self::format_id(self.pos, true),
            Self::format_id(self.ctype, false),
            Self::format_id(self.cform, false)
        )
    }

    fn format_id(id: Option<u8>, is_long: bool) -> String {
        if let Some(id) = id {
            if is_long {
                format!("{:0>2}", id)
            } else {
                format!("{:0>1}", id)
            }
        } else {
            "xx".to_string()
        }
    }

    pub fn count_mora(&self) -> usize {
        self.moras.mora_size()
    }
}

impl From<&NJDNode> for Word {
    fn from(njdnode: &NJDNode) -> Self {
        Self {
            pos: pos_to_id(njdnode.get_pos()),
            ctype: ctype_to_id(njdnode.get_ctype()),
            cform: cform_to_id(njdnode.get_cform()),
            moras: njdnode.get_pron().clone(),
        }
    }
}