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
pub(crate) mod builder;
pub(crate) mod character;
pub(crate) mod connector;
pub(crate) mod lexicon;
pub(crate) mod mapper;
pub(crate) mod unknown;
pub(crate) mod word_idx;
use std::io::{Read, Write};
use bincode::{Decode, Encode};
use crate::common;
use crate::dictionary::character::CharProperty;
use crate::dictionary::connector::Connector;
use crate::dictionary::lexicon::Lexicon;
use crate::dictionary::mapper::ConnIdMapper;
use crate::dictionary::unknown::UnkHandler;
use crate::dictionary::word_idx::WordIdx;
use crate::errors::Result;
pub(crate) use crate::dictionary::lexicon::WordParam;
#[derive(Clone, Copy, Eq, PartialEq, Debug, Decode, Encode)]
#[repr(u8)]
pub enum LexType {
System,
User,
Unknown,
}
impl Default for LexType {
fn default() -> Self {
Self::System
}
}
#[derive(Decode, Encode)]
pub(crate) struct DictionaryInner {
system_lexicon: Lexicon,
user_lexicon: Option<Lexicon>,
connector: Connector,
mapper: Option<ConnIdMapper>,
char_prop: CharProperty,
unk_handler: UnkHandler,
}
pub struct Dictionary {
pub(crate) data: DictionaryInner,
pub(crate) need_check: bool,
}
impl Dictionary {
#[inline(always)]
pub(crate) const fn system_lexicon(&self) -> &Lexicon {
&self.data.system_lexicon
}
#[inline(always)]
pub(crate) const fn user_lexicon(&self) -> Option<&Lexicon> {
self.data.user_lexicon.as_ref()
}
#[inline(always)]
pub(crate) const fn connector(&self) -> &Connector {
&self.data.connector
}
#[allow(dead_code)]
#[inline(always)]
pub(crate) const fn mapper(&self) -> Option<&ConnIdMapper> {
self.data.mapper.as_ref()
}
#[inline(always)]
pub(crate) const fn char_prop(&self) -> &CharProperty {
&self.data.char_prop
}
#[inline(always)]
pub(crate) const fn unk_handler(&self) -> &UnkHandler {
&self.data.unk_handler
}
#[inline(always)]
pub(crate) fn word_param(&self, word_idx: WordIdx) -> WordParam {
match word_idx.lex_type {
LexType::System => self.system_lexicon().word_param(word_idx),
LexType::User => self.user_lexicon().unwrap().word_param(word_idx),
LexType::Unknown => self.unk_handler().word_param(word_idx),
}
}
#[inline(always)]
pub(crate) fn word_feature(&self, word_idx: WordIdx) -> &str {
match word_idx.lex_type {
LexType::System => self.system_lexicon().word_feature(word_idx),
LexType::User => self.user_lexicon().unwrap().word_feature(word_idx),
LexType::Unknown => self.unk_handler().word_feature(word_idx),
}
}
pub fn write<W>(&self, mut wtr: W) -> Result<usize>
where
W: Write,
{
let num_bytes =
bincode::encode_into_std_write(&self.data, &mut wtr, common::bincode_config())?;
Ok(num_bytes)
}
pub fn read<R>(mut rdr: R) -> Result<Self>
where
R: Read,
{
let data = bincode::decode_from_std_read(&mut rdr, common::bincode_config())?;
Ok(Self {
data,
need_check: true,
})
}
pub unsafe fn read_unchecked<R>(mut rdr: R) -> Result<Self>
where
R: Read,
{
let data = bincode::decode_from_std_read(&mut rdr, common::bincode_config())?;
Ok(Self {
data,
need_check: false,
})
}
}