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
//! Dictionary for tokenization.
pub 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, ConnectorWrapper};
use crate::dictionary::lexicon::Lexicon;
use crate::dictionary::mapper::ConnIdMapper;
use crate::dictionary::unknown::UnkHandler;
use crate::errors::{Result, VibratoError};
pub use crate::dictionary::builder::SystemDictionaryBuilder;
pub use crate::dictionary::word_idx::WordIdx;
pub(crate) use crate::dictionary::lexicon::WordParam;
const MODEL_MAGIC: &[u8] = b"VibratoTokenizer 0.5\n";
/// Type of a lexicon that contains the word.
#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash, Decode, Encode)]
#[repr(u8)]
pub enum LexType {
/// System lexicon.
System,
/// User lexicon.
User,
/// Unknown words.
Unknown,
}
impl Default for LexType {
fn default() -> Self {
Self::System
}
}
/// Inner data of [`Dictionary`].
#[derive(Decode, Encode)]
pub(crate) struct DictionaryInner {
system_lexicon: Lexicon,
user_lexicon: Option<Lexicon>,
connector: ConnectorWrapper,
mapper: Option<ConnIdMapper>,
char_prop: CharProperty,
unk_handler: UnkHandler,
}
/// Dictionary for tokenization.
pub struct Dictionary {
pub(crate) data: DictionaryInner,
}
impl Dictionary {
/// Gets the reference to the system lexicon.
#[inline(always)]
pub(crate) const fn system_lexicon(&self) -> &Lexicon {
&self.data.system_lexicon
}
/// Gets the reference to the user lexicon.
#[inline(always)]
pub(crate) const fn user_lexicon(&self) -> Option<&Lexicon> {
self.data.user_lexicon.as_ref()
}
/// Gets the reference to the connection matrix.
#[inline(always)]
pub(crate) const fn connector(&self) -> &ConnectorWrapper {
&self.data.connector
}
/// Gets the reference to the mapper for connection ids.
#[allow(dead_code)]
#[inline(always)]
pub(crate) const fn mapper(&self) -> Option<&ConnIdMapper> {
self.data.mapper.as_ref()
}
/// Gets the reference to the character property.
#[inline(always)]
pub(crate) const fn char_prop(&self) -> &CharProperty {
&self.data.char_prop
}
/// Gets the reference to the handler of unknown words.
#[inline(always)]
pub(crate) const fn unk_handler(&self) -> &UnkHandler {
&self.data.unk_handler
}
/// Gets the word parameter.
#[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),
}
}
/// Gets the reference to the feature string.
#[inline(always)]
pub 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),
}
}
/// Exports the dictionary data.
///
/// # Examples
///
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use std::fs::File;
///
/// use vibrato::SystemDictionaryBuilder;
///
/// let dict = SystemDictionaryBuilder::from_readers(
/// File::open("src/tests/resources/lex.csv")?,
/// File::open("src/tests/resources/matrix.def")?,
/// File::open("src/tests/resources/char.def")?,
/// File::open("src/tests/resources/unk.def")?,
/// )?;
///
/// let writer = File::create("path/to/system.dic")?;
/// dict.write(writer)?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// When bincode generates an error, it will be returned as is.
pub fn write<W>(&self, mut wtr: W) -> Result<usize>
where
W: Write,
{
wtr.write_all(MODEL_MAGIC)?;
let config = common::bincode_config();
let num_bytes = bincode::encode_into_std_write(&self.data, &mut wtr, config)?;
Ok(MODEL_MAGIC.len() + num_bytes)
}
/// Creates a dictionary from raw dictionary data.
///
/// The argument must be a byte sequence exported by the [`Dictionary::write()`] function.
///
/// # Examples
///
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use std::fs::File;
///
/// use vibrato::Dictionary;
///
/// let reader = File::open("path/to/system.dic")?;
/// let dict = Dictionary::read(reader)?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// When bincode generates an error, it will be returned as is.
pub fn read<R>(rdr: R) -> Result<Self>
where
R: Read,
{
Ok(Self {
data: Self::read_common(rdr)?,
})
}
fn read_common<R>(mut rdr: R) -> Result<DictionaryInner>
where
R: Read,
{
let mut magic = [0; MODEL_MAGIC.len()];
rdr.read_exact(&mut magic)?;
if magic != MODEL_MAGIC {
return Err(VibratoError::invalid_argument(
"rdr",
"The magic number of the input model mismatches.",
));
}
let config = common::bincode_config();
let data = bincode::decode_from_std_read(&mut rdr, config)?;
Ok(data)
}
/// Resets the user dictionary from a reader.
///
/// # Arguments
///
/// - `user_lexicon_rdr`: A reader of a lexicon file `*.csv` in the MeCab format.
/// If `None`, clear the current user dictionary.
///
/// # Errors
///
/// [`VibratoError`] is returned when an input format is invalid.
pub fn reset_user_lexicon_from_reader<R>(mut self, user_lexicon_rdr: Option<R>) -> Result<Self>
where
R: Read,
{
if let Some(user_lexicon_rdr) = user_lexicon_rdr {
let mut user_lexicon = Lexicon::from_reader(user_lexicon_rdr, LexType::User)?;
if let Some(mapper) = self.data.mapper.as_ref() {
user_lexicon.map_connection_ids(mapper);
}
if !user_lexicon.verify(self.connector()) {
return Err(VibratoError::invalid_argument(
"user_lexicon_rdr",
"includes invalid connection ids.",
));
}
self.data.user_lexicon = Some(user_lexicon);
} else {
self.data.user_lexicon = None;
}
Ok(self)
}
/// Edits connection ids with the given mappings.
///
/// # Arguments
///
/// - `lmap/rmap`: An iterator of mappings of left/right ids, where
/// the `i`-th item (1-origin) indicates a new id mapped from id `i`.
///
/// # Errors
///
/// [`VibratoError`] is returned when
/// - a new id of [`BOS_EOS_CONNECTION_ID`](crate::common::BOS_EOS_CONNECTION_ID)
/// is included,
/// - new ids are duplicated, or
/// - the set of new ids are not same as that of old ids.
pub fn map_connection_ids_from_iter<L, R>(mut self, lmap: L, rmap: R) -> Result<Self>
where
L: IntoIterator<Item = u16>,
R: IntoIterator<Item = u16>,
{
let mapper = ConnIdMapper::from_iter(lmap, rmap)?;
self.data.system_lexicon.map_connection_ids(&mapper);
if let Some(user_lexicon) = self.data.user_lexicon.as_mut() {
user_lexicon.map_connection_ids(&mapper);
}
self.data.connector.map_connection_ids(&mapper);
self.data.unk_handler.map_connection_ids(&mapper);
self.data.mapper = Some(mapper);
Ok(self)
}
}