Skip to main content

lindera_core/dictionary/
word_entry.rs

1use std::io;
2
3use byteorder::{ByteOrder, LittleEndian, WriteBytesExt};
4use serde::{Deserialize, Serialize};
5
6#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
7pub struct WordId(pub u32, pub bool);
8
9impl WordId {
10    pub fn is_unknown(&self) -> bool {
11        self.0 == u32::MAX
12    }
13    pub fn is_system(&self) -> bool {
14        self.1
15    }
16}
17
18impl Default for WordId {
19    fn default() -> Self {
20        WordId(u32::MAX, true)
21    }
22}
23
24#[derive(Default, Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
25pub struct WordEntry {
26    pub word_id: WordId,
27    pub word_cost: i16,
28    pub left_id: u16,
29    pub right_id: u16,
30}
31
32impl WordEntry {
33    pub const SERIALIZED_LEN: usize = 10;
34
35    pub fn left_id(&self) -> u32 {
36        self.left_id as u32
37    }
38
39    pub fn right_id(&self) -> u32 {
40        self.right_id as u32
41    }
42
43    pub fn serialize<W: io::Write>(&self, wtr: &mut W) -> io::Result<()> {
44        wtr.write_u32::<LittleEndian>(self.word_id.0)?;
45        wtr.write_i16::<LittleEndian>(self.word_cost)?;
46        wtr.write_u16::<LittleEndian>(self.left_id)?;
47        wtr.write_u16::<LittleEndian>(self.right_id)?;
48        Ok(())
49    }
50
51    pub fn deserialize(data: &[u8], is_system_entry: bool) -> WordEntry {
52        let word_id = WordId(LittleEndian::read_u32(&data[0..4]), is_system_entry);
53        let word_cost = LittleEndian::read_i16(&data[4..6]);
54        let left_id = LittleEndian::read_u16(&data[6..8]);
55        let right_id = LittleEndian::read_u16(&data[8..10]);
56        WordEntry {
57            word_id,
58            word_cost,
59            left_id,
60            right_id,
61        }
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use crate::dictionary::word_entry::{WordEntry, WordId};
68
69    #[test]
70    fn test_word_entry() {
71        let mut buffer = Vec::new();
72        let word_entry = WordEntry {
73            word_id: WordId(1u32, true),
74            word_cost: -17i16,
75            left_id: 1411u16,
76            right_id: 1412u16,
77        };
78        word_entry.serialize(&mut buffer).unwrap();
79        assert_eq!(WordEntry::SERIALIZED_LEN, buffer.len());
80        let word_entry2 = WordEntry::deserialize(&buffer[..], true);
81        assert_eq!(word_entry, word_entry2);
82    }
83}